【问题标题】:Excel to open word, powerpoint and pdf filesExcel 打开 word、powerpoint 和 pdf 文件
【发布时间】:2014-04-07 04:31:55
【问题描述】:

我正在尝试让 Excel 打开 xls 以外的文件(word 文件、powerpoint 文件和 pdf 文件)。我只成功使用了 excel 文件(原因很明显)。有没有办法让excel使用默认程序打开文件。例如,如果我打开一个 .doc 文件,它应该使用 Word,但如果我打开一个 .pdf,它应该使用 Acrobat(或其他 pdf 阅读器)。我正在使用超链接。我会告诉你我到目前为止得到了什么:

我有一个列表(在本例中为 Listbox2),其中包含用于加载特定文件夹中所有文件的代码:

Private Sub UserForm_Initialize()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
USER = TextBox99
MyFolder = "C:\Example"
MyFile = Dir(MyFolder & "\*.*")
Do While MyFile <> ""
    ListBox2.AddItem MyFile
    MyFile = Dir
Loop
End Sub

假设在 listbox2 中我有文件 word.doc、powerpoint.ppt、excel.xls 和 acrobat.pdf。如果我从 listbox2 中选择文件并按下带有以下代码的按钮:

Private Sub CommandButton14_Click()
   Dim strFileandPath As String
   strFileandPath = "C:\Example\" & UserForm1.ListBox2.Column(0) 
   Workbooks.Open strFileandPath 
End Sub

它只会打开 excel 文件。其他的会给我一个错误。有没有办法让 Excel 使用 PC 中的默认程序打开任何类型的文件。感谢您的关注!

干杯

【问题讨论】:

标签: vba excel hyperlink


【解决方案1】:

我已经可以打开 pdf 和 word 文件,但现在我无法打开 excel 文件。归功于 user1302114 (How can Excel VBA open file using default application)。

我使用的代码:

    Dim Shex As Object
   Set Shex = CreateObject("Shell.Application")
   tgtfile = "C:\Example" & UserForm1.ListBox2.Column(0)
   Shex.Open (tgtfile)

我还在 Tools-Preferences 中启用了 Acrobat 和词库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2019-05-13
    • 2015-12-23
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    相关资源
    最近更新 更多