0

ユーザーにファイルを要求し、アクティブなスプレッドシートにハイパーリンクを作成するボタンを作成しようとしています。

目標: ファイルがアップロードされた後、後続のユーザーはハイパーリンクをクリックしてファイルを表示できます。

私が試したことは、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).Select3

End Sub


Private Sub CommandButton1_Click()

InsertObjectAsIcon

End Sub
4

1 に答える 1

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 (*.*), *.*")すべてのファイルを表示するように置き換えることができます。リンクをクリックすると、そのファイル タイプにリンクされたアプリケーションが呼び出されるため、リンクがどのファイルであるかは関係ありません。

于 2013-05-29T10:15:31.013 に答える