【SolidWorks宏】VBA 选择文件

本文介绍了一种利用HTML应用程序(HTA)和Windows注册表来实现文件选择的方法,包括两种不同的实现方式:一种是直接通过注册表读取用户选择的文件路径;另一种则是将文件路径写入到临时文本文件中再进行读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 _
    "<html>" & _
    "<head>" & _
    "<title>Browse</title>" & _
    "</head>" & _
    "<body>" & _
    "<input type='file' id='f' />" & _
    "<script type='text/javascript'>" & _
    "var f = document.getElementById('f');" & _
    "f.click();" & _
    "var shell = new ActiveXObject('WScript.Shell');" & _
    "shell.RegWrite('HKEY_CURRENT_USER\\Volatile Environment\\MsgResp', f.value);" & _
    "window.close();" & _
    "</script>" & _
    "</body>" & _
    "</html>"
    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 _
    "<html><head><meta charset='UTF-8' /><title>Browse</title></head>" & _
    "<body><input type='file' id='f'>" & _
    "<script type='text/javascript'>" & _
    " var f = document.getElementById('f');f.click();" & _
    " var fso = new ActiveXObject('Scripting.FileSystemObject');" & _
    " var file = fso.OpenTextFile('" & _
    Replace(tempBaseName, "\", "\\") & ".txt" & "', 2, true, -1);" & _
    " file.Write(' ' + f.value + ' '); file.Close(); window.close();" & _
    "</script></body></html>"
    
    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

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在VBA编程中,实现让用户选择文件夹的功能可以通过多种方法,以下是三种常见的实现方式: 1. API方法:通过调用Windows的SHBrowseForFolder和SHGetPathFromIDList函数,VBA可以直接与操作系统交互来实现文件夹选择。首先需要定义BROWSEINFO结构体,并声明相关的API函数。在GetFolder_API函数中,通过设置对话框标题和标志调用SHBrowseForFolder来显示对话框,然后利用SHGetPathFromIDList获取用户选定的文件夹路径。此外,还需要注意初始化和卸载Ole服务。 2. Shell.Application方法:此方法通过创建Shell.Application对象并调用其BrowseForFolder方法来实现文件夹选择。BrowseForFolder方法需要四个参数:父窗口句柄(通常为0)、对话框标题、标志(可选)以及预设的文件夹项(通常为0)。如果用户选择文件夹,该方法会返回一个Folder对象。通过检查该对象是否非空,可以获取选定文件夹的路径。 3. FileDialog方法:VBA的FileDialog对象提供了用户友好的文件文件夹选择对话框。在这种情况下,使用msoFileDialogFolderPicker常量来指定显示文件夹选择对话框。当用户点击“确定”时,Show方法会返回-1,表示用户已做出选择。可以通过SelectedItems(1)获取第一个(也是唯一一个)选定的文件夹路径。 这三种方法各有特点。API方法虽然灵活性最高,但需要编写较多代码且需要对API有一定了解;Shell.Application方法相对简单,但功能较为有限;FileDialog方法最为简单易用,适合那些不想深入了解底
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值