Vba for saving PDF file

Vba for saving PDF file

stocjon25
Observer Observer
871 Views
9 Replies
Message 1 of 10

Vba for saving PDF file

stocjon25
Observer
Observer

Hi Folks, first off I am not a code guy so please go easy😂

 

With my small knowlegde I assembled the below code, I got parts from other codes and with my little knowledge I coud get till here i.e. the problem starts on the following line. = > Doc.PLOT.PlotToFile (pdffile)

 at this point a window pops-up to indicate a place and the name of the file, so I want to include in my code the lines for indicate the path and the file name without the system opening this window. Do you know how to do that?

 

Sub PrintTOPdf()

Dim App As Object
Dim Doc As Object
Dim pdffile As String
Dim result As String
Dim cadfile As String
Dim papersize As String
Dim backPlot As Integer
Dim FldrPicker As FileDialog
Dim Caminho As String
Dim ExtDwg As String
Dim ExtPdf As String

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Selecione a pasta para imprimir os arquivos"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
Caminho = .SelectedItems(1) & "\"
End With

ExtDwg = "*.dwg"

ExtPdf = "*.pdf"
cadfile = Dir(Caminho & ExtDwg, vbNormal)

pdffile = Len(cadfile)
pdffile = Left(cadfile, pdffile - 4)
' pdffile = pdffile & ".pdf"
pdffile = Caminho & pdffile & ".pdf"

Set App = CreateObject("autoCad.Application")
App.Visible = False
Set Doc = App.Documents.Open(Caminho & cadfile)
Doc.ActiveLayout.ConfigName = "PDF-XChange Standard.pc3"
backPlot = Doc.GetVariable("BACKGROUNDPLOT")
Doc.SetVariable "BACKGROUNDPLOT", 0
Doc.SetVariable "filedia", 0

If cbtfile <> "" Then
Doc.ActiveLayout.StyleSheet = cbtfile
Else
Doc.ActiveLayout.StyleSheet = "monochrome.ctb"
End If

Doc.ActiveLayout.CanonicalMediaName = "A3"

Doc.PLOT.PlotToFile (pdffile)


Doc.SetVariable "BACKGROUNDPLOT", backPlot
Doc.Close False
App.Quit

MsgBox "All printed"



End Sub

 

Thank you very much for any help.

 

JonnyCaptura de tela 2026-02-02 171446.png

0 Likes
872 Views
9 Replies
Replies (9)
Message 2 of 10

Norman_Yuan
Mentor
Mentor

Firstly, please post your code by using the "</>" button in the toolbar of the message window, so that the code's format is kept and easy to read.

 

As for the code, you may want to firstly give it a try to make this change:

 

from

Doc.PLOT.PlotToFile (pdffile)

to

Doc.PLOT.PlotToFile pdffile

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 10

Ed__Jobe
Mentor
Mentor

@Norman_Yuan suggestion will work. You only need to include parenthesis around the argument when the method is on the right side of and = sign.

 

I've been recommending the solution by @BlackBox_ . All you have to do is right click on a dwg in Windows Explorer and plot to pdf. I would use acCoreConsole.exe. See this post. Just change the script to do what you want.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 4 of 10

stocjon25
Observer
Observer

Hi  Norman,

 

thanks for replying I did what you asked me to do and I got the same result, not has changed.

The window still pops-up requesting file name and local for saving the file. 

 

Error2.png

 

 

Regards Jonny

0 Likes
Message 5 of 10

Norman_Yuan
Mentor
Mentor

Well, this code works OK inside AutoCAD (i.e. as AutoCAD VBA code):

Option Explicit

Sub PrintTOPdf()

    Dim backPlot As Integer
    Dim result As Boolean
    
    '' For simplicity, instead of creating an AcadPlogConfiguration object,
    '' make sure the current/active layout is configured to use a PDF
    '' plotting device, such as "Dwg To Pdf.pc3"
    
    backPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT")
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
    
    On Error Resume Next
    ThisDrawing.Plot.PlotToFile "D:\Temp\Plot Test.pdf"
    If Err.Number <> 0 Then
        MsgBox Err.Number & vbCrLf & Err.Description
    End If
    
    ThisDrawing.SetVariable "BACKGROUNDPLOT", backPlot

End Sub

 

So, it seems that your issue may be related to the fact that your code is not AutoCAD VBA code, which you did not specify, maybe from Excel?

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 10

stocjon25
Observer
Observer

Hi Norman,

 

yes you are right. The VBA Code is triggered from Excel.I thought it was not necessary to mention it.

I need this option from Excel because there are a lot of files to be printed and I wanna avoid to open AutoCAD to do that.

 

Regards,

 

Jonny

0 Likes
Message 7 of 10

Norman_Yuan
Mentor
Mentor

Well, it is completely misunderstanding that doing this task from Excel side could avoid open AutoCAD: a running AutoCAD instance is still required (evidenced by your code "CreateObject(...)". It is running invisibly because your code did not set its Visible property to true (but AutoCAD instance may still becomes visible for various reasons automatically). Also, each line of code execution from your Excel VBA must communicate to AutoCAD instance across the 2 application processes (Acad and excel), the execution is MUCH MORE slow than the same PDF plotting code being executed in AutoCAD VBA. So, I'd among many possible batch PDF plotting options, doing it from Excel probably is the worse one.

 

As @Ed__Jobe suggested, using AutoCAD core console would be much preferred/better choice, if you are up to it. If you are limited to use VBA code for some reason, do it from AutoCAD side is desired, IMO. You may want to first retrieve some data from an Excel sheet before starting the PDF exporting, of course.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 10

stocjon25
Observer
Observer

The reason of doing it inside Excel is because I have a specific task which involves a bunch of code in Excel retrieving some data from other documents, files and even change some AutoCad files generated by another program. So I want to concentrate all codes inside Excel because there I have all piece of tasks that I shall perform i.e. lot o buttons and each button executes one macro, for importing data, converting information, changing title block and so on. Once any person from my department has to perform this piece of work, there in the excel sheet he will have all buttons and just follow the sequence to have the task performed completely. When I meant I dont want to open AutoCad, as I am not a CODE guy I unterstand that I need to open AutoCAD go to certain button and request to run e.g. the code you mentioned and I do not want to do that I just want to have a button for clicking and it will convert all files from a specific folder that I will pick but it will be triggered from Excel.

0 Likes
Message 9 of 10

Ed__Jobe
Mentor
Mentor

@stocjon25 wrote:

 I just want to have a button for clicking and it will convert all files from a specific folder that I will pick but it will be triggered from Excel.


You can still have a button for plotting in xl, just don't have AutoCAD plot it, use acCoreConsole.exe. From the link I posted previously, the line below is the key part you need. Just have SHELL run it instead of the registry entry that was given.

"\"C:\\Program Files\\Autodesk\\AutoCAD 2026\\accoreconsole.exe\" /i \"%1\" /ld \"C:\\Program Files\\Autodesk\\AutoCAD 2026\\AecBase.dbx\" /p \"<YourProfileHere>\" /product \"C3D\" /language \"en-US\" /s \"C:\\<YourFilePathHere>\\RightClickDwgToPdf.scr\""

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 10 of 10

Ed__Jobe
Mentor
Mentor

@stocjon25  Did any of these suggestions solve the problem? If so, please mark the post as a solution.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes