将solidworks工程图保存为带前缀的PDF文件
Saving solidworks drawings as PDF file with prefix
我正在尝试使用宏保存 solidworks 工程图文件并向 PDF 添加前缀。该代码在从 solidworks 中提取文件名后添加所有内容,但它似乎无法添加前缀
代码:
Dim swApp As Object
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long
Dim prefix As String
Dim CurrRev As String
Dim strFilenameShort As String
Dim TempString As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Save
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)
'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then
prefix = (InputBox("Enter Company Prefix"))
CurrRev = (InputBox("Enter Current Revision"))
strFilenameShort = swModel.GetPathName
strFilenameShort = Left(strFilenameShort, Len(strFilenameShort) - 7)
TempString = CStr(strFilenameShort)
strFilename = prefix & TempString & "REV" & CurrRev & ".PDF"
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0
End If
Set swApp = Application.SldWorks
End Sub
你好 Siddiq Khareghat,
在您的示例中,您将 prefix 与 TempString 的完整路径组合在一起,这是错误的,因为结果类似于“ prefixC:\...REV1.pdf"
这是一个包含多个步骤的示例,可提取模型路径名的所需部分。最后,所有内容都与 prefix 和 CurrRev
相结合
' PathName of current model document
Dim sModelFullPath As String
sModelFullPath = swModel.GetPathName
' get path name without filename
Dim sFilePath As String
sFilePath = Left(sModelFullPath, InStrRev(sModelFullPath, "\"))
' get filename and extension
Dim sFileName As String
sFileName = Right(sModelFullPath, Len(sModelFullPath) - InStrRev(sModelFullPath, "\"))
' get filename without extension
Dim sFileNameWithoutExtension As String
sFileNameWithoutExtension = Left(sFileName, InStrRev(sFileName, ".") - 1)
' combine everything to new path name
Dim sNewFullPath As String
sNewFullPath = prefix & sFileNameWithoutExtension & "REV" & CurrRev & ".pdf"
' SaveAs with new full path
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs sNewFullPath, 0, 0, swExportPDFData, 0, 0
我正在尝试使用宏保存 solidworks 工程图文件并向 PDF 添加前缀。该代码在从 solidworks 中提取文件名后添加所有内容,但它似乎无法添加前缀
代码:
Dim swApp As Object
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long
Dim prefix As String
Dim CurrRev As String
Dim strFilenameShort As String
Dim TempString As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Save
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)
'Export to PDF if it is a drawing
If (swModel.GetType = swDocDRAWING) Then
prefix = (InputBox("Enter Company Prefix"))
CurrRev = (InputBox("Enter Current Revision"))
strFilenameShort = swModel.GetPathName
strFilenameShort = Left(strFilenameShort, Len(strFilenameShort) - 7)
TempString = CStr(strFilenameShort)
strFilename = prefix & TempString & "REV" & CurrRev & ".PDF"
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData, 0, 0
End If
Set swApp = Application.SldWorks
End Sub
你好 Siddiq Khareghat,
在您的示例中,您将 prefix 与 TempString 的完整路径组合在一起,这是错误的,因为结果类似于“ prefixC:\...REV1.pdf"
这是一个包含多个步骤的示例,可提取模型路径名的所需部分。最后,所有内容都与 prefix 和 CurrRev
相结合' PathName of current model document
Dim sModelFullPath As String
sModelFullPath = swModel.GetPathName
' get path name without filename
Dim sFilePath As String
sFilePath = Left(sModelFullPath, InStrRev(sModelFullPath, "\"))
' get filename and extension
Dim sFileName As String
sFileName = Right(sModelFullPath, Len(sModelFullPath) - InStrRev(sModelFullPath, "\"))
' get filename without extension
Dim sFileNameWithoutExtension As String
sFileNameWithoutExtension = Left(sFileName, InStrRev(sFileName, ".") - 1)
' combine everything to new path name
Dim sNewFullPath As String
sNewFullPath = prefix & sFileNameWithoutExtension & "REV" & CurrRev & ".pdf"
' SaveAs with new full path
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs sNewFullPath, 0, 0, swExportPDFData, 0, 0