Solved

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

Posted on 2014-11-19
3
216 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word / Printing 8 59
How can I eliminate Paragraph Marks using VBA? 7 47
clicking a shape in a frame array vb6 3 40
Resetting Word when a document is closed 5 38
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

896 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

15 Experts available now in Live!

Get 1:1 Help Now