VBA Macro AutoCAD : DWG to PDF

VBA Macro AutoCAD : DWG to PDF

Anonymous
Not applicable
18,202 Views
21 Replies
Message 1 of 22

VBA Macro AutoCAD : DWG to PDF

Anonymous
Not applicable

Hi,

 

I have to create a VBA macro for AutoCAD, but I'm not a VBA expert... Smiley LOL

 

The goal of my macro is :

  1. Get all the DWG in a specific directory
  2. Open DWG
  3. Convert them to PDF
  4. Rename & Move the PDF to another specific directory
  5. Close the DWG
  6. Loop on the next DWG File

Actually, there is what my macro does :

  1. Get all the DWG in a specific directory
  2. Open DWG (only the first)
  3. Convert them to PDF
  4. 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 ?

 

0 Likes
Accepted solutions (1)
18,203 Views
21 Replies
Replies (21)
Message 21 of 22

wianeh6700
Participant
Participant

Hi,

When i execute this code i get the error: error 9 index out of range,  any ideas what could be the problem ??

0 Likes
Message 22 of 22

wianeh6700
Participant
Participant

Hi,

When i execute your code, this error pops up :  "error 9 : Index out of range" .

Any ideas what could be the problem please ??

 

0 Likes