2013-05-29 28 views
0

在此輸入代碼我試圖在excel中創建一個按鈕,提示用戶輸入文件,然後將該文件作爲活動電子表格中的超文本鏈接上傳。Excel按鈕提示用戶將文件附加到活動工作表?

目標:文件上傳後,後續用戶只需單擊超鏈接查看文件。

我試過了,在excel中創建一個ActiveX控件,但是在單元格中將輸入表示爲超鏈接輸出是問題所在?

Private Sub CommandButton1_Click() 

Dim sFullName As String 
Application.FileDialog(msoFileDialogOpen).Show 
sFullName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1) 
End Sub 

插入引用到PDF

Sub InsertObjectAsIcon() 
'lets user browse for a file to insert into the 
'current active worksheet. 
'all are inserted as icons, not "visible" objects, so 
'to view they will need an appropriate viewer/reader 
'at the recipient end. 
' 
'This one shows how you could set up to use 
'several different icons depending on the type of file 
'inserted. You'll have to experiment by recording 
'macros while inserting various file types to build 
'up a list to use, just add new Case Is = statements 
'do deal with the file types. Be sure to enter the 
'file type in all UPPERCASE. 
' 
    Dim iconToUse As String 
    Dim fullFileName As String 
    Dim FNExtension As String 
    fullFileName = Application.GetOpenFilename("*.*, All Files", , , , False) 

    If fullFileName = "False" Then 
    Exit Sub ' user cancelled 
    End If 
'choose an icon based on filename extension 
    'get all after last "." in filename 
    FNExtension = Right(fullFileName, Len(fullFileName) - _ 
    InStrRev(fullFileName, ".")) 

    'select icon based on filename extension 
    Select Case UCase(FNExtension) 
    Case Is = "TXT" 
     iconToUse = "C:\Windows\system32\packager.dll" 

    Case Is = "XLS", "XLSM", "XLSX" 
     iconToUse = "C:\Windows\Installer\{91140000-0011-0000-0000-0000000FF1CE}\xlicons.exe" 

    Case Is = "PDF" 
     iconToUse = "C:\Windows\Installer\{AC76BA86-1033-F400-7761-000000000004}\_PDFFile.ico" 

    Case Else 
     'this is a generic icon 
     iconToUse = "C:\Windows\system32\packager.dll" 
    End Select 

    ActiveSheet.OLEObjects.Add(Filename:=fullFileName, Link:=False, DisplayAsIcon:=True, IconFileName:=iconToUse, IconIndex:=0, IconLabel:=fullFileName).Select 




End Sub 


Private Sub CommandButton1_Click() 

InsertObjectAsIcon 

End Sub 
+0

你有什麼試圖實現這個目標?查看[問]有關問題的信息,以及哪些問題更有可能得到解答。 –

回答

3

這段代碼打開通用文件對話框,過濾,以顯示.xslx文件。它拾取文件的路徑,然後將其插入活動單元。還有一個inputbox要求一個簡短的文本名稱,如果你不想看到完整的路徑。

Sub FileToLink() 

Dim strFileName As String 
Dim strShortName As String 

strFileName = Application.GetOpenFilename("Excel Documents (*.xlsx), *.xlsx") 

If strFileName = "False" Then 
    Exit Sub ' user cancelled 
End If 

strShortName = InputBox("What do you want to call this link?", "Short Text", strFileName) 

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=strFileName, TextToDisplay:=strShortName 

End Sub 

您可以用strFileName = Application.GetOpenFilename("All Documents (*.*), *.*")替代顯示所有文件。鏈接無關緊要,因爲點擊鏈接將調用與該文件類型鏈接的應用程序。

+1

謝謝你的工作,我改變了上面的代碼以允許使用Anchor:= Range(「A11」)的特定範圍 – user803271

相關問題