Turns out I had to add a sleep into the code to get it to wait for the printer software to kick in.
The following code will print all sheets in an Inventor drawing file to pdf. Each sheet is a separate named file in a pre-assigned location.
I plan to make this into an add-in so that I can install on as many machines as I like.
Watch this space for the updated file.
In the meantime, here's the code: -
{code}Option Explicit
Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Public tmpstr As String
Public revisions(8, 4) As String ' 10 is the maximum number of revisions the border can have
' Add a reference to PDFCreator
Public PDFCreator1 As PDFCreator.clsPDFCreator
Public ReadyState As Boolean
Public DefaultPrinter As String
Public Sub PlotPdf()
Dim killit
Dim numsheets As Integer
numsheets = 0
If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
Set PDFCreator1 = New clsPDFCreator
With PDFCreator1
If .cStart("/NoProcessingAtStartup") = False Then
' CommandButton1.Enabled = False
killit = Shell("taskkill /f /im PDFCreator.exe", VbAppWinStyle.vbHide)
MsgBox ("There was an error starting the pdf printer, please try (click) again!")
Debug.Print "Can't initialize PDFCreator."
Exit Sub
End If
End With
Debug.Print "PDFCreator initialized."
Dim oDrgDoc As DrawingDocument
Set oDrgDoc = ThisApplication.ActiveDocument
' Set reference to drawing print manager
Dim oDrgPrintMgr As DrawingPrintManager
Set oDrgPrintMgr = oDrgDoc.PrintManager
' Set the printer name
oDrgPrintMgr.Printer = "PDFCreator"
Dim shts As sheets
Dim sht As sheet
Dim outName As String
Dim i As Integer
Dim j As Integer
Dim Latestrev As Integer
Dim sheetsize As PaperSizeEnum
sheetsize = kPaperSizeA0
sheetsize = kPaperSizeA1
' shts = oDrgDoc.sheets
For Each sht In oDrgDoc.sheets
sht.Activate
'Set the paper size , scale and orientation
oDrgPrintMgr.ScaleMode = kPrintFullScale ' kPrintBestFitScale
' Change the paper size to a custom size. The units are in centimeters.
Dim shtsize As Long
shtsize = sht.Size
oDrgPrintMgr.PaperSize = kPaperSizeCustom
If shtsize = 9993 Then ' A0
oDrgPrintMgr.PaperHeight = 84.1
oDrgPrintMgr.PaperWidth = 118.9
ElseIf shtsize = 9994 Then ' A1
oDrgPrintMgr.PaperHeight = 59.4
oDrgPrintMgr.PaperWidth = 84.1
ElseIf shtsize = 9995 Then ' A2
oDrgPrintMgr.PaperHeight = 42
oDrgPrintMgr.PaperWidth = 59.4
ElseIf shtsize = 9996 Then ' A3
oDrgPrintMgr.PaperHeight = 29.7
oDrgPrintMgr.PaperWidth = 42
End If
oDrgPrintMgr.PrintRange = kPrintCurrentSheet
oDrgPrintMgr.Orientation = kLandscapeOrientation
oDrgPrintMgr.AllColorsAsBlack = False
oDrgPrintMgr.Rotate90Degrees = True
Latestrev = RetrieveRev
outName = RetrievePE("
", sht) & " REV " & Latestrev & ".pdf"
With PDFCreator1
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = "\\bas059\Aliquot\pdfs\"
.cOption("AutosaveFilename") = outName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
oDrgPrintMgr.SubmitPrint
Do Until PDFCreator1.cCountOfPrintjobs = 1
DoEvents
Sleep 1000
Loop
Sleep 1000
PDFCreator1.cPrinterStop = False
For i = 1 To 8
For j = 1 To 4
revisions(i, j) = ""
Next j
Next i
numsheets = numsheets + 1
Next
Else
MsgBox ("You aren't using an Inventor drawing!")
Exit Sub
End If
MsgBox ("Done Printing " & numsheets & " sheets!")
PDFCreator1.cClose
killit = Shell("taskkill /f /im PDFCreator.exe", VbAppWinStyle.vbHide)
End Sub
Public Function Setsheetsize(shtsize As PaperSizeEnum) As PaperSizeEnum
If shtsize = 9993 Then
Setsheetsize = kPaperSizeA0
ElseIf shtsize = 9994 Then
Setsheetsize = kPaperSizeA1
ElseIf shtsize = 9995 Then
Setsheetsize = kPaperSizeA2
ElseIf shtsize = 9996 Then
Setsheetsize = kPaperSizeA3
End If
End Function
Public Function RetrievePE(searchstring As String, oSheet As sheet) As String
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Dim oSheet As sheet
' Set oSheet = oDrawDoc.ActiveSheet
' Get the prompted text value from the title block.
' This is done by first getting the text box in the title
' block definition that defines the prompted text. Then
' you can use this to get the value specified for this
' particular title block instance.
Dim oBorderDef As BorderDefinition
Set oBorderDef = oSheet.Border.Definition
Dim oTextBox As TextBox
Dim bFound As Boolean
bFound = False
For Each oTextBox In oBorderDef.Sketch.TextBoxes
If GetPromptField(oTextBox.FormattedText) = searchstring Then
bFound = True
Exit For
End If
Next
If bFound Then
' oSheet.Name = oSheet.Border.GetResultText(oTextBox)
RetrievePE = oSheet.Border.GetResultText(oTextBox)
Else
MsgBox "Specified formatted text was not found in the title block."
End If
End Function
Public Function RetrieveRev() As Integer ' will only work whilst the revision is numeric!
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As sheet
Set oSheet = oDrawDoc.ActiveSheet
' Get all the (prompted) revision values from the title block.
' and add them to an array so we can sort them.
' Debug.Print "Retrieving revision from " & oSheet.Name
Dim oBorderDef As BorderDefinition
Set oBorderDef = oSheet.Border.Definition
Dim oTextBox As TextBox
Dim bFound As Boolean
bFound = False
Dim Revision As String
Dim cnt As Integer
Dim i As Integer
Dim j As Integer
i = 1
cnt = 0
For Each oTextBox In oBorderDef.Sketch.TextBoxes
Revision = GetPromptField(oTextBox.FormattedText)
If Revision Like "*REV*" Then
If Revision Like "*REV*Change*" Or Revision Like "*REV*CHANGE*" Then
revisions(i, 1) = oSheet.Border.GetResultText(oTextBox) ' Change
cnt = cnt + 1
ElseIf Revision Like "*REV*Date*" Or Revision Like "*REV*DATE*" Then
revisions(i, 3) = oSheet.Border.GetResultText(oTextBox) ' Date
cnt = cnt + 1
ElseIf Revision Like "*REV*" And Len(Revision) < 13 Then
revisions(i, 2) = oSheet.Border.GetResultText(oTextBox) ' Rev
cnt = cnt + 1
End If
revisions(i, 4) = oSheet.Name
If cnt = 3 Then
cnt = 0
i = i + 1
End If
End If
Next
For i = LBound(revisions) To UBound(revisions)
If revisions(i, 1) <> "" Then
' Debug.Print revisions(i, 1) & "|" & revisions(i, 2) & "|" & revisions(i, 3)
End If
Next i
Bubblesort
For i = LBound(revisions) To UBound(revisions)
If revisions(i, 1) <> "" Then
' Debug.Print revisions(i, 1) & "|" & revisions(i, 2) & "|" & revisions(i, 3)
End If
Next i
For i = LBound(revisions) To UBound(revisions)
If revisions(i, 2) <> "" Then
RetrieveRev = revisions(i, 2)
If revisions(i + 1, 2) = "" Then ' we reached the highest revision.
Exit For
End If
End If
Next i
End Function
' Get the text value of the prompted text. It extracts this from the' formatted text. If there's a failure then an empty string is =returned.
Private Function GetPromptField(ByVal FormattedText As String) As String
On Error GoTo ErrorFound
' Verify that this is a prompt field.
If Left$(FormattedText, 7) <> "
GetPromptField = ""
Exit Function
End If
' Get the text that is to the right of the first ">" symbol
' and to the left of the last "<" symbol.
' Debug.Print FormattedText
GetPromptField = Right$(FormattedText, Len(FormattedText) - InStr(FormattedText, ">"))
GetPromptField = Left$(GetPromptField, InStr(GetPromptField, "<") - 1)
' Replace any < or > with < and > symbols.
GetPromptField = Replace(GetPromptField, "<", "<")
GetPromptField = Replace(GetPromptField, ">", ">")
Exit Function
ErrorFound: GetPromptField = ""
End Function
Public Sub Bubblesort()
Dim i As Integer
Dim j As Integer
Dim temp As String
Dim iOuter As Long
Dim iInner As Long
Dim iLbound As Long
Dim iUbound As Long
Dim iTemp As String
iLbound = LBound(revisions)
For i = iLbound To UBound(revisions) ' - 1
If revisions(i, 2) <> "" Then
iUbound = i
End If
Next i
For iOuter = iLbound To iUbound ' - 1
'Which comparison
For iInner = iLbound To iUbound - iOuter - 1
'Compare this item to the next item
If revisions(iInner, 2) <> "" Then ' Continue
' Debug.Print "About to sort " & revisions(iInner, 4)
If CInt(revisions(iInner, 2)) > CInt(revisions(iInner + 1, 2)) Then
'Swap
iTemp = revisions(iInner, 1)
revisions(iInner, 1) = revisions(iInner + 1, 1)
revisions(iInner + 1, 1) = iTemp
iTemp = revisions(iInner, 2)
revisions(iInner, 2) = revisions(iInner + 1, 2)
revisions(iInner + 1, 2) = iTemp
iTemp = revisions(iInner, 3)
revisions(iInner, 3) = revisions(iInner + 1, 3)
revisions(iInner + 1, 3) = iTemp
iTemp = revisions(iInner, 4)
revisions(iInner, 4) = revisions(iInner + 1, 4)
revisions(iInner + 1, 4) = iTemp
End If
End If
Next iInner
Next iOuter
' MsgBox ("Done Sorting!")
End Sub
Private Sub PrintPage(PageNumber As Integer)
Dim cPages As Long
cPages = Selection.Information(wdNumberOfPagesInDocument)
If PageNumber > cPages Then
MsgBox "This document has only " & cPages & " pages!", vbExclamation
End If
DoEvents
ActiveDocument.PrintOut Background:=False, Range:=wdPrintFromTo, From:=CStr(PageNumber), To:=CStr(PageNumber)
DoEvents
End Sub
Private Sub PDFCreator1_eError()
AddStatus "ERROR [" & PDFCreator1.cErrorDetail("Number") & "]: " & PDFCreator1.cErrorDetail("Description")
End Sub
Private Sub PDFCreator1_eReady()
AddStatus "File'" & PDFCreator1.cOutputFilename & "' was saved."
PDFCreator1.cPrinterStop = True
' CommandButton1.Enabled = True
End Sub
Private Sub AddStatus(Str1 As String)
Debug.Print vbCrLf & Now & ": " & Str1
End Sub
{code}