Excel按钮提示用户将文件附加到活动工作表?

时间:2013-05-29 08:33:26

标签: excel excel-vba excel-formula vba

在此处输入代码我正在尝试在Excel中创建一个按钮,提示用户输入文件,然后将此文件作为活动电子表格中的超文本链接上传。

目标:文件上传后,用户只需单击超链接即可查看该文件。

我试过在excel中创建一个ActiveX控件,但是在单元格中将输入表示为超链接输出是问题吗?

Private Sub CommandButton1_Click()

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

插入对pdfs的引用

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

1 个答案:

答案 0 :(得分:3)

此代码打开公共文件对话框,过滤后显示.xslx个文件。它选择文件的路径,然后将其插入activecell。如果您不想看到完整的路径,那么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 (*.*), *.*")来显示所有文件。链接到什么文件并不重要,因为单击该链接将调用与该文件类型链接的应用程序。