Hi, I want to convert dwg files to pdf using vba for autocad. the program open each dwg file, plot it to pdf, close it then move to the next file. i'm struggling with the error dwg files are not found even though they exist in the folder.
I get the error when it reaches the line AcadApplication.Documents.Open(fName). Any ideas what could be the problem?
The Code:
Private Function ObtainFileNames(path As String) As String()
Dim fileNames() As String 'Dynamic Array Declaration
Dim i As Integer
Dim file As String
''Get all DWG file from the Original Path
file = Dir(path & "*.dwg") ''Dir function gets the name of the file or a folder, using their path name, here it only gets the first file in the folder
While file <> ""
ReDim Preserve fileNames(i)
fileNames(i) = file
i = i + 1
file = Dir
Wend
ObtainFileNames = fileNames
End Function
Public Sub AutoPlot()
Dim backPlot As Integer
backPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT") ''Setting background plot variable because we need to disable background plotting
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
Dim fileNames() As String
Dim oPath As String
Dim dPath As String
''Original path -> Where DWG are
oPath = "C:\Users\Public\Documents\dwg\"
''Destination path -> Where PDF go
dPath = "C:\Users\Public\Documents\pdf\"
fileNames = ObtainFileNames(oPath)
Dim fName As String
Dim pdfName As String
Dim dwg As AcadDocument
Dim i As Variant
For i = 0 To UBound(fileNames)
fName = fileNames(i)
Dim splitedFileName() As String
Dim brutName As String
''Name de PDF "[DWGName].pdf"
splitedFileName = Split(fName, ".")
brutName = splitedFileName(0)
pdfName = dPath & brutName & ".pdf" ''Determine PDF name based on DWG file name
Set dwg = AcadApplication.Documents.Open(fName)
PlotToPdf dwg, pdfName
dwg.Close False
Next
ThisDrawing.SetVariable "BACKGROUNDPLOT", backPlot
End Sub
Private Sub PlotToPdf(dwg As AcadDocument, pdfFile As String)
Dim layout As AcadLayout
Set layout = ThisDrawing.ActiveLayout
layout.ConfigName = "DWG To PDF.pc3" 'Plot device
layout.CanonicalMediaName = "ISO_A4_(210.00_x_297.00_mm)" 'PDF format
layout.CenterPlot = True
layout.StandardScale = acScaleToFit
ThisDrawing.Application.ZoomExtents
Set currentplot = dwg.Plot
''Convertion step
currentplot.PlotToFile pdfFile
End Sub
Solved! Go to Solution.
Hi, I want to convert dwg files to pdf using vba for autocad. the program open each dwg file, plot it to pdf, close it then move to the next file. i'm struggling with the error dwg files are not found even though they exist in the folder.
I get the error when it reaches the line AcadApplication.Documents.Open(fName). Any ideas what could be the problem?
The Code:
Private Function ObtainFileNames(path As String) As String()
Dim fileNames() As String 'Dynamic Array Declaration
Dim i As Integer
Dim file As String
''Get all DWG file from the Original Path
file = Dir(path & "*.dwg") ''Dir function gets the name of the file or a folder, using their path name, here it only gets the first file in the folder
While file <> ""
ReDim Preserve fileNames(i)
fileNames(i) = file
i = i + 1
file = Dir
Wend
ObtainFileNames = fileNames
End Function
Public Sub AutoPlot()
Dim backPlot As Integer
backPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT") ''Setting background plot variable because we need to disable background plotting
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
Dim fileNames() As String
Dim oPath As String
Dim dPath As String
''Original path -> Where DWG are
oPath = "C:\Users\Public\Documents\dwg\"
''Destination path -> Where PDF go
dPath = "C:\Users\Public\Documents\pdf\"
fileNames = ObtainFileNames(oPath)
Dim fName As String
Dim pdfName As String
Dim dwg As AcadDocument
Dim i As Variant
For i = 0 To UBound(fileNames)
fName = fileNames(i)
Dim splitedFileName() As String
Dim brutName As String
''Name de PDF "[DWGName].pdf"
splitedFileName = Split(fName, ".")
brutName = splitedFileName(0)
pdfName = dPath & brutName & ".pdf" ''Determine PDF name based on DWG file name
Set dwg = AcadApplication.Documents.Open(fName)
PlotToPdf dwg, pdfName
dwg.Close False
Next
ThisDrawing.SetVariable "BACKGROUNDPLOT", backPlot
End Sub
Private Sub PlotToPdf(dwg As AcadDocument, pdfFile As String)
Dim layout As AcadLayout
Set layout = ThisDrawing.ActiveLayout
layout.ConfigName = "DWG To PDF.pc3" 'Plot device
layout.CanonicalMediaName = "ISO_A4_(210.00_x_297.00_mm)" 'PDF format
layout.CenterPlot = True
layout.StandardScale = acScaleToFit
ThisDrawing.Application.ZoomExtents
Set currentplot = dwg.Plot
''Convertion step
currentplot.PlotToFile pdfFile
End Sub
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Firstly, when post code in your discussion content, please use "</>" button in the toolbar above the message window. That keeps the code formatting, so that is more read-able.
As for the error you get, the first and easiest thing to debug is simply use MsgBox or Debug.Print to print out the file name and see it with your own eyes to make sure the file fully pathed name is correct. That is, in the "For i=0 to Ubound(...)" loop, comment out last 3 lines, use Debug.Print or MsgBox to show variable fName and pdfName
For i = 0 To UBound(fileNames)
fName = fileNames(i)
Dim splitedFileName() As String
Dim brutName As String
''Name de PDF "[DWGName].pdf"
splitedFileName = Split(fName, ".")
brutName = splitedFileName(0)
pdfName = dPath & brutName & ".pdf" ''Determine PDF name based on DWG file name
'' Add these 2 lines to verify the file names
Debug.Print fName
Debug.Print pdfName
''Set dwg = AcadApplication.Documents.Open(fName)
''PlotToPdf dwg, pdfName
''dwg.Close False
Next
The in VBA Editor's immediate window, you would see all the file names and you would be tell if the file names are really correct or not. Once you did such a simple debugging, you would have immediately known what went wrong: your DWG file name do not have full path (i.e. only a file name without path), so AutoCAD would try to find the file in its current working directory, thus the error.
So, writing code is only half of programming, debugging properly is another half of programming that make code works.
Norman Yuan
Firstly, when post code in your discussion content, please use "</>" button in the toolbar above the message window. That keeps the code formatting, so that is more read-able.
As for the error you get, the first and easiest thing to debug is simply use MsgBox or Debug.Print to print out the file name and see it with your own eyes to make sure the file fully pathed name is correct. That is, in the "For i=0 to Ubound(...)" loop, comment out last 3 lines, use Debug.Print or MsgBox to show variable fName and pdfName
For i = 0 To UBound(fileNames)
fName = fileNames(i)
Dim splitedFileName() As String
Dim brutName As String
''Name de PDF "[DWGName].pdf"
splitedFileName = Split(fName, ".")
brutName = splitedFileName(0)
pdfName = dPath & brutName & ".pdf" ''Determine PDF name based on DWG file name
'' Add these 2 lines to verify the file names
Debug.Print fName
Debug.Print pdfName
''Set dwg = AcadApplication.Documents.Open(fName)
''PlotToPdf dwg, pdfName
''dwg.Close False
Next
The in VBA Editor's immediate window, you would see all the file names and you would be tell if the file names are really correct or not. Once you did such a simple debugging, you would have immediately known what went wrong: your DWG file name do not have full path (i.e. only a file name without path), so AutoCAD would try to find the file in its current working directory, thus the error.
So, writing code is only half of programming, debugging properly is another half of programming that make code works.
Norman Yuan
Thank you so much for your answer,
I followed the code using step into option while monitoring the locals Window , as you can see below file names are correct. for example, for the first dwg file called "dessin1" the fName Variable gets "Dessin1.dwg" as for pdfName it gets "C:\Users\Public\Documents\pdf\Dessin1.pdf".
I was wondering if the AcadApplication.Documents.Open() requires only the file name or the whole path ?
I was
Thank you so much for your answer,
I followed the code using step into option while monitoring the locals Window , as you can see below file names are correct. for example, for the first dwg file called "dessin1" the fName Variable gets "Dessin1.dwg" as for pdfName it gets "C:\Users\Public\Documents\pdf\Dessin1.pdf".
I was wondering if the AcadApplication.Documents.Open() requires only the file name or the whole path ?
I was
Well, you have seen it clearly: variable "fName" only has file name in it, NO PATH. you NEED prefix the file name with full path in the Documents.Open() method. Isn't it so obvious?
Norman Yuan
Well, you have seen it clearly: variable "fName" only has file name in it, NO PATH. you NEED prefix the file name with full path in the Documents.Open() method. Isn't it so obvious?
Norman Yuan
Now i get it!
Sorry for the inconvenience I'm new to vba for autocad.
Thank you for your help
Now i get it!
Sorry for the inconvenience I'm new to vba for autocad.
Thank you for your help
Can't find what you're looking for? Ask the community or share your knowledge.