SolidWorks将VBA用于其宏,但是它与Excel VBA(这是我习惯的)有很大不同。它们使得在SW中操纵字符串非常困难(甚至很可能是不可能的)。我试过使用Left()函数和Mid()函数,但是我不知道如何使它工作。基本上,我需要做的是另存为.DXF文件,并将其命名为标题,但没有工作表名称。工作表名称导致了该问题,我正在尝试将其删除。我可以用

Part.GetTitle


获取标题的字符串,例如

PA0000 - Sheet1


我只是想成为

PA0000


有时长度不同,所以我尝试使用

Left(Part.GetTitle,Instr(Part.GetTitle, " ")-1)


但它给出了Type Mismatch错误。我究竟做错了什么?该宏剩下的就是剪切“-Sheet1”。

最佳答案

我已经在help.solidworks.com上对代码进行了一些更改;这应该为您工作。只需根据需要更改文件夹。

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
'http://help.solidworks.com/2012/English/api/sldworksapi/Save_File_As_PDF_Example_VB.htm
Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.DrawingDoc
    Dim swPart                      As SldWorks.PartDoc
    Dim bRet                        As Boolean
    Dim MyPath                      As String
    Dim MyFolder                    As String
    Dim longstatus                  As Long
    Dim filename                    As String
    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    MyFolder = "C:\temp" ' Change to temp drive since curdir is often write protected.
    myFile = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".") - 1)
    myFile = Right(myFile, Len(myFile) - InStrRev(myFile, "\"))
    MyPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)
    MyPath = Right(MyPath, Len(MyPath) - InStrRev(MyPath, "\"))

    Debug.Print "FileName = " & myFile
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "Folder = " & MyPath
    Debug.Print "Current Folder = " & CurDir$
    Debug.Print (MyFolder & "\" & myFile & ".DXF")
    filename = MyFolder & "\" & myFile & ".DXF"
    longstatus = swModel.SaveAs3(filename, 0, 0)
    ' For PDF Generation
    ' longstatus = swModel.SaveAs3(MyFolder & "\" & myFile & ".PDF", 0, 0)
End Sub

08-27 10:36