Hi,
I’m trying to create a VBA macro to plot a specific area from the Model space in AutoCAD to a PDF file. The plot settings (e.g., plotter, paper size, plot style) are already configured, so I don’t need to set them again in the code. I only want to specify the coordinates of the area to plot and generate the PDF.
Here’s my current code, but I’m getting the error:
Error Number: 438, Description: Object doesn't support this property or method.
Sub TestPlotFromModel()
Dim fileName As String
fileName = "C:\Temp\ModelAreaPlot.pdf" ' Specify the output PDF file name
' Coordinates of the plot area (example values)
Call PlotSelectedAreaFromModel(fileName, 0, 0, 420, 297)
End Sub
Sub PlotSelectedAreaFromModel(plotFileName As String, lowerLeftX As Double, lowerLeftY As Double, upperRightX As Double, upperRightY As Double)
On Error GoTo ErrorHandler
' Disable background plotting
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
' Switch to Model space layout
ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Model")
' Set the plot window using the coordinates
ThisDrawing.ActiveLayout.SetWindowToPlot Array(lowerLeftX, lowerLeftY), Array(upperRightX, upperRightY)
' Plot directly to the specified file
Debug.Print "Plotting to: " & plotFileName
ThisDrawing.Plot.PlotToFile plotFileName
' Notify user
Debug.Print "PDF generated successfully: " & plotFileName
Exit Sub
ErrorHandler:
Debug.Print "Error Number: " & Err.Number & ", Description: " & Err.Description
Err.Clear
Exit Sub
End Sub
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
You should indicate which line of code that causes the error to save others from having to guess, or actually running your code.
Anyway, I did run your code and get error on this line:
ThisDrawing.ActiveLayout.SetWindowToPlot Array(lowerLeftX, lowerLeftY), Array(upperRightX, upperRightY)
However, the error I got is different:
Error Number: 5, Description: Invalid procedure call or argument
I made changes like this:
Sub PlotSelectedAreaFromModel(plotFileName As String, lowerLeftX As Double, lowerLeftY As Double, upperRightX As Double, upperRightY As Double)
On Error GoTo ErrorHandler
' Disable background plotting
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
' Switch to Model space layout
ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Model")
Dim a(0 To 1) As Double
Dim b(0 To 1) As Double
a(0) = lowerLeftX: a(1) = lowerLeftY
b(0) = upperRightX: b(1) = upperRightY
' Set the plot window using the coordinates
ThisDrawing.ActiveLayout.SetWindowToPlot a, b
' Plot directly to the specified file
Debug.Print "Plotting to: " & plotFileName
ThisDrawing.Plot.PlotToFile plotFileName
' Notify user
Debug.Print "PDF generated successfully: " & plotFileName
Exit Sub
ErrorHandler:
Debug.Print "Error Number: " & Err.Number & ", Description: " & Err.Description
Err.Clear
Exit Sub
End Sub
Then, it works as expected.
Can't find what you're looking for? Ask the community or share your knowledge.