Solved

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

Posted on 2014-11-19
3
208 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
3 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the responses. Confirm that my modified code did work.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now