I started with the following sample code from the eDrawings API download samples for Excel:
Private Sub btn_Open_Click()
Dim sPath As String
sPath = ThisWorkbook.Path
Dim index As Long
index = InStrRev(sPath, "\\")
sPath = VBA.Left\\\$(sPath, index)
Me.EModelViewControl1.OpenDoc sPath + "Sample_Documents\\top_level.edrw", False, False, False, ""
End Sub
From this, I surmise that I can cycle through documents and print them as I go, so I modified the code to:
Dim x As Long
Dim DrwPath As String
Dim DrwName As String
Dim DrwFileName As String
DrwPath = "C:\\Drawing_Downloads\\"
DrwName = Dir(DrwPath & "*.slddrw")
Do While DrwName <> ""
DrwFileName = DrwPath & DrwName
Me.EModelViewControl1.OpenDoc DrwFileName, False, False, False, ""
eDrawings_SampleForm.Caption = "Preview of " & DrwName
Me.EModelViewControl1.SetPageSetupOptions 2, 17, 0, 0, 1, 7, "\\\\piwifp1\\Eng_5100", 0, 0, 0, 0
Me.EModelViewControl1.Print2 False, DrwFileName, False, False, False, 1
Me.EModelViewControl1.CloseActiveDoc ("")
DrwName = Dir
Loop
eDrawings_SampleForm.Hide
End
End Sub
This causes Excel to crash on some of my larger drawings when I run the program, but it works okay when I step through, so I begin to wonder if there is a second CPU core doing some of the printing, and the document is getting unloaded before the print is complete, so I added the following:
Me.EModelViewControl1.Print2 False, DrwFileName, False, False, False, 1
Excel.Application.Wait (Now() + 0.0001) 'wait about 10 seconds
Me.EModelViewControl1.CloseActiveDoc ("")
DrwName = Dir
Loop
eDrawings_SampleForm.Hide
End
End Sub
This does not really seem to address the issue, certainly not all the time; it still crashes if I run the program, but works if I step through again.
Next, I notice multi-sheet drawings are only printing the first sheet, so I add:
Dim x As Long
.
.
Me.EModelViewControl1.SetPageSetupOptions 2, 17, 0, 0, 1, 7, "\\\\piwifp1\\Eng_5100", 0, 0, 0, 0
For x = 1 To Me.EModelViewControl1.SheetCount
Me.EModelViewControl1.ShowSheet x
Me.EModelViewControl1.Refresh
Me.EModelViewControl1.Print2 False, DrwFileName, False, False, False, 1
Excel.Application.Wait (Now() + 0.0001)
Next x
Me.EModelViewControl1.CloseActiveDoc ("")
That seems to make only the last sheet print, so I can't get all sheets of a multi-sheet document (I get either first or last with these pieces of code), and all versions still crash Excel when ran but not when stepped.
The entire chunk of code I have now is:
Dim x As Long
Dim DrwPath As String
Dim DrwName As String
Dim DrwFileName As String
DrwPath = "C:\\Drawing_Downloads\\"
DrwName = Dir(DrwPath & "*.slddrw")
Do While DrwName <> ""
DrwFileName = DrwPath & DrwName
Me.EModelViewControl1.OpenDoc DrwFileName, False, False, False, ""
eDrawings_SampleForm.Caption = "Preview of " & DrwName
Me.EModelViewControl1.SetPageSetupOptions 2, 17, 0, 0, 1, 7, "\\\\piwifp1\\Eng_5100", 0, 0, 0, 0
For x = 1 To Me.EModelViewControl1.SheetCount
Me.EModelViewControl1.ShowSheet x
Me.EModelViewControl1.Refresh
Me.EModelViewControl1.Print2 False, DrwFileName, False, False, False, 1
Excel.Application.Wait (Now() + 0.0001) 'wait about 10 seconds
Next x
Me.EModelViewControl1.CloseActiveDoc ("")
DrwName = Dir
Loop
eDrawings_SampleForm.Hide
End
End Sub
Any ideas?
SolidworksEdrawings