When a .PDF plan is opened at the workshop for consultation and I need to make a change. on this same plane directly on Solidworks, and I use this PDF macro, Solidworks crashes and closes, because it can't overwrite the PDF file since it's already open.
However, when I try to save the plan by .PDF using "File"-"Save sous-.PDF", the message "This file is read-only" appears and allows Solidworks not to crash.
Would it be possible to retrieve a line of code in my macro that would allow me to have this message when the file is already open, and not crash Solidworks?
Here is the macro .PL had proposed to me (thanks again!):
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Gets the full path of the current document, including the file name: PathName = UCase(Part.GetPathName)
Dim myModelView As Object Set myModelView = Part.ActiveView myModelView.FrameState = swWindowState_e.swWindowMaximized boolstatus = Part.EditRebuild3() Part.ViewZoomtofit2 Part.ViewZoomtofit2 Part.ViewZoomtofit2 longstatus = Part.SaveAs3(Replace(UCase(PathName), "SLDDRW", "PDF"), 0, 0) End Sub
You have to check if the file is open to create a warning msgbox.
See this page :
https://support.microsoft.com/en-us/kb/153058
And here is the code:
Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Gets the full path of the current document, including the file name: PathName = UCase(Part.GetPathName) Dim myModelView As Object Set myModelView = Part.ActiveView myModelView.FrameState = swWindowState_e.swWindowMaximized boolstatus = Part.EditRebuild3() Part.ViewZoomtofit2 Part.ViewZoomtofit2 Part.ViewZoomtofit2 If GetAttr(Replace(UCase(PathName), "SLDDRW", "PDF") And vbReadOnly Then MsgBox "Read-Only File" Else longstatus = Part.SaveAs3(Replace(UCase(PathName), "SLDDRW", "PDF"), 0, 0) End If End Sub
Thank you very much for your answer .PL, however the macro doesn't work, apparently there is a problem with this line: If GetAttr(Replace(UCase(PathName), "SLDDRW", "PDF") And vbReadOnly Then
By taking a look at the link you also provided me I saw that there should be a point before the ReadOnly, but I tried to add it and it didn't change anything.
Thank you in advance, and sorry for my total ignorance in VBA codification...
I modified the code in case the PDF file doesn't exist, here it is:
Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Gets the full path of the current document, including the file name: PathName = UCase(Part.GetPathName) Dim myModelView As Object Set myModelView = Part.ActiveView myModelView.FrameState = swWindowState_e.swWindowMaximized boolstatus = Part.EditRebuild3() Part.ViewZoomtofit2 Part.ViewZoomtofit2 Part.ViewZoomtofit2
If Dir(Replace(UCase(PathName), "SLDDRW", "PDF")) <> "" Then If GetAttr(Replace(UCase(PathName), "SLDDRW", "PDF")) And vbReadOnly Then MsgBox "Read-Only File" Else longstatus = Part.SaveAs3(Replace(UCase(PathName), "SLDDRW", "PDF"), 0, 0) End If
Sorry, small mistake on my part, it will teach me not to test enough!
Now it should work!
Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Gets the full path of the current document, including the file name: PathName = UCase(Part.GetPathName) Dim myModelView As Object Set myModelView = Part.ActiveView myModelView.FrameState = swWindowState_e.swWindowMaximized boolstatus = Part.EditRebuild3() Part.ViewZoomtofit2 Part.ViewZoomtofit2 Part.ViewZoomtofit2
If Dir(Replace(UCase(PathName), "SLDDRW", "PDF")) <> "" Then If GetAttr(Replace(UCase(PathName), "SLDDRW", "PDF")) And vbReadOnly Then MsgBox "Read-Only File" Else longstatus = Part.SaveAs3(Replace(UCase(PathName), "SLDDRW", "PDF"), 0, 0) End If
Thank you for this new macro, indeed it works but under certain conditions, and to solve this I don't know if I'm the one who has to fix something or if the macro can be improved.
When I open a drawing and create a new PDF, no file.
On the other hand, when I try to generate the PDF from the drawing while it is open, it makes me crash Solidworks as before.
This issue is resolved when I open the PDF after checking the "Attributes: Read-only" checkbox in the properties. At this point the macro works and the message "Read-only file" appears when I try to generate the PDF. Only after closing the PDF, trying to regenerate, the message "Read-only file" still appears, as if the file remains open, when it is not. I manage to overcome the problem by unchecking "Attributes: Read-only" in the properties, and then the PDF wants to regenerate.
Is it up to me to set read/write rights or does the macro need to be modified?
Hello Manu67, I couldn't test your macro because when I click on the link of your answer, it opens an indescribable code for me, I have this same problem for all attachment links in message on the forum.
Unless I'm mistaken, the read-only attribute is not extractable on pdf files directly.
We should look at the file deletion functions of the file method (see: http://warin.developpez.com/access/fichiers/#LI-D-2) and apply the desired treatment according to the result (if error --> Read-only file).
Function GetFilename(strPath As String) As String Dim strTemp As String strTemp = Mid$(strPath, InStrRev(strPath, "\") + 1) GetFilename = Left$(strTemp, InStrRev(strTemp, ".") - 1) End Function
Function GetFilename(strPath As String) As String Dim strTemp As String strTemp = Mid$(strPath, InStrRev(strPath, "\") + 1) GetFilename = Left$(strTemp, InStrRev(strTemp, ".") - 1) End Function
I have exactly the same problem, I'm going to test your solution to see if it works and I'd like to push the vice even further. When a pdf plan is opened in Windows Explorer in the preview pane, the backup looks good but the pdf file is unreadable. An explanation?
manu67, I haven't tried your code because I don't understand it (and since I like to understand what I'm doing...)
So I did some research and I came up with this:
https://support.microsoft.com/en-us/kb/291295
to be completed with this (because error problem 75): http://stackoverflow.com/questions/12599322/check-if-a-file-is-already-open-vba
It works well, it detects if the PDF is open on another computer. On the other hand, I just left the open pdfs on another workstation and it still tells me that they are open... to see if it comes back on its own, if it's due to the server....