2014-02-28 21 views
0

我在Inventor中使用VBA編輯器來執行此操作。我剛剛使用了一個OpenFileDialog來選擇一個* .iam文件,並在Form3.TextBox3.Text中顯示該名稱。從那裏,有一個外殼設置來打開文件。但是,由於路徑的其餘部分缺失,shell不知道它只打開了文件名。如何獲取我的文件夾路徑名並將其與我的文件名連接起來?

我已經嘗試了這幾種方式沒有運氣,所以我放棄並詢問: 1)如何獲取文件夾路徑?從那裏我將它作爲一個字符串。 (FileName已經是一個字符串。) 2)如何連接兩個字符串以在文本框中一起顯示?

非常感謝! 阿莉莎

相關的代碼:

這是調用打開文件對話框的模塊:

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 
+1

顯示您的代碼... –

+0

@TimWilliams按要求添加 – meer2kat

+1

'OpenFile.lpstrFile'爲您提供完整路徑。 'OpenFile.lpstrFileTitle'只是文件名。 –

回答

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()函數來查找最後的\,以便將路徑分割爲文件名和文件夾路徑。

+0

謝謝!我會在星期一早上嘗試一下,確認它是否有效! – meer2kat

相關問題