?
Solved

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

Posted on 2014-11-19
3
Medium Priority
?
224 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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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