我在Inventor中使用VBA编辑器来执行此操作。我刚刚使用OpenFileDialog选择* .iam文件并在Form3.TextBox3.Text中显示名称。从那里,有一个shell设置来打开文件。但是,由于缺少路径的其余部分,shell不知道它只用文件名打开了什么。
我已经尝试了几种没有运气的方法,所以我放弃并询问它: 1)如何获取文件夹路径?从那里我将它设置为一个字符串。 (FileName已经是一个字符串。) 2)如何连接两个字符串以在文本框中一起显示?
非常感谢! 阿莉莎
相关代码:
这是调用OpenFileDialog的模块:
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA"(pOpenfilename As OPENFILENAME) As Long
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Function SelectFileOpenDialog()
Dim strTemp, strTemp1, pathStr As String
Dim i, n, j As Long
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim Fname As String
OpenFile.lStructSize = Len(OpenFile)
sFilter = "Assembly Files (*.iam)" & Chr(0) & "*.IAM" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = dir_path
OpenFile.lpstrTitle = "Select An Assembly"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "No file selected. Please try again."
Else
Fname = Trim$(OpenFile.lpstrFileTitle) ' copy the filename to "Fname"
n = FileLen(OpenFile.lpstrFile) 'length of the file
Resolve.FileName.Text = Fname
End If
End Function
这是尝试打开程序集的表单3的一部分:
Private Sub Open_Button_Click()
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open FileName
End Sub
答案 0 :(得分:1)
我希望这会有所帮助。这是我在MS Access vba中使用的一个函数:
Public Function SelectFile(multi As Boolean, Optional filterName As String, Optional filterList As String, Optional defaultPath As String)
Dim f As Object, vrtSelectedItem As Variant, s As String
If Nz(filterName) = "" Then filterName = "All Files"
If filterName = "All Files" Then filterList = "*.*"
s = ""
defaultPath = IIf(Nz(defaultPath, "") = "", CurrentProject.Path, defaultPath)
Set f = Application.FileDialog(msoFileDialogFilePicker)
With f
If (Dir(defaultPath, vbDirectory) <> "") Then .InitialFileName = defaultPath
.AllowMultiSelect = multi
.Filters.Clear
.Filters.Add filterName, filterList, 1
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
s = s & vrtSelectedItem & "|"
Next vrtSelectedItem
If s <> "" Then s = Left(s, Len(s) - 1)
End If
End With
SelectFile = s
End Function
它已经参数化了Application.FileDialog使用的所有输入。它返回所选文件的完整路径,或者在多次选择的情况下,每个路径与“|”连接。
如果您没有开始使用shell应用程序,这将满足您的需求。您可以使用InStrRev()
函数找到最后一个\
,以便将路径拆分为文件名和文件夹路径。