- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I have to create a VBA macro for AutoCAD, but I'm not a VBA expert...
The goal of my macro is :
- Get all the DWG in a specific directory
- Open DWG
- Convert them to PDF
- Rename & Move the PDF to another specific directory
- Close the DWG
- Loop on the next DWG File
Actually, there is what my macro does :
- Get all the DWG in a specific directory
- Open DWG (only the first)
- Convert them to PDF
- Rename & Move the PDF to another specific directory
I have to main issue :
- I don't know how to close my DWG
- The macro crash at the conversion part at the 2nd DWG
The macro :
Sub DWGtoPDF()
Dim file As String
Dim path As String
Dim destinationPath As String
path = "D:\Users\merel.thomas\Documents\test\dwg\"
destinationPath = "D:\Users\merel.thomas\Documents\test\pdf\"
'Get all DWG from directory
file = dir$(path & "*.DWG")
'Loop on all DWG files
While file <> ""
ThisDrawing.Application.Documents.Open path & file
Dim currentplot As AcadPlot
Set currentplot = ThisDrawing.Plot
ThisDrawing.ActiveLayout.ConfigName = "DWG To PDF.pc3" 'Plot device
ThisDrawing.ActiveLayout.CanonicalMediaName = "ANSI_full_bleed_D_(34.00_x_22.00_Inches)" 'PDF format
ThisDrawing.ActiveLayout.CenterPlot = True
ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
ThisDrawing.Application.ZoomExtents
'Convertion DWG to PDF
' FAIL HERE
currentplot.PlotToDevice
Dim splitedFileName() As String
Dim fileName As String
splitedFileName = Split(file, ".")
fileName = splitedFileName(0)
Dim originalPdfFile As String
Dim destinationPdfFile As String
originalPdfFile = path & fileName & "-Model.pdf"
destinationPdfFile = destinationPath & fileName & ".pdf"
Line1:
g_sb_Delay 26 'Waiting until the PDF is created
If dir(originalPdfFile) <> "" Then
'Move + rename PDF file
Name originalPdfFile As destinationPdfFile
Else
GoTo Line1 'If PDF is not created after x seconds, retry
End If
'Close file
' FAIL HERE
AcadApplication.Documents.Close
Wend
End Sub
Public Sub g_sb_Delay(ai_Count As Long)
Dim Start As Long
Dim I As Long
I = 0
Start = Timer ' Set start time.
Do While Timer < Start + ai_Count
DoEvents ' Yield to other processes.
Loop
End Sub
Can anyone help me ?
Solved! Go to Solution.