PDF Macro Enhancement

Hello

To follow up on the subject of the PDF macro (http://www.lynkoa.com/forum/cao/macro-d-enregistrement-pdf-dans-dossier-actuel#answer-617678), I would now like to improve a "bug", not blocking the use of the macro but somewhat annoying in the long run.

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

 

Thank you in advance for your help,

Kind regards

Allan.

Hello

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

 

 

1 Like

Hello

Here's the one we're using, it saves the file with its name in the drawing folder.


macro_pdf.swp

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...

There was a parenthesis missing between GetAttr(Replace(UCase(PathName), "SLDDRW", "PDF") and AND,

Here is the new code:

 

Code Removal

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

End if
End Sub

Thank you .PL you go out of your way for me, unfortunately the macro still doesn't work, it doesn't generate anything at all.

I'm trying to do my best not to waste your time, so I went back to the parentheses etc but I don't see anything sorry, the code seems good to me...

Is there a mistake or nothing happens?

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

    Else
        longstatus = Part.SaveAs3(Replace(UCase(PathName), "SLDDRW", "PDF"), 0, 0)

End if
End Sub

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?

Thank you for your help.

 

Have you been able to test mine?

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.

Good evening

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).

Here is the macro we use:

 

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim lErrors As Long
Dim lWarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
swModel.Extension.SaveAs GetFilename(swModel.GetPathName) & " rev." & swModel.GetCustomInfoValue("", "Revision") & ".pdf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings

End Sub


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

Hello

Here's the one that works and I just tested it:

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim lErrors As Long
Dim lWarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
swModel.Extension.SaveAs GetFilename(swModel.GetPathName) & " rev." & swModel.GetCustomInfoValue("", "Revision") & ".pdf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings

End Sub

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

Hello

 

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?

 

Come on, I'll test your codes!

Small feedback after tests. 

 

For my part, the PL code doesn't work. 

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....