iLogic error handling on export PDF - "File in use"

iLogic error handling on export PDF - "File in use"

Wowbagger2
Enthusiast Enthusiast
1,258 Views
5 Replies
Message 1 of 6

iLogic error handling on export PDF - "File in use"

Wowbagger2
Enthusiast
Enthusiast

Hi,

 

I found this script in another thread (https://forums.autodesk.com/t5/inventor-forum/ilogic-rule-quot-auto-export-pdf-on-save-quot/td-p/538...) to export/save a pdf from a drawing upon save. This works really good but I would like to add a error message if the file was unable to be saved/updated, most likely cause the PDF is open or in use somewhere. As of now the error that shows up is  "(Exception from HRESULT: 0x80004005 (E_FAIL))" I would like to simply add a MessageBox instead. Happy if anyone could help me out.

 

Here's the script:

' Get the PDF translator Add-In.
Dim PDFAddIn As TranslatorAddIn
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
'oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If

PDFDirectory = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension

If ThisDoc.Path =  "" Then
MessageBox.Show("PDF file not created, click Save button again for PDF creation.", "iLogic")
Return 'exit rule
Else
oDataMedium.FileName = PDFDirectory & "\" & oFileName & ".PDF"
End If

'Publish document.
PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
'Launch the PDF file in whatever application Windows is set to open this document type with
'i = MessageBox.Show("Preview the PDF file?", "PDF Preview",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
'If i = vbYes Then ThisDoc.Launch(oDataMedium.FileName)

On Error
MessageBox.Show("PDF not saved, file is in use. Please close the PDF-document and try again", "iLogic")

 

 

 Thanks

 

/Fredrik

 

0 Likes
Accepted solutions (1)
1,259 Views
5 Replies
Replies (5)
Message 2 of 6

ThomasB44
Mentor
Mentor

Hi @Wowbagger2

 

Before "publish document" with the "save copy as" command at the end of your code, you could add these lines :

 

SyntaxEditor Code Snippet

DocPDF = CreateObject("AcroExch.AVDoc")
'Check if PDF document exists
If System.IO.File.Exists(oDataMedium.FileName) Then
'Close it without saving
DocPDF.Open(oDataMedium.FileName,"...")
DocPDF.Close(True)
End If

 

Edit : I precise it will work only if the PDF document is open on your PC. If open by an other user...I think there is no way to solve this.


Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

Message 3 of 6

Wowbagger2
Enthusiast
Enthusiast

@ThomasB44

 

Thank you for that suggestion but I'm wondering, it seems to me pretty much the only error that can occur when trying to re save the PDF is that Inventor cannot access the file because it is already in use somewhere. Would it not be possible to have a MessageBox that pops up if the save failed for some (any!) reason?

 

Best,

Fredrik

0 Likes
Message 4 of 6

ThomasB44
Mentor
Mentor
Accepted solution

Hi @Wowbagger2


 

Would it not be possible to have a MessageBox that pops up if the save failed for some (any!) reason?

 


Sure it would be possible, you can try these lines at the end of your code.

'Publish document.
Try PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
'Fail to save, an error occurs
MessageBox.Show("PDF not saved, file is in use. Please close the PDF-document and try again", "iLogic")
End Try 

 


Thomas
Mechanical Designer / Inventor Professional 2025
Inventor Professional EESignature

0 Likes
Message 5 of 6

Curtis_Waguespack
Consultant
Consultant

@Anonymous wrote:
... Would it not be possible to have a MessageBox that pops up if the save failed for some (any!) reason?

 

 


Hi fredrik.carlsson,

 

Just change the last line to read as you see fit.

 

MessageBox.Show("PDF not saved.", "iLogic")

 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 6 of 6

Wowbagger2
Enthusiast
Enthusiast

Thank you all. I think this solution will be sufficient!

0 Likes