Export DWG to PDF via VBA (without SendCommand and PlotToFile)

Export DWG to PDF via VBA (without SendCommand and PlotToFile)

Anonymous
Not applicable
8,714 Views
8 Replies
Message 1 of 9

Export DWG to PDF via VBA (without SendCommand and PlotToFile)

Anonymous
Not applicable

Hi everyone,


I didn't find my answer on the web by myself so I started a topic here, sorry if it already exists one.


I want to convert a DWG in PDF via a VBA code. Simple, right ? I already tried two solutions :

 - PlotToFile, it works but it takes 25 sec. for each drawing.

 - SendCommand + Export, it looks great when I do it manually on AutoCad but it doesn't work via VBA.

 

Does anyone know a third solution ?


I'm using AutoCad 2012.

 

0 Likes
8,715 Views
8 Replies
Replies (8)
Message 2 of 9

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> PlotToFile, it works but it takes 25 sec. for each drawing

It depends on file content, on amount of text (and how it is handled) and the settings in "DWG to PDF.PC3" (or whatever driver you use), can you show a drawing and it's settings (run _ETRANSMIT so we get your PC3 and PMP if there is one assigned).

BTW: what time do you expect?

 

>> when I do it manually on AutoCad but it doesn't work via VBA

What means "does not work"? Crash? Error message? Incorrect PDF content? Anything else ...?

And what code "does not work", I guess you have to show it so we can look into it and try it on our systems.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 9

Anonymous
Not applicable

@Alfred.NESWADBA wrote:

Hi,

 

>> PlotToFile, it works but it takes 25 sec. for each drawing

It depends on file content, on amount of text (and how it is handled) and the settings in "DWG to PDF.PC3" (or whatever driver you use), can you show a drawing and it's settings (run _ETRANSMIT so we get your PC3 and PMP if there is one assigned).

BTW: what time do you expect?

 

>> when I do it manually on AutoCad but it doesn't work via VBA

What means "does not work"? Crash? Error message? Incorrect PDF content? Anything else ...?

And what code "does not work", I guess you have to show it so we can look into it and try it on our systems.

 

- alfred -


 

Hi Alfred,

 

Sorry if I left not enough informations about my issue.

 

- About the PlotToFile method duration, I didn't expect too much but if every of my 100 drawing takes so long it will take 25 minutes to have a PDF. I know that I can make a Publish with every of them to earn a lot of time but the point is to make PDF from various documents (including DWG) from a list at the same time so I must use something that plot each DWG separately to keep the order. Furthermore, the export method is almost immediate so I'm a bit frustrated by the 25 sec. of the PlotToFile method. Ah and I tried with a DWG with only 4 blocs inserted, it took 25 sec too.
I use "DWG to PDF.PC3" indeed, here is the code I have :

 

Sub DWGtoPDF(dwg As AcadDocument, Ligne As String)

Dim PtConfigs As AcadPlotConfigurations
Dim PlotConfig As AcadPlotConfiguration
Dim PtObj As AcadPlot
Dim BackPlot As Variant
Dim NomComplet As String
Dim NomPDFenDWG As String


'Create a new plot configuration with all needed parameters
Set PtObj = dwg.Plot
Set PtConfigs = dwg.PlotConfigurations

'Add a new plot configuration
PtConfigs.Add "PDF", False

'The plot config you created become active
Set PlotConfig = PtConfigs.Item("PDF")

'Use this method to set the scale
PlotConfig.ConfigName = "DWG To PDF.pc3" ' "PDFCreator" ne fonctionne pas
PlotConfig.UseStandardScale = False
PlotConfig.StandardScale = acScaleToFit
'PlotConfig.ModelType = True
PlotConfig.PlotRotation = ac0degrees
PlotConfig.PlotType = acExtents

'Updates the plot
PlotConfig.RefreshPlotDeviceInfo

PlotConfig.CanonicalMediaName = "ISO_expand_A3_(297.00_x_420.00_MM)"
PlotConfig.CenterPlot = True

