Can't SaveAs PDF in PDM Task script...

This script runs, the task exits, and the task manager says it executed successfully. But it doesn't save the open drawing as a pdf. It opens it, it does all the pomp and circumstance, but when it runs the Extension.SaveAs command nothing actually happens. I tried changing visibility from False to True, neither helps. Using ModelDoc2.SaveAs3 works but is antiquated and doesn't appear to be able to force silent saving (so the pdf's open after being saved). Any thoughts? Thanks!

<p>Option Explicit</p><p>Sub main()</p><p>Dim swApp           As SldWorks.SldWorks</p><p>Dim swModel         As SldWorks.ModelDoc2</p><p>Dim swCustPrpMgr    As SldWorks.CustomPropertyManager</p><p>Dim longstatus      As Long</p><p>Dim longwarnings    As Long</p><p>Dim sFileName       As String</p><p>Dim Path            As String</p><p>Dim FileName        As String</p><p>Dim Value           As String</p><p>Dim DwgRev          As String</p><p>Dim Success       As Object</p><p>Dim swExtension       As SldWorks.ModelDocExtension</p><p>Dim Errors             As Long</p><p>Dim Warnings           As Long</p><p>Dim vSheetName          As Variant</p><p>Dim swExportPDFData     As SldWorks.ExportPdfData</p><p></p><p>On Error Resume Next</p><p></p><p>Set swApp = Application.SldWorks</p><p>swApp.Visible = True</p><p></p><p>sFileName = "<Filepath>"</p><p></p><p>    Set swModel = swApp.OpenDoc6(sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)</p><p>    Set swModel = swApp.ActiveDoc</p><p>             </p><p>    swModel.ViewZoomtofit2</p><p>    </p><p>    DwgRev = swModel.GetCustomInfoValue("", "Revision")</p><p>    </p><p>    If DwgRev = "?" Or DwgRev = "" Or DwgRev = "-" Then</p><p>        DwgRev = "--"</p><p>    End If</p><p></p><p>    vSheetName = swModel.GetSheetNames</p><p>    Set swExportPDFData = swApp.GetExportFileData(1)</p><p></p><p>    FileName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "_" & DwgRev & ".PDF"</p><p>    'swModel.SaveAs3 "" & FileName & "", 0, 0</p><p>    swExtension = swModel.Extensiona</p><p>    Success = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, vSheetName)</p><p>    swExportPDFData.ViewPdfAfterSaving = True</p><p>    'Success = swExtension.SaveAs(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Errors, Warnings)</p><p>    Success = swExtension.SaveAs(FileName, 0, 0, swExportPDFData, Errors, Warnings)</p><p>    swApp.QuitDoc swModel.GetTitle</p><p>    Set swModel = Nothing</p><p></p><p>End Sub</p>
SolidworksApi/macros
Answers 20
AJ
AJ 2021-04-09 Best Answer

Print :
FileName
Errors
Warnings
and let's see those values.

Best,

Amen Jlili

SOLIDWORKS API / PDM API Developer

www.cadsharp.com

DT
DT 2021-04-09

Nice catch! I think I moved on to the default PDM macro but I'm sure that was the problem. We will never know but I'll mark this as solved because I'm sure that was it.

Thanks!

MZ
MZ 2021-04-09

whats that?

comment imagecomment image
DT
DT 2021-04-09

Thanks Dmitry. I have done this, that's how I know what the error is and other variables. Anyway, we'll see. Thanks!

DZ
DZ 2021-04-09
<pre __jive_macro_name="quote" class="jive_macro_quote jive_text_macro"><p>Drew Drew wrote:</p><p></p><p>This script runs, the task exits, and the task manager says it executed successfully. But it doesn't save the open drawing as a pdf. It opens it, it does all the pomp and circumstance, but when it runs the Extension.SaveAs command nothing actually happens. I tried changing visibility from False to True, neither helps. Using ModelDoc2.SaveAs3 works but is antiquated and doesn't appear to be able to force silent saving (so the pdf's open after being saved). Any thoughts? Thanks!</p><p style="min-height: 8pt; padding: 0px;"></p>
  1. OptionExplicit
  2. Submain()
  3. DimswAppAsSldWorks.SldWorks
  4. DimswModelAsSldWorks.ModelDoc2
  5. DimswCustPrpMgrAsSldWorks.CustomPropertyManager
  6. DimlongstatusAsLong
  7. DimlongwarningsAsLong
  8. DimsFileNameAsString
  9. DimPathAsString
  10. DimFileNameAsString
  11. DimValueAsString
  12. DimDwgRevAsString
  13. DimSuccessAsObject
  14. DimswExtensionAsSldWorks.ModelDocExtension
  15. DimErrorsAsLong
  16. DimWarningsAsLong
  17. DimvSheetNameAsVariant
  18. DimswExportPDFDataAsSldWorks.ExportPdfData
  19. OnErrorResumeNext
  20. SetswApp=Application.SldWorks
  21. swApp.Visible=True
  22. sFileName=""
  23. SetswModel=swApp.OpenDoc6(sFileName,swDocDRAWING,swOpenDocOptions_Silent,"",longstatus,longwarnings)
  24. SetswModel=swApp.ActiveDoc
  25. swModel.ViewZoomtofit2
  26. DwgRev=swModel.GetCustomInfoValue("","Revision")
  27. IfDwgRev="?"OrDwgRev=""OrDwgRev="-"Then
  28. DwgRev="--"
  29. EndIf
  30. vSheetName=swModel.GetSheetNames
  31. SetswExportPDFData=swApp.GetExportFileData(1)
  32. FileName=Left(swModel.GetPathName,Len(swModel.GetPathName)-7)&"_"&DwgRev&".PDF"
  33. 'swModel.SaveAs3""&FileName&"",0,0
  34. swExtension=swModel.Extensiona
  35. Success=swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets,vSheetName)
  36. swExportPDFData.ViewPdfAfterSaving=True
  37. 'Success=swExtension.SaveAs(FileName,swSaveAsCurrentVersion,swSaveAsOptions_Silent,swExportPDFData,Errors,Warnings)
  38. Success=swExtension.SaveAs(FileName,0,0,swExportPDFData,Errors,Warnings)
  39. swApp.QuitDocswModel.GetTitle
  40. SetswModel=Nothing
  41. EndSub
<pre __jive_macro_name="quote" class="jive_macro_quote jive_text_macro"><pre class="plain" style="display: none;">Option Explicit Sub main() Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swCustPrpMgr As SldWorks.CustomPropertyManager Dim longstatus As Long Dim longwarnings As Long Dim sFileName As String Dim Path As String Dim FileName As String Dim Value As String Dim DwgRev As String Dim Success As Object Dim swExtension As SldWorks.ModelDocExtension Dim Errors As Long Dim Warnings As Long Dim vSheetName As Variant Dim swExportPDFData As SldWorks.ExportPdfData  On Error Resume Next  Set swApp = Application.SldWorks swApp.Visible = True  sFileName = "<Filepath>"   Set swModel = swApp.OpenDoc6(sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)  Set swModel = swApp.ActiveDoc    swModel.ViewZoomtofit2    DwgRev = swModel.GetCustomInfoValue("", "Revision")    If DwgRev = "?" Or DwgRev = "" Or DwgRev = "-" Then  DwgRev = "--"  End If   vSheetName = swModel.GetSheetNames  Set swExportPDFData = swApp.GetExportFileData(1)   FileName = Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "_" & DwgRev & ".PDF"  'swModel.SaveAs3 "" & FileName & "", 0, 0  swExtension = swModel.Extensiona  Success = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, vSheetName)  swExportPDFData.ViewPdfAfterSaving = True  'Success = swExtension.SaveAs(FileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, Errors, Warnings)  Success = swExtension.SaveAs(FileName, 0, 0, swExportPDFData, Errors, Warnings)  swApp.QuitDoc swModel.GetTitle  Set swModel = Nothing  End Sub

Drew,

Place a "stop" at the beginning of the sub and run it step by step to see where the task craps out on you. Probably before "OnErrorResumeNext". You can highlight the variables and see if the tooltip shows any variable values (note that the task add-in debugger isn't very powerful, but you can at least see if your variables are getting populated).

AJ
AJ 2021-04-09

apisupport@solidworks.com

DT
DT 2021-04-09

Interesting. The link you provide talks of not being able to overwrite a file, but when I run the task it doesn't even generate a file! Alas.

AJ
AJ 2021-04-09
DT
DT 2021-04-09

I tried running the script in Solidworks and it works fine. It doesn't work in the PDM task. That's something!

DT
DT 2021-04-09

I tried running the script in Solidworks and it works fine. It doesn't work in the PDM task. That's something!

DT
DT 2021-04-09

We are on 2017 task add-in. Just checked.

DT
DT 2021-04-09

Yeah. And when I use SaveAs3 or SaveAs4 it saves and shows up as a local file in the vault, then the vault auto-adds it as vaulted. But this newer method isn't working at all...

AJ
AJ 2021-04-09

Interesting. Did you refresh the save folder after executing the task?

DT
DT 2021-04-09

Thanks for the info on 4.

I have that info PDF and am willing to do that but this code is much simpler and works just fine, I just need to find a way to save the pdfs with swModel.SaveAs4 or something else that actually works but still doesn't open the pdf after saving!

KW
KW 2021-04-09

4. It's recommended to upgrade AddIns after upgrade, let look for instruction how to do that: 2018 SOLIDWORKS PDM Help - Performing the SWTaskAddin Upgrade (For SOLIDWORKS PDM Professional Only)

Oh, so you just want to add revision in the PDF Output File Name, yes?

Then let's a look for S-054527 in Knowledge Base, you can find there instruction step by step how to modify default convert task to add variable (like revision) into output file name.

Let me know if it is what you want!

BR,

Kamil

comment imagecomment image
DT
DT 2021-04-09

Currently my settings are as below. I should be seeing the PDF. Other local files show up...

comment imagecomment image
AJ
AJ 2021-04-09

Ok. So it saved correctly. The problem I reckon that it is saved as a local file. Eventually you will need to add it to the vault. Can you make sure your vault user's settings are set to view local files?

DT
DT 2021-04-09

Hey Kamil, thanks.

1. Script. The default doesn't consistently add the revision to the pdf file name.

2. More than one. Running the script as SW 2015.

3. 2017

4. Not sure. I'll have to look into this. You have to upgrade it separately?

5. No warnings pop up in SW.

6. I will look but it is saving the file to the same path as the drawing based on the FileName variable.

Thanks again!

KW
KW 2021-04-09

Hi Drew!

A few questions to help find a clue what is wrong:

1. Are you just using default PDM convertion task or are you writing your script to convert drawings on PDFs? The script which you upload doesn't look like default convertion task so may let's try to use them?

2. Do you have only one SW installation or more? Like SW 2019 and SW 2017? If yes, just check if

3. What PDM version are you using?

4. If you were upgrading PDM vault, did you upgrade SWTaskAddin too?

5. If you are openning a drawing which you want to convert, the some warnings are poping up in SW? If yes, just try to remove them to won't show next time.

6. May PDF is created on local disc but it's not added to vault? Could you check if there is no PDF locally?

Let me know your observation!

Best Regards,

Kamil Wilkosz

DT
DT 2021-04-09

Thanks Amen,

FileName = "C:\Vault\Data\P001\1001\MODELS\DWG\Z000.PDF"

Errors = 0

Warnings = 0