从其他目录/文件夹打开pdf文件

时间:2015-06-07 01:32:24

标签: excel vba pdf

我在Microsoft Office Professional Plus 2013中使用的是Excel版本。

使用Excel VBA,我想启动Adobe Reader XI并打开另一个文件夹中的pdf文件。

如果Excel文件和Adobe Reader文件位于同一文件夹中,我可以成功打开Adobe Reader文件。这是有效的代码:

Dim ABCfilename As String
Dim returnAcrobatfile As Variant
ABCfilename = "ABC.pdf"
acrobatFile = ThisWorkbook.Path & Application.PathSeparator & ABCfilename
returnAcrobatfile = Shell("C:\Program Files\Adobe\Reader _
11.0\Reader\AcroRd32.exe " & acrobatFile, vbNormalFocus)

但是,我想启动Adobe Reader并对其进行编程以打开位于不同文件夹中的pdf文件。

我的Excel文件位于名为C:\ Customers \ Pricing \

的文件夹中

我的Adobe文件位于名为Z:\ XYZ Company \

的文件夹中

我应该如何修改以returnAcrobatfile开头的代码行,以便打开位于Z:\ XYZ Company的pdf文件?

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码。

Sub PDF_Picker()

Dim Acrobatfile         As String

Acrobatfile = GetFile
Runit (Acrobatfile)

End Sub

Sub Runit(FileName As String)
   Dim Shex As Object
   Dim tgtfile As String
   Set Shex = CreateObject("Shell.Application")
   tgtfile = FileName
   Shex.Open (tgtfile)
End Sub

Function GetFile()

    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
        .AllowMultiSelect = False
        .Title = "Select PDF Files"
        .Filters.Clear
        .Filters.Add "Adobe PDF Files", "*.pdf"
        .InitialView = msoFileDialogViewDetails
        If .Show = True Then
            GetFile = .SelectedItems(1)
        Else
            GetFile = False
        End If
    End With

End Function
  • 如果您的客户将Acrobat安装在与您硬编码的路径不同的位置,则您的解决方案将失败。我对其进行了调整以使其更加强大,并使用PDF的默认应用程序选择。 BTW如果你删除/删除函数GetFile中的过滤器:(.Filters.Add "Adobe PDF Files", "*.pdf") 您可以使用它来打开任何文件甚至是我刚刚测试过的mp3。
  • 感谢tigeravatar,我根据这篇文章调整了他的功能: Importing folder to Excel (FileDialogFolderPicker) using VBA
  • 通过以下帖子对用户1302114改编为RunIt Sub:How can Excel VBA open file using default application
相关问题