Export to PDF with date, not working.

Export to PDF with date, not working.

Brian.Price
Advocate Advocate
1,215 Views
19 Replies
Message 1 of 20

Export to PDF with date, not working.

Brian.Price
Advocate
Advocate

I have a rule that exports to pdf and ad adds the date to the end of the file so I can keep track of changes. I have run into an issue when I export for the first time that day it overrides the previous date. But when I run the rule again it will update the date and export properly. Is there something in Inventor that needs to be updated before running the rule?

 

Most of this rule has been copy pasted from other rules on the forums to piece together what I need.

 

SyntaxEditor Code Snippet

'Get Date and Format
Dim Time As DateTime = DateTime.Now
Dim Format As String = "yyyy.M.d"

Dim oPropSet As PropertySet
oPropSet = _
ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

Try
	'try to get the iproperty value
	oTest = iProperties.Value("Custom", "DateTimeString") 
Catch
	'catch error when iproperty doesn't exist and create it
	oPropSet.Add("", "DateTimeString")
End Try

oDate = iProperties.Value("Custom", "DateTimeString") 

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("project", "revision number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 

'then set the property
iProperties.Value("Custom", "DateTimeString") = Time.ToString(Format)

If oPDFAddIn.HasSaveCopyAsOptions(oDocument, 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 


'get PDF target folder path
oFolder = ThisDoc.Path


'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & " - " & oDate & ".pdf" 


'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) 
'------end of iLogic-------



InventorVb.DocumentUpdate()

 

 

 Thanks for the help.

0 Likes
Accepted solutions (2)
1,216 Views
19 Replies
Replies (19)
Message 2 of 20

Charlies_3D_T
Advocate
Advocate
Accepted solution

@Brian.Price

 

Hello,

 

You just want to have the date and not the hour for example? 

 

So if you save that day multiple times you only have one PDF?

 

Is that correct?

0 Likes
Message 3 of 20

Brian.Price
Advocate
Advocate

Correct, I would like only one .pdf per day.

 

Didn't mean to click the accept solution, oops.

0 Likes
Message 4 of 20

Charlies_3D_T
Advocate
Advocate

@Brian.Price

 

Try this code: 

 

It gives you back the pdf with the date after in the same directory as your drawing. 

 

SyntaxEditor Code Snippet

'Get Date and Format
Dim Time As DateTime = DateTime.Now
Dim MyDate As String
MyDate = Format(Time, "dd.MM.yyyy")

ThisDrawing.Document.SaveAs(ThisDoc.FileName & "_" & MyDate & ".pdf", True)

InventorVb.DocumentUpdate()

 If you want you can change the date to yyyy.MM.dd also. 

 

Message 5 of 20

Brian.Price
Advocate
Advocate

The rule doesn't like the date format and it still won't update the date before saving the document. I have tried placing the update command in a couple other places in the code to see if that works but it hasn't yet.

0 Likes
Message 6 of 20

JamieVJohnson2
Collaborator
Collaborator

"yyyy.M.d" is what you are using "yyyy.MM.dd" is what you should be using.  Fix that and see if its happy.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 7 of 20

JamieVJohnson2
Collaborator
Collaborator

You can save as PDF without even using the translator?  That's interesting, I guess the translator method is only good if you want to change the options before saving, which most of us do.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 8 of 20

Brian.Price
Advocate
Advocate

I fixed that, however it has been adding the date correctly when it adds the date. However it still overwrites the old file before updating the date. The second time it's run the date is correct and it generates a new file.

0 Likes
Message 9 of 20

JamieVJohnson2
Collaborator
Collaborator

Ok so I'd like to step in and take a look at your current code revision (please post), and give me a full rundown of your intended function.  It appears that you are saving the file on top of any existing file (which may happen to have the same name if saved on the same date).  Is this behavior intended, or not wanted?  Do you want a PDF copy of the file with EACH save, or just Each save once a day (last one is kept), or something else?

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 10 of 20

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

Wait a second, I think I see your original posted problem in your code.  You are getting oDate from the property and setting that to the file name, then getting the current date, and setting that in the property.  Change up your code a bit.

get oDate from DateTime.Now first.

Then set to the property.value

Then use in file name.

Never GET from the property.value, because it doesn't look like you have any purpose for it, such as making a decision to NOT save the file because the dates match.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 11 of 20

Brian.Price
Advocate
Advocate

Got it working, thanks.

0 Likes
Message 12 of 20

Charlies_3D_T
Advocate
Advocate

@JamieVJohnson2

 

Can i ask you if your remark is also a problem in my code? 

 

'Get Date and Format
Dim Time As DateTime = DateTime.Now
Dim MyDate As String
MyDate = Format(Time, "dd.MM.yyyy")

ThisDrawing.Document.SaveAs(ThisDoc.FileName & "_" & MyDate & ".pdf", True)

InventorVb.DocumentUpdate()

It's great if someon can point me to my mistakes! Thank you 

0 Likes
Message 13 of 20

Brian.Price
Advocate
Advocate
Dim Time As DateTime = DateTime.Now
Dim MyDate As String
MyDate = Format(Time, "dd.MM.yyyy")

It didn't work for me instead I used:

 

 SyntaxEditor Code Snippet

Dim Time As DateTime = DateTime.Now
Dim Format As String = "dd.MM.yyyy"

 Here is my complete code:

 

SyntaxEditor Code Snippet

Dim oPropSet As PropertySet
oPropSet = _
ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

Try
	'try to get the iproperty value
	oTest = iProperties.Value("Custom", "DateTimeString") 
Catch
	'catch error when iproperty doesn't exist and create it
	oPropSet.Add("", "DateTimeString")
End Try

oDate = iProperties.Value("Custom", "DateTimeString") 

InventorVb.DocumentUpdate()

'Get Date and Format
Dim Time As DateTime = DateTime.Now
Dim Format As String = "yyyy.MM.dd"

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("project", "revision number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 

'then set the property
iProperties.Value("Custom", "DateTimeString") = Time.ToString(Format)

If oPDFAddIn.HasSaveCopyAsOptions(oDocument, 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 
InventorVb.DocumentUpdate()

'get PDF target folder path
oFolder = ThisDoc.Path


'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If


 'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & " - " & iProperties.Value("Custom", "DateTimeString") & ".pdf" 


'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) 
'------end of iLogic-------

InventorVb.DocumentUpdate()
0 Likes
Message 14 of 20

JamieVJohnson2
Collaborator
Collaborator

@Charlies_3D_T I actually tested your code suggestion first.  It worked properly on my machine as is within the iLogic editor.  So great work! 

 

That lead me to dive deeper into @Brian.Price 's code issues to find the hidden problem, and attempt to study his question in more detail until I understood the actual issue.  Sometimes I reply to an issue with a partial solution or a guided solution, followed up by quick edits or new understanding as I continue to think about the problem to be solved.

 

I have no desire to 'collect' points for solutions, because they did not exist when this same organization assisted me in learning AutoCAD (VBA, VB.Net, Lisp), Revit (VB.Net), Inventor (VB.Net, iLogic), and Vault (VB.Net).  I am merely here giving back to the community that has given me so much during the past 24 years.  In fact I have approx. 4 accounts here with many solutions that the current account doesn't take into its calculator.

 

If there is any advice to be given, it would be this:

1.  Understand the problem from all angles.  The OP (original poster?) may be asking for one thing, but needing something else to get there.

2.  Supply understanding as well as code.  Don't just drop code down (that's very important to do so mind you), but also supply an explanation of the code's function.  Or perhaps the OP only needs an explanation of the code object structure.

3.  Don't be afraid to research for an answer (I've done this many times), it gains you new knowledge as well.

4.  Don't be afraid to be wrong, sometimes even when we are right it doesn't actually solve the problem due to unknown complications, we must strive to find other ways to solve the problem.

5.  Admit to someone when you confirm the issue can't be solved.  This helps the OP to verify the dead end exists, and to take steps down a different path sooner.

6.  Type a lot, its fun, and improves you coding skills, text messaging skills, grammar and the like (just for fun!).

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 15 of 20

Charlies_3D_T
Advocate
Advocate

@JamieVJohnson2

 

Thanks for the reply! I was not responding because i wanted my answer to be the correct one but to learn like you describe! iLogic is great but you need to search a lot so this is why i was answering in the first place because i'm programming for weeks now and start to understand it a bit!

 

Thanks for the reply and the tips! I will keep them in mind!

0 Likes
Message 16 of 20

JamieVJohnson2
Collaborator
Collaborator

btw the proper way to use Format command is THRU the String object:

String.Format("text value to be formatted")

https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.7.2

 

In short it will fail because Format is not the command by itself.

However when you set a variable to Format (as string) and gave it the date format, the datetime class understood what to do with that.  So you performed a perfectly usable work around.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 17 of 20

Brian.Price
Advocate
Advocate

Thanks for the info, I have a long ways to go before getting a good understanding on VBA. Haven't done coding in 15 years and didn't retain much from my programming classes back then.

0 Likes
Message 18 of 20

JamieVJohnson2
Collaborator
Collaborator

I can relate.  I learned coding the HARD WAY, lots of cussing and screaming.  When I took coding in class it was 1988 and was just BASIC.  BTW is sucked at it, so me being here is somewhat of a life long irony.  For both of you, don't give up on it, and ask any question you want (even privately).  I believe knowledge (generic, not stuff under an NDA) should be freely given.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 19 of 20

Charlies_3D_T
Advocate
Advocate

@JamieVJohnson2

 

Can i ask you how my code has to be to be perfect. 

 

I'm comparing the link you added but i don't understand what is wrong.

0 Likes
Message 20 of 20

JamieVJohnson2
Collaborator
Collaborator

First of all, working code is perfect.

But since you asked, your attempt to use:

MyDate = Format(Time, "dd.MM.yyyy")

that failed because Format() is not the command by itself, it requires access to the shared class String

MyDate = String.Format(Time, "dd.MM.yyyy")

since there are many other ways to handle that, such as the DateTime class, you could super simplify by stating

MyDate = DateTime.Now.ShortDate

https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.7.2

you can imagine how popular date and time is with current coders, so this structure has a lot of features and documentation.

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes