I am currently working on a batch plot program for AutoCAD and I’m about 95% done. But I have a problem. When ever the program runs for the first time and I plot a list of files, it never plots the first file it opens. It only plots the 2nd file and up, but never plot the first file. Here is part of the code. Please help
Sub PrintTheseFiles()
Me.Hide
On Error Resume Next
For Collate = 1 To Combo3.List(Combo3.ListIndex)
Dim AcadApp As AcadApplication
Set AcadApp = GetObject(, "AutoCAD.Application"
If Err Then
Err.Clear
Set AcadApp = CreateObject("Autocad.Application"
Set AcadApp = GetObject(, "AutoCAD.Application"
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
AcadApp.Visible = True
ReadOnly = True
For TimesToPlot = 0 To (ListBox1.ListCount - 1)
ArraySize = 0
ReDim AddedLayouts(ArraySize)
ListBox1.ListIndex = TimesToPlot ' if all layouts are to be plotted
FileToOpen = ListBox1.Text
AcadApp.Documents.Open FileToOpen, ReadOnly
Set AcadDoc = AcadApp.ActiveDocument
'================================================================
'Layout information
'================================================================
'ArraySize = 0
For Each LayOut In AcadDoc.Layouts
If LayOut.Name = "Model" Then
GoTo SkipLayOut
End If
LayOut.ConfigName = Combo5.List(Combo5.ListIndex)
LayOut.RefreshPlotDeviceInfo
LayOut.CenterPlot = Check1.Value
LayOut.PlotType = WhatToPlot
LayOut.PlotRotation = DwgOrientation
'=======================================================
'=======================================================
Open App.Path & "\media" & Combo5.ListIndex & "a" & ".inf" For Input As #1
x = 0
Do While Not EOF(1)
Input #1, Pname
If x = Combo6.ListIndex Then
PaperName = Pname
End If
x = x + 1
Loop
Close #1
ACADPref.DefaultPlotStyleTable = Combo7.List(Combo7.ListIndex)
'it's the plot name that makes this work, not the style
LayOut.CanonicalMediaName = PaperName
'=======================================================
'=======================================================
If Combo1.ListIndex > 0 Then
LayOut.UseStandardScale = True
LayOut.StandardScale = ScaleToPlot
Else
LayOut.UseStandardScale = False
Numerator = Text5.Text
Denominator = Text6.Text
LayOut.SetCustomScale Numerator, Denominator
End If
If Label13.Caption = " mm" Then
LayOut.PaperUnits = acMillimeters
Else
LayOut.PaperUnits = acInches
End If
Dim AcadLayOut As AcadLayOut
Dim NewOrigin As Variant
Set AcadLayOut = AcadDoc.ActiveLayout
NewOrigin = AcadLayOut.PlotOrigin
If LayOut.PaperUnits = acInches Then
NewOrigin(0) = Text2.Text * 25.4 ' Convert to MilM
NewOrigin(1) = Text1.Text * 25.4 ' Convert to MilM
LayOut.PlotOrigin = NewOrigin
Else
NewOrigin(0) = Text2.Text
NewOrigin(1) = Text1.Text
LayOut.PlotOrigin = NewOrigin
End If
ReDim Preserve AddedLayouts(ArraySize)
AddedLayouts(ArraySize) = LayOut.Name
ArraySize = ArraySize + 1
SkipLayOut:
Next LayOut
'================================================================
'Layout information
'================================================================
'================================================================
'Plot portion of the program, do not erease
'================================================================
LayoutList = AddedLayouts
Set Plot = AcadDoc.Plot
'AcadApp.ZoomExtents
If Option5.Value = True Then
Plot.SetLayoutsToPlot LayoutList
Plot.PlotToDevice
Else
Plot.PlotToDevice
End If
SysvarName = "FILEDIA"
intData = 1
sysVarData = intData ' Integer data
'================================================================
'Plot portion of the program, do not erease
'================================================================
If ListBox1.ListCount = 1 Then
AcadApp.ActiveDocument.Close (False)
GoTo OnlyOne
End If
AcadApp.ActiveDocument.Close (False)
Next TimesToPlot
OnlyOne:
Next Collate
AcadApp.ActiveDocument.SetVariable SysvarName, sysVarData
'AcadApp.Quit
Me.Visible = True
End Sub
Sub PrintTheseFiles()
Me.Hide
On Error Resume Next
For Collate = 1 To Combo3.List(Combo3.ListIndex)
Dim AcadApp As AcadApplication
Set AcadApp = GetObject(, "AutoCAD.Application"
If Err Then
Err.Clear
Set AcadApp = CreateObject("Autocad.Application"
Set AcadApp = GetObject(, "AutoCAD.Application"
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
AcadApp.Visible = True
ReadOnly = True
For TimesToPlot = 0 To (ListBox1.ListCount - 1)
ArraySize = 0
ReDim AddedLayouts(ArraySize)
ListBox1.ListIndex = TimesToPlot ' if all layouts are to be plotted
FileToOpen = ListBox1.Text
AcadApp.Documents.Open FileToOpen, ReadOnly
Set AcadDoc = AcadApp.ActiveDocument
'================================================================
'Layout information
'================================================================
'ArraySize = 0
For Each LayOut In AcadDoc.Layouts
If LayOut.Name = "Model" Then
GoTo SkipLayOut
End If
LayOut.ConfigName = Combo5.List(Combo5.ListIndex)
LayOut.RefreshPlotDeviceInfo
LayOut.CenterPlot = Check1.Value
LayOut.PlotType = WhatToPlot
LayOut.PlotRotation = DwgOrientation
'=======================================================
'=======================================================
Open App.Path & "\media" & Combo5.ListIndex & "a" & ".inf" For Input As #1
x = 0
Do While Not EOF(1)
Input #1, Pname
If x = Combo6.ListIndex Then
PaperName = Pname
End If
x = x + 1
Loop
Close #1
ACADPref.DefaultPlotStyleTable = Combo7.List(Combo7.ListIndex)
'it's the plot name that makes this work, not the style
LayOut.CanonicalMediaName = PaperName
'=======================================================
'=======================================================
If Combo1.ListIndex > 0 Then
LayOut.UseStandardScale = True
LayOut.StandardScale = ScaleToPlot
Else
LayOut.UseStandardScale = False
Numerator = Text5.Text
Denominator = Text6.Text
LayOut.SetCustomScale Numerator, Denominator
End If
If Label13.Caption = " mm" Then
LayOut.PaperUnits = acMillimeters
Else
LayOut.PaperUnits = acInches
End If
Dim AcadLayOut As AcadLayOut
Dim NewOrigin As Variant
Set AcadLayOut = AcadDoc.ActiveLayout
NewOrigin = AcadLayOut.PlotOrigin
If LayOut.PaperUnits = acInches Then
NewOrigin(0) = Text2.Text * 25.4 ' Convert to MilM
NewOrigin(1) = Text1.Text * 25.4 ' Convert to MilM
LayOut.PlotOrigin = NewOrigin
Else
NewOrigin(0) = Text2.Text
NewOrigin(1) = Text1.Text
LayOut.PlotOrigin = NewOrigin
End If
ReDim Preserve AddedLayouts(ArraySize)
AddedLayouts(ArraySize) = LayOut.Name
ArraySize = ArraySize + 1
SkipLayOut:
Next LayOut
'================================================================
'Layout information
'================================================================
'================================================================
'Plot portion of the program, do not erease
'================================================================
LayoutList = AddedLayouts
Set Plot = AcadDoc.Plot
'AcadApp.ZoomExtents
If Option5.Value = True Then
Plot.SetLayoutsToPlot LayoutList
Plot.PlotToDevice
Else
Plot.PlotToDevice
End If
SysvarName = "FILEDIA"
intData = 1
sysVarData = intData ' Integer data
'================================================================
'Plot portion of the program, do not erease
'================================================================
If ListBox1.ListCount = 1 Then
AcadApp.ActiveDocument.Close (False)
GoTo OnlyOne
End If
AcadApp.ActiveDocument.Close (False)
Next TimesToPlot
OnlyOne:
Next Collate
AcadApp.ActiveDocument.SetVariable SysvarName, sysVarData
'AcadApp.Quit
Me.Visible = True
End Sub