【SolidWorks宏】VBA 选择文件

Rem 选择文件-从注册表中获取
Function SG_BrowseForFileFromReg()
    Dim shell: Set shell = CreateObject("WScript.Shell")
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim tempFolder: Set tempFolder = fso.GetSpecialFolder(2)
    Dim tempName: tempName = fso.GetTempName()
    Dim tempFile: Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
    tempFile.Write _
    "" & _
    "" & _
    "Browse" & _
    "" & _
    "" & _
    "" & _
    "" & _
    "" & _
    ""
    tempFile.Close
    shell.Run tempFolder & "\" & tempName & ".hta", 0, True
    SG_BrowseForFileFromReg = shell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp")
    shell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp"
    fso.DeleteFile tempName & ".hta"
End Function

Rem 选择文件-从临时文件中获取
Function SG_BrowseForFileFromTxt()
    Dim shell: Set shell = CreateObject("WScript.Shell")
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim tempFolder: Set tempFolder = fso.GetSpecialFolder(2)
    Dim tempName: tempName = fso.GetTempName()
    Dim tempFile: Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
    Dim tempBaseName: tempBaseName = tempFolder & "\" & tempName
    
    Rem 写HTML应用程序(hta)
    tempFile.Write _
    "Browse" & _
    "" & _
    ""
    
    tempFile.Close
    shell.Run tempBaseName & ".hta", 1, True
    
    Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1, False, -1)
    SG_BrowseForFileFromTxt = Trim(tempFile.ReadLine)
    tempFile.Close
    
    Rem 删除创建的临时文件
    fso.DeleteFile tempBaseName & ".hta"
    fso.DeleteFile tempBaseName & ".txt"
End Function

你可能感兴趣的:(SolidWorks宏)