SolidWorks 2013宏 - 字符串操作?

时间:2015-06-18 18:58:01

标签: vba solidworks

SolidWorks使用VBA作为其宏,但它与Excel VBA(这是我习以为常的)非常不同。他们在SW中操纵字符串非常困难(而且很可能不可能)。我尝试过使用Left()函数和Mid()函数,但我无法弄清楚如何使它工作。基本上我需要它做的是一个另存为.DXF文件,并将其命名为标题,但没有表单名称。工作表名称导致问题,我试图将其删除。我可以用

Part.GetTitle

获取标题的字符串,例如

PA0000 - Sheet1

我只想要它

PA0000

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

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

但它会出现类型不匹配错误。我究竟做错了什么?这个宏的所有剩余部分就是删除" - Sheet1"。

2 个答案:

答案 0 :(得分:0)

我在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

答案 1 :(得分:0)

longname = Len(Part.GetTitle) - 9

'另存为 longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".SLDDRW", 0, 2)

所以完整的代码是:

Dim swApp As Object
Dim longname As Long
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
longname = Len(Part.GetTitle) - 9

' Save As
longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".SLDDRW", 0, 2)
boolstatus = Part.FeatureManager.EditRollback(swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "")
Part.ClearSelection2 True

' Save As
longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".PDF", 0, 0)
End Sub