I'm here in need of help for a macro in SolidWorks that works correctly, but is lacking a crucial check while working.
The macro simply saves the document (drawing) as a PDF. Simple right?
Well, it saves the file in the same location the file is being worked from, locally or in a network and with the same name but the ".pdf" extension.
So far so good.
The problem is access rights. usually you have rights to write the file, but depending on certain conditions you don't.
This is what happens:
If there is no PDF of that file in the target location, it creates the PDF and it finishes fine.
If there is a PDF file already, ( say you are updating changes to the drawing), then it overwrites the file but only IF you have access to it.
Here is the problem:
If the file exists in the location and it is READ ONLY, then the macro never finishes and hangs up.
This also hangs up SolidWorks, causing it to crash-hang.
Your only option is force quit SolidWorks and restart, loosing your changes.
If the program is hanged up and you try to manually force DELETE the read only file in Explorer, it also crashes Explorer.
The macro is only missing a step to check if the file is read only.
If it is, then is should just tell the user the file is read only and finish.
I have tried many ways to do this , but since I'm not trained at all in VB, I simply cannot figure it out...
I have cut and paste several other VB subs but I cannot make it work properly... having errors.
Here is the macro steps that work fine, (I recorded back in 2012) but it lacks the file checking for read only:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
On Error Resume Next
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
Part.SaveAs2 NewFilePath, 0, True, False
End Sub
Can someone add the missing steps to check if the file to be recorded is read only and then quit the macro with a " File is read only" message?
Then the user can go and take control of the file prior to re-writing over it using the macro.
Much appreciated, Thanks!
Adrian
I'm here in need of help for a macro in SolidWorks that works correctly, but is lacking a crucial check while working.
The macro simply saves the document (drawing) as a PDF. Simple right?
Well, it saves the file in the same location the file is being worked from, locally or in a network and with the same name but the ".pdf" extension.
So far so good.
The problem is access rights. usually you have rights to write the file, but depending on certain conditions you don't.
This is what happens:
If there is no PDF of that file in the target location, it creates the PDF and it finishes fine.
If there is a PDF file already, ( say you are updating changes to the drawing), then it overwrites the file but only IF you have access to it.
Here is the problem:
If the file exists in the location and it is READ ONLY, then the macro never finishes and hangs up.
This also hangs up SolidWorks, causing it to crash-hang.
Your only option is force quit SolidWorks and restart, loosing your changes.
If the program is hanged up and you try to manually force DELETE the read only file in Explorer, it also crashes Explorer.
The macro is only missing a step to check if the file is read only.
If it is, then is should just tell the user the file is read only and finish.
I have tried many ways to do this , but since I'm not trained at all in VB, I simply cannot figure it out...
I have cut and paste several other VB subs but I cannot make it work properly... having errors.
Here is the macro steps that work fine, (I recorded back in 2012) but it lacks the file checking for read only:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
On Error Resume Next
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
Part.SaveAs2 NewFilePath, 0, True, False
End Sub
Can someone add the missing steps to check if the file to be recorded is read only and then quit the macro with a " File is read only" message?
Then the user can go and take control of the file prior to re-writing over it using the macro.
Much appreciated, Thanks!
Adrian
Share Improve this question edited Jan 19 at 6:35 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Jan 17 at 18:08 Adrian CrisciAdrian Crisci 13 bronze badges 4- Please edit your question title to describe the problem you're having or question you're asking. Your current title is nothing but a repetition of the tag information (which should not be in the title at all) and the word Macro, which says nothing meaningful. The title should be clear and descriptive enough that a future site user skimming a list of search results will know what it contains. For suggestions on writing a good title, see How to Ask. – Ken White Commented Jan 18 at 2:45
- 1 Thank you Ken, this was my first post ever in Stack overflow, and you are right the title was quite ambiguous. I just corrected it. Regards, – Adrian Crisci Commented Jan 19 at 6:06
- Thanks. I just improved on your edit. Again, the tag information should not appear in the title at all, with very rare exceptions (none of which apply to your question). Repeating them in the title is just useless noise that adds nothing of value. The tagging system works very well here. – Ken White Commented Jan 19 at 6:37
- 1 Perfect thank you! – Adrian Crisci Commented Jan 20 at 16:11
2 Answers
Reset to default 0Based on this thread:
NewFilePath = PathNoExtension & "pdf"
If Dir(NewFilePath) <> "" Then
Dim res As Long
res = GetAttr(NewFilePath)
If res = 33 Then
MsgBox "File is read only"
Exit Sub
End If
End If
Jerome:
Based on this post and your, answer I was able to solve the issue. The problem is that I'm working with SolidWorks PDM another layer of complexity.
PDM is marking the "res" = 1 if the file is read only (or checked-in into the vault)
And "res" = 0 is not read only (if the PDM document is check-out and available for write)
Based on your reply and the post, I was able to play around and make it work with the following:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
On Error Resume Next
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
Dim res As Long
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
res = GetAttr(NewFilePath)
If res = 0 Then
Part.SaveAs2 NewFilePath, 0, True, False
Exit Sub
Else
MsgBox "PDF File is not CHECK OUT"
End If
End Sub
Now I need to figure out the commands to simply Check it out and Check it in automatically from PDM and not force the user to do it manually.
Thank you so very much!.
Adrian
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349882a4623766.html
评论列表(0条)