2016-08-05 36 views
2

我創建了一個表單,允許用戶點擊一個按鈕,然後它允許您瀏覽和查找您的文檔或任何內容,並將其作爲鏈接提交給其他相關數據。我想知道如果他們是一種方式來允許用戶使鏈接連接到充滿多個文件的文件夾,而不需要手動執行它。這裏是我的代碼如下如何通過Excel中的vba表單鏈接文件

Private Sub AddPicture_Click() 
Dim strFileToLink As String 

'link name 
lnkNm = InputBox("please enter link description") 
Application.ScreenUpdating = False 
strFileToLink = Application.GetOpenFilename _ 
(Title:="Please select an Evidence file to link to") 

'Checking if file is selected. 
If strFileToLink = "" Then 
    'Displaying a message if file not choosen in the above step. 
    MsgBox "No file selected.", vbExclamation, "Sorry" 
    'And exiting from the procedure. 
    Exit Sub 
Else 
    'print link to sheet as a hyperlink. 
    erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
    With ActiveSheet 

     If ActiveSheet.Index >= 5 Then 
      .Hyperlinks.Add Anchor:=Cells(erow, 12), _ 
      Address:=strFileToLink, _ 
      ScreenTip:="Picture Link", _ 
      TextToDisplay:=lnkNm 

     Else 
      .Hyperlinks.Add Anchor:=Cells(erow, 13), _ 
      Address:=strFileToLink, _ 
      ScreenTip:="Picture Link", _ 
      TextToDisplay:=lnkNm 

     End If 
    End With 
    End If 
End Sub 

關於改進代碼的意見,將不勝感激。謝謝

+0

用戶表單上的鏈接是什麼意思?我很抱歉,我不明白您的要求 –

+0

我希望用戶窗體讓用戶抓住文件夾/文件,然後將鏈接放在Excel表單上。 – cfaidl05

+0

那麼你用上面的代碼面臨什麼問題? –

回答

4

而不是Application.GetOpenFileName使用Application.FileDialog,它可以爲文件夾或文件等自定義。

https://msdn.microsoft.com/en-us/library/office/ff836226.aspx

這裏是你如何使用它來得到一個文件夾名或文件名:

Dim fdlg As FileDialog 

Dim fdlgType as Long, itm as Variant 
fdlgType = Application.InputBox("Enter '3' to choose a FILE, or '4' to choose a FOLDER") 
If fdlgType < 3 or fdlgType > 4 Then Exit Sub 
Set fdlg = Application.FileDialog(fdlgType) 
With fdlg 
    .Title = IIf(fdlgType = 3, "Please select an Evidence FILE to link to", _ 
           "Please select an Evidence FOLDER to link to") 
    .ButtonName = IIf(fdlgType = 3, "Select File", "Select Folder") 
    .Show 
    For Each itm in .SelectedItems 
     MsgBox itm 
    Next 
End With 

還有就是FileDialog.AllowMultiSelect屬性,如果真將讓用戶選擇多個文件(不適用於文件夾)。然後,您可以循環播放.SelectedItems

在您的代碼,那麼For Each itm循環將含有添加超鏈接代碼:

If .SelectedItems.Count = 0 Then 
    MsgBox "Nothing selected.", vbExclamation, "Sorry" 
    Exit Sub 
End If 
For Each itm in .SelectedItems 
    'print link to sheet as a hyperlink. 
    With ActiveSheet 
     erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
     If .Index >= 5 Then 
      .Hyperlinks.Add Anchor:=.Cells(erow, 12), _ 
      Address:=itm, _ 
      ScreenTip:="Picture Link", _ 
      TextToDisplay:=lnkNm 

     Else 
      .Hyperlinks.Add Anchor:=.Cells(erow, 13), _ 
      Address:=itm, _ 
      ScreenTip:="Picture Link", _ 
      TextToDisplay:=lnkNm 

     End If 
    End With 
Next 
+1

謝謝你一點點調整它的作品完美。 – cfaidl05

+0

不客氣! –

1

而不是使用: -

Application.GetOpenFilename(Title:="Please select an evidence file to link to") 

甚至 -

Application.GetOpenFilename(Title:="Please select an evidence file to link to", MultiSelect:=True) 

以便他們可以選擇多個文件,您可以根據需要將其添加爲自己的訂單項。

我會建議使用: -

Application.FileDialog msoFileDialogFolderPicker 

對於文件夾選擇,並且: -

Application.FileDialog msoFileDialogFilePicker 

對於文件的選擇,因爲你沒有錯誤,問題是: -

我想知道如果他們是一種方式來允許用戶使鏈接連接到一個文件夾充滿多個文件

以上信息應足以讓您根據需要更改您的代碼,我建議在您的UserForm,'鏈接文件'和'鏈接文件夾'上有兩個按鈕。

如果在更改代碼時發生錯誤,請隨時發佈它們(如果找不到答案),並且我確信人們會幫助您完成特定的查詢。

相關問題