gpt4 book ai didi

vba - SolidWorks 2013 宏 - 字符串操作?

转载 作者:行者123 更新时间:2023-12-02 00:10:31 28 4
gpt4 key购买 nike

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

Part.GetTitle

获取标题的字符串,例如类似于

PA0000 - Sheet1

我只是希望它是

PA0000

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

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

但它给出了类型不匹配错误。我究竟做错了什么?该宏剩下的工作就是剪掉“- 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

关于vba - SolidWorks 2013 宏 - 字符串操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923584/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com