Print to pdf from an excel

Print to pdf from an excel

shuaib_cad
Advocate Advocate
1,434 Views
3 Replies
Message 1 of 4

Print to pdf from an excel

shuaib_cad
Advocate
Advocate

I have a list of drawing numbers which I have placed in an excel with their location intact. I want to convert all of them into PDFs through VBA excel macro.

Currently I am using the below code but it isn't working for me:

 

Sub ConvertToPDF()

Dim oWkbk As Workbook
Set oWkbk = ThisWorkbook
Dim oSheet As Worksheet
Set oSheet = oWkbk.ActiveSheet

sFilepathRange = "O2:O50"

Dim oAcadApp As Object
Dim oAcadDwg As AcadDocument

Set oAcadApp = CreateObject("Autocad.Application")

oAcadApp.Visible = True

Dim sFilePath As String

'oAcadApp.Documents.Close
For Each oCell In oSheet.Range(sFilepathRange)

If oCell <> "\.dwg" Then
Set oAcadDwg = oAcadApp.Documents.Open(oCell, True)

'oAcadDwg.ActiveLayout.UseStandardScale = True
'oAcadDwg.ActiveLayout.StandardScale = acScaleToFit
'oAcadDwg.ActiveLayout.PlotRotation = PlotRotation
'oAcadDwg.ActiveLayout.StyleSheet = "monochrome.ctb"
'oAcadDwg.ActiveLayout.ConfigName = "PDF-XChange 4.0 Lite"
'oAcadDwg.ActiveLayout.CanonicalMediaName = "Letter"
'oAcadDwg.ActiveLayout.PlotType = acExtents
'oAcadDwg.Plot.PlotToDevice

End If

oAcadApp.Documents.Close

Next

oAcadApp.Quit
Set oAcadApp = Nothing

MsgBox "Converted to PDF"

End Sub

 

Please help me with the correct code.

 

 

Regards,

Mohammed Shuaib

0 Likes
Accepted solutions (1)
1,435 Views
3 Replies
Replies (3)
Message 2 of 4

grobnik
Collaborator
Collaborator

Hi @shuaib_cad could you explain where your code hangs ? or what's wrong for your application ?

Seems you are opening the dwg but not plot due to  you have commented the plot command.

0 Likes
Message 3 of 4

shuaib_cad
Advocate
Advocate

I need code to print the drawings into pdf. The existing code which I have commented is not working for me as I get error in the line:

End If

oAcadApp.Documents.Close

Next

 

and its not moving to the next drawings.

0 Likes
Message 4 of 4

grobnik
Collaborator
Collaborator
Accepted solution

Hi @shuaib_cad , as first step I suggest to run the procedure step by step, check all the variable, check if drawing will be opend, check the filename.. and so on.

As second issue fix the following row

Set oAcadDwg = oAcadApp.Documents.Open(oCell, True)

With

Set oAcadDwg = oAcadApp.Documents.Open(oCell.value, True)

If you wirte "Ocell" only, instead "oCell.Value", perhaps will be not passed the cell value only with file name and or path, but all cell properties content in the oCell object.

Let us know in case share the excel file, and at least one or two dwg for test.

Thank you.

0 Likes