Solved

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

Posted on 2014-11-19
3
222 Views
Last Modified: 2014-12-04
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

0
Comment
Question by:Fi69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40455154
Hi,

SInce ShellExecute returns in 32 and 64 the same type of variable you only have to test for VBA7

for other function which return other varaiables you have to test with Win64

refer to

http://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx

Regards
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40455182
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.
0
 

Author Comment

by:Fi69
ID: 40481733
Thanks for the responses. Confirm that my modified code did work.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question