Link to home
Start Free TrialLog in
Avatar of Fi69
Fi69

asked on

Opening a PDF from Word via VBA (Office 2010 64 bit problem)

Hi experts

I am running Word 2010 32 bit. My user must be running Word 2010 64 bit because the "before" code below was causing a compile error. I've got a button on the ribbon in Word that opens a PDF.

I've been searching for a solution. Can someone please confirm if my thoughts on how to correct this error are correct. Here's my before and after code.

I'm programming Word using VBA.

Before:

Option Explicit

Private Declare Function ShellExecute Lib "shell32" _
    Alias "ShellExecuteA" _
   (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10
Private Const SE_ERR_NOASSOC As Long = 31

Public Function File_Exists(ByVal sPathName As String, Optional Directory As Boolean) As Boolean

    On Error Resume Next
    If sPathName <> "" Then
    
        If IsMissing(Directory) Or Directory = False Then
        
        File_Exists = (Dir$(sPathName) <> "")
    Else
        
        File_Exists = (Dir$(sPathName, vbDirectory) <> "")
        End If
    
    End If
End Function

Sub QuickGuide(ByVal control As IRibbonControl)

    Dim strQuickGuide As String
    strQuickGuide = Application.StartupPath & "\My User Guide.pdf"


    If File_Exists(strQuickGuide) Then
        Call ShellExecute(0&, "open", strQuickGuide, 0&, 0&, SW_SHOWNORMAL)
    Else
        MsgBox ("PDF not available")
    End If

End Sub

Open in new window


And here's what I THINK I need to change it :

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If
    
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10
Private Const SE_ERR_NOASSOC As Long = 31


Public Function File_Exists(ByVal sPathName As String, Optional Directory As Boolean) As Boolean

    On Error Resume Next
    If sPathName <> "" Then
    
        If IsMissing(Directory) Or Directory = False Then
        
        File_Exists = (Dir$(sPathName) <> "")
    Else
        
        File_Exists = (Dir$(sPathName, vbDirectory) <> "")
        End If
    
    End If
End Function

Sub QuickGuide(ByVal control As IRibbonControl)

    Dim strQuickGuide As String
    strQuickGuide = Application.StartupPath & "\User Guide.pdf"


    If File_Exists(strQuickGuide) Then
        Call ShellExecute(0&, "open", strQuickGuide, 0&, 0&, SW_SHOWNORMAL)
    Else
        MsgBox ("PDF not available")
    End If

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Phillip Burton
Phillip Burton

If you are looking for something simpler, then you could use:

ActiveDocument.FollowHyperlink (strQuickGuide)

It has the advantage of not needing the libraries, which are probably the cause of the problem, but does mean you have a dialog box asking whether you are sure.
Avatar of Fi69

ASKER

Thanks for the responses. Confirm that my modified code did work.