I write code for plot all dwg of project to pdf. But code con't create all pdf of project .Received an incomplete pdf file. Plase help me.
This my code . Sub ReadFileForPlotToPdf for Read dwg file of project and Sub PlotThisFileToPdf for plot pdf
Sub ReadFileForPlotToPdf()
Dim configName As String
Dim mediaName As String
Dim plotRotation As Integer
Dim plotType As Integer
Dim scaleSheet As Integer
Dim styleSheet As String
Dim plotWithStyles As Boolean
Dim plotHidden As Boolean
Dim plotViewportsFirst As Boolean
With ThisDrawing.ActiveLayout
configName = .configName
mediaName = .CanonicalMediaName
plotRotation = .plotRotation
plotType = .plotType
scaleSheet = .StandardScale
styleSheet = .styleSheet
plotWithStyles = .PlotWithPlotStyles
plotHidden = .plotHidden
plotViewportsFirst = .plotViewportsFirst
End With
Dim folderPath As String
folderPath = ThisDrawing.Path
Dim File As String
File = Dir(folderPath & "\*.dwg")
Dim FullFilename As String
Do While File <> ""
FullFilename = folderPath & "\" & File
If FullFilename <> ThisDrawing.FullName Then
ThisDrawing.Application.Documents.Open (FullFilename)
With ThisDrawing.ActiveLayout
.configName = configName
.CanonicalMediaName = mediaName
.CenterPlot = True
.plotRotation = plotRotation
.plotType = plotType
.StandardScale = scaleSheet
.styleSheet = styleSheet
.PlotWithPlotStyles = plotWithStyles
.plotHidden = plotHidden
.plotViewportsFirst = plotViewportsFirst
End With
'Save plot style
ThisDrawing.Save
'Plot thisdrawing to pdf
PlotThisFileToPdf
ThisDrawing.Close False
Else
'Plot file type to pdf
PlotThisFileToPdf
End If
File = Dir
If Emergency Then
File = ""
End If
Loop
End Sub
Sub PlotThisFileToPdf()
Dim File As String
File = ThisDrawing.FullName
Dim plotFileName As String
plotFileName = Left(File, Len(File) - 4) & ".pdf"
Dim result As Boolean
On Error Resume Next
result = ThisDrawing.plot.PlotToFile(plotFileName)
On Error GoTo 0
If result Then
Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
Else
Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
End If
End Sub
Thank you.
Solved! Go to Solution.
Solved by sonchai_n. Go to Solution.
Firstly, please post code by using the button "</>", so the code would be easier to read.
You may want to explain what "...But code con't create all pdf of project .Received an incomplete pdf file. ..." mean. Do you mean some drawing files opened and plotted correctly, and some do not, or the PDF does not present the desired plotting area?
I mean some DWG files can be plotted to PDF, but some cannot. When plotting the entire project to PDF, not all files are included in the output, even though the DWG files themselves can be opened normally without any issues. The problem only occurs during the plot-to-PDF process, resulting in incomplete PDFs.
Sub Button1_Click()
Call ReadFileForPlotToPdf
End Sub
Sub ReadFileForPlotToPdf()
Dim configName As String
Dim mediaName As String
Dim plotRotation As Integer
Dim plotType As Integer
Dim scaleSheet As Integer
Dim styleSheet As String
Dim plotWithStyles As Boolean
Dim plotHidden As Boolean
Dim plotViewportsFirst As Boolean
With ThisDrawing.ActiveLayout
configName = .configName
mediaName = .CanonicalMediaName
plotRotation = .plotRotation
plotType = .plotType
scaleSheet = .StandardScale
styleSheet = .styleSheet
plotWithStyles = .PlotWithPlotStyles
plotHidden = .plotHidden
plotViewportsFirst = .plotViewportsFirst
End With
Dim folderPath As String
folderPath = ThisDrawing.Path
Dim File As String
File = Dir(folderPath & "\*.dwg")
Dim FullFilename As String
Do While File <> ""
FullFilename = folderPath & "\" & File
If FullFilename <> ThisDrawing.FullName Then
ThisDrawing.Application.Documents.Open (FullFilename)
With ThisDrawing.ActiveLayout
.configName = configName
.CanonicalMediaName = mediaName
.CenterPlot = True
.plotRotation = plotRotation
.plotType = plotType
.StandardScale = scaleSheet
.styleSheet = styleSheet
.PlotWithPlotStyles = plotWithStyles
.plotHidden = plotHidden
.plotViewportsFirst = plotViewportsFirst
End With
'Save plot style
ThisDrawing.Save
'Plot thisdrawing to pdf
PlotThisFileToPdf
ThisDrawing.Close False
Else
'Plot file type to pdf
PlotThisFileToPdf
End If
File = Dir
If Emergency Then
File = ""
End If
Loop
End Sub
Sub PlotThisFileToPdf()
Dim File As String
File = ThisDrawing.FullName
Dim plotFileName As String
plotFileName = Left(File, Len(File) - 4) & ".pdf"
Dim result As Boolean
On Error Resume Next
result = ThisDrawing.plot.PlotToFile(plotFileName)
On Error GoTo 0
If result Then
Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
Else
Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
End If
End Sub
So, when you say the PDFs are incomplete, you mean, say, you have 10 drawings in the folder, and all 10 drawing have been opened in AutoCAD for plotting, but instead of getting 10 PDFs, you got less PDF files, right?
If so, obviously, something is not correct i the PlotThisFileToPdf() method, most likely one error occurred but your code did not catch it, because you use On "Error Resume Next". Have you taken a closer look in the immediate window to see if the "Plot failed for: ..." printed there?
Also, you might want to set system variable "BackgroundPlot" to 0 before the loop starts (and restore it to original value after PDF plotting is done).
Yes i got less PDF files.
I set "BackgroundPlot" to 0 This solution is Ok
Thank you very must
Sub PlotThisFileToPdf()
Dim File As String
File = ThisDrawing.FullName
Dim plotFileName As String
plotFileName = Left(File, Len(File) - 4) & ".plt.pdf"
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
Dim result As Boolean
On Error Resume Next
result = ThisDrawing.plot.PlotToFile(plotFileName)
On Error GoTo 0
If result Then
Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
Else
Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
End If
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.