Hie,
I have this routine to plot in PDF but it give a mistake in
The mistake is:
Could you helo me please?
The media/paper name "a4", if you see it in the printing dialog box's "Paper size" dropdown list, is not "CanonicalMediaName"; rather, it is localized version of the canonical media name. By knowing the localized media name, you need to find out the corresponding canonical media name:
Dim mediaName as String
Dim allMediaNames As Variant
Dim i As Integer
Dim localeName As String
allMediaNames=ThisDrawing.ActiveLayout.GetCanonicalMediaNames
For i=LBound(allMediaNames) To UBound(allMediaNames)
localeName=ThisDrawing.ActiveLayout.GetLocaleMediaName(allMediaNames(i))
If LCase(localeName)="a4" Then
mediaName = allMediaNames(i)
Exit For
End If
Next
ThisDrawing.ActiveLayout.CanonicalMediaName = mediaName
... ...
Thanks so much. I adapt the code but i receive this warning:
Public Sub VBAplot()
Dim mediaName As String
Dim allMediaNames As Variant
Dim i As Integer
Dim localeName As String
Dim plotDevice As String
Dim plotFilePath As String
Dim currentPlot As AcadPlot
' Get all available paper sizes for the current layout
allMediaNames = ThisDrawing.ActiveLayout.GetCanonicalMediaNames
' Search for "A4" paper size
mediaName = ""
For i = LBound(allMediaNames) To UBound(allMediaNames)
localeName = ThisDrawing.ActiveLayout.GetLocaleMediaName(allMediaNames(i))
If LCase(localeName) = "a4" Then
mediaName = allMediaNames(i)
Exit For
End If
Next i
' Validate if A4 paper size was found
If mediaName = "" Then
MsgBox "The paper size 'A4' was not found for the selected plot device.", vbExclamation, "Error"
Exit Sub
End If
' Set the plot device (PDF)
plotDevice = "DWG To PDF.pc3"
ThisDrawing.ActiveLayout.configName = plotDevice
ThisDrawing.ActiveLayout.CanonicalMediaName = mediaName
ThisDrawing.ActiveLayout.CenterPlot = True
ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
' Define the output file path for the PDF
If ThisDrawing.Path = "" Then
MsgBox "The file must be saved before generating a PDF.", vbExclamation, "Error"
Exit Sub
End If
plotFilePath = ThisDrawing.Path & "\" & Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 4) & ".pdf"
' Generate the PDF
On Error Resume Next
Set currentPlot = ThisDrawing.Plot
If Err.Number <> 0 Then
MsgBox "Error configuring the plot device: " & Err.Description, vbCritical, "Error"
Exit Sub
End If
On Error GoTo 0
' Execute the plot to PDF
On Error Resume Next
If currentPlot.PlotToFile(plotFilePath) Then
MsgBox "PDF successfully generated: " & plotFilePath, vbInformation, "Success"
Else
MsgBox "Failed to generate PDF. Please check the plot device configuration.", vbCritical, "Error"
End If
On Error GoTo 0
End Sub
Well, paper sizes are defined according to plot device (printer or pc3). So, if you want to know the available paper sizes, you need to set the AcadPlotConfiguration/AcadLayout's plot device (ConfigName property). There may be different PDF plotters/PC3s in your computer. Assume you want to use AutoCAD's "DWG to PDF.pc3". Your code should first set the "ConfigName" BEFORE trying to find a suitable paper size. Also, you may want to call RefreshPlotDeviceInfo() (especially if the ActiveLayout was not set to use "DWG to PDF.pc3" and you code just set it to use "DWG to PDF.pc3"). So the code should be like (psuedo code):
plotDevice = "DWG To PDF.pc3"
ThisDrawing.ActiveLayout.configName = plotDevice
ThisDrawing.ActiveLayout.RefreshPlotDeviceInfo
allMediaNames = ThisDrawing.ActiveLayout.GetCanonicalMediaNames
For i=LBound(allMediaNames) To UBound(allMediaNames)
localeName=ThisDrawing.ActiveLayout.GetLocaleMediaName(allMediaNames(i))
Debug.Print localeName '' Show the localeName in the immediate window
If LCase(localeName)="XXX" Then
mediaName = allMediaNames(i)
Exit For
End If
Next
Looking at the printed localeName in immediate window, you should be able to tell why there "a4" is not found: the device (DWG to PDF.pc3) does not come with a media size named as "a4" or "A4" (locale name). You should also be aware of it by lookin at the paper size dropdown list in Plot dialog box (all the paper sizes in the dropdown list are locale names).
Thanks so much. I rewrite the code with you comments but i receive the same message:
Public Sub VBAplot()
Dim mediaName As String
Dim allMediaNames As Variant
Dim i As Integer
Dim localeName As String
Dim plotDevice As String
Dim plotFilePath As String
Dim currentPlot As AcadPlot
' Set the plot device (PDF)
plotDevice = "DWG To PDF.pc3"
ThisDrawing.ActiveLayout.ConfigName = plotDevice
ThisDrawing.ActiveLayout.RefreshPlotDeviceInfo
allMediaNames = ThisDrawing.ActiveLayout.GetCanonicalMediaNames
For i = LBound(allMediaNames) To UBound(allMediaNames)
localeName = ThisDrawing.ActiveLayout.GetLocaleMediaName(allMediaNames(i))
Debug.Print localeName '' Show the localeName in the immediate window
If LCase(localeName) = "ISO A4 (210.00 x 297.00 mm)" Then
mediaName = allMediaNames(i)
Exit For
End If
Next
' Validate if A4 paper size was found
If mediaName = "" Then
MsgBox "The paper size 'A4' was not found for the selected plot device.", vbExclamation, "Error"
Exit Sub
End If
' Define the output file path for the PDF
If ThisDrawing.Path = "" Then
MsgBox "The file must be saved before generating a PDF.", vbExclamation, "Error"
Exit Sub
End If
plotFilePath = ThisDrawing.Path & "\" & Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 4) & ".pdf"
' Generate the PDF
On Error Resume Next
Set currentPlot = ThisDrawing.Plot
If Err.Number <> 0 Then
MsgBox "Error configuring the plot device: " & Err.Description, vbCritical, "Error"
Exit Sub
End If
On Error GoTo 0
' Execute the plot to PDF
On Error Resume Next
If currentPlot.PlotToFile(plotFilePath) Then
MsgBox "PDF successfully generated: " & plotFilePath, vbInformation, "Success"
Else
MsgBox "Failed to generate PDF. Please check the plot device configuration.", vbCritical, "Error"
End If
On Error GoTo 0
End Sub
this line is obviously the cause of not finding "ISO A4 (210.00 x 297.00)mm":
If LCase(localeName) = "ISO A4 (210.00 x 297.00 mm)" Then
it should be
If LCase(localeName) = "iso a4 (210.00 x 297.00 mm)" Then
or:
If UCase(localeName) = "ISO A4 (210.00 x 297.00 MM)" Then
Can't find what you're looking for? Ask the community or share your knowledge.