'Specifies whether or not to plot using the plot styles
PlotConfig.PlotWithPlotStyles = False

BackPlot = dwg.GetVariable("BACKGROUNDPLOT")
dwg.SetVariable "BACKGROUNDPLOT", 1

'Updates the plot
PlotConfig.RefreshPlotDeviceInfo

'Now you can use the PlotTofile method
NomPDFenDWG = Replace(dwg.FullName, Left(dwg.name, Len(dwg.name) - 4), Ligne)

a = PtObj.PlotToFile(Replace(NomPDFenDWG, "dwg", "pdf"), PlotConfig.ConfigName)

' Faire patienter le code 25 secondes le temps que le PDF soit créé (eh oui avec ce code ça prend 25 secondes)
Application.Wait Now + TimeValue("0:0:25")

'If you wish you can delete the plot configuration you created
'programmatically, and set the 'BACKGROUNDPLOT' system variable
'to its original status.
PtConfigs.Item("PDF").Delete
Set PlotConfig = Nothing
dwg.SetVariable "BACKGROUNDPLOT", BackPlot

dwg.Close

End Sub

 

 

I could get along with the 25 sec. but the fact is that this code's result is not the same as the manually performed Plot : manual PDF is centered and VBA's PDF is larger and thus go a little outside of the paper. I tried to move every setting int he code above but nothing changed. I even changed PlotRotation to see what would happen and the result was the same with every angle ! (strange isn't it ?)

0 Likes
Message 4 of 9

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> but the point is to make PDF from various documents (including DWG) from a list at the same time so

You can use _PUBLISH with multiple drawings/multiple layouts into a single PDF

 

>> but the fact is that this code's result is not the same as the manually performed Plot

You are assigning a new plotter, why not using the plot-config already assigned to the layout?

To see your differences and to be able to play with that we would need a drawing + your pc3/pmp and 2 pdf's, one created manually and the other created by code so we see what your differences are. We would have to avoid to try 20 times to plot to maybe have not any difference and so don't come more close to your result.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 9

Anonymous
Not applicable

@Alfred.NESWADBA wrote:

Hi,

 

>> PlotToFile, it works but it takes 25 sec. for each drawing

It depends on file content, on amount of text (and how it is handled) and the settings in "DWG to PDF.PC3" (or whatever driver you use), can you show a drawing and it's settings (run _ETRANSMIT so we get your PC3 and PMP if there is one assigned).

BTW: what time do you expect?

 

>> when I do it manually on AutoCad but it doesn't work via VBA

What means "does not work"? Crash? Error message? Incorrect PDF content? Anything else ...?

And what code "does not work", I guess you have to show it so we can look into it and try it on our systems.

 

- alfred -


And about the SendCommand + Export, here is the code :

 

Sub ExportPDF()

Dim varFile As String
Dim AcadApp As AcadApplication
Set AcadApp = GetObject(, "AutoCAD.Application")

AcadApp.ActiveDocument.Activate
a = AcadApp.ActiveDocument.Export("yoExportCommande", ".pdf")
'AcadApp.ActiveDocument.SendCommand ("filedia 0 ")
varFile = "T:\_USERS\ECOR\ProjetMacroPriorité1\DossierTest\vba"

' SendCommand
AcadApp.ActiveDocument.SendCommand ("-EXPORT" & vbCr & "PDF" & vbCr & "Extents" & vbCr & "Yes" & vbCr & TaillePapier & vbCr & _
"Millimeters" & vbCr & "Landscape" & vbCr & "Fit" & vbCr & "Yes" & vbCr & "monochrome.ctb" & vbCr & "Yes" & vbCr & varFile & vbCr)

' Paramètres après le <yes> :
' Enter Paper size (ex = <ANSI A (8.50 x11.00 Inches> )
' Enter Paper units [Inches/Millimeters]
' Enter Drawing orientation [Portrait/Landscape]
' Enter PlotScale (vaut mieux mettre <Fit> )
' Plot with plot styles ? [Yes/No]
' Enter plot style table name (à mon avis il est facultatif si tu dis de pas prendre de plot.style)
' (un exemple : <monochrome.ctb> )
' Plot with lineweights [Yes/No]
' Enter file name

End Sub


The issue is that on AutoCAD, when I run this code, I see that it appears on the "command status", it appears right, every setting fits right ... but the Export command is not performed by AutoCad.

 

When I try manually to run the Export command on AutoCAD directly (when I write the command and fill the settings myself), the Export command is well performed and a PDF appears.

 

Voilà, here are my issues, congrats if you managed all of my stuff and thanks a lot to spend time for this !

0 Likes
Message 6 of 9

Anonymous
Not applicable

@Alfred.NESWADBA wrote:

 

You can use _PUBLISH with multiple drawings/multiple layouts into a single PDF

 


Alas, I also have have Word and Excel documents to convert in PDF and their order in the list is important so I cannot use PUBLISH only for the DWG and then treat the others.

 


@Alfred.NESWADBA wrote:

 

To see your differences and to be able to play with that we would need a drawing + your pc3/pmp and 2 pdf's, one created manually and the other created by code so we see what your differences are.


Here they are !

 

 

0 Likes
Message 7 of 9

Anonymous
Not applicable

And the DWG.

0 Likes
Message 8 of 9

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

a bit short in time, but have you seen that these two pdf's are not rotated to the same direction, they differ by 180°, try to set that angle in your code.

 

- alfred -

PS: within the ZIP file I'm missing the PC3, the drawings are set to plot to a LaserJet 5si, not to create pdf, so whatever I would try I would have to set my own PC3's and that might differ to your PC3's for PDF

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 9 of 9

Anonymous
Not applicable

just in case you shouldn't make it the way you're struggling for, maybe a workaround by means of PDF APIs could help you.

 

I mean you can print and put all your needed pdfs (from word, excel, autocad...) in a certain folder and with proper names (accounting for their final publishing order) and then manage them (choosing proper files and or pages order) with a VBA macro that use PFD APIs which chooses files and it's pages order to create the final pdf dcoument

 

I only know that those APIs are out there and babbled with them with the following basic example (but it makes you glimpse its potenitalities)

 

Sub Button1_Click()

    Dim AcroApp As Acrobat.CAcroApp

    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc

    Dim numPages As Integer

    Set AcroApp = CreateObject("AcroExch.App")

    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")

    Part1Document.Open ("C:\Users\chojwa\Desktop\a\UT\VARIE\software\VBA programming\PDF\Stampa Automatica\DM 14-01-08 - tabella azioni variabili.pdf")
    Part2Document.Open ("C:\Users\chojwa\Desktop\a\UT\VARIE\software\VBA programming\PDF\Stampa Automatica\DT.OC.ST.0054221.08.U.pdf")

    ' Insert the pages of Part2 after the end of Part1
    numPages = Part1Document.GetNumPages()

    If Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = False Then
        MsgBox "Cannot insert pages"
    End If

    If Part1Document.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
        MsgBox "Cannot save the modified document"
    End If

    Part1Document.Close
    Part2Document.Close

    AcroApp.Exit
    Set Part1Document = Nothing
    Set Part2Document = Nothing
    Set AcroApp = Nothing

    MsgBox "Done"


End Sub

 

for which I needed a reference to "Adobe Acrobat 10.0 Type Library" (some "AcroRd32.dll" in "C:\Program Files (x86)\Adobe\Reader 11.0" folder. but it depends on Acrobat installation)

 

in web you should be able to find those API's programming tools  (some "iac_api_reference.pdf") and developer's guide (some "iac_developer_guide.pdf")

 

If memory serves the full functionalities come with Adobe Acrobat installed, but Adobe Reader should work too, though with fewer tools.

 

0 Likes