Option Explicit
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
Dim res as Long
Private Sub Command1_Click()
res = ShellExecute(hWnd, vbNullString, "FULL PATH AND FILE NAME", vbNullString, vbNullString, vbNormalFocus)
If res = 31 Then
MsgBox "This file is not associated with a program!", vbCritical, "File Association Error"
Exit Sub
End If
End If
End Sub
Thanks Swi and sdraper for your suggestions, but nothing happens when I click the Command2 Button.
I tried Swi's code first and entered:
Private Sub Command1_Click()
res = ShellExecute(hWnd, vbNullString, "C:\Documents and Settings\ehicks\Desktop\TrailProvisioningReport.doc", vbNullString, vbNullString, vbNormalFocus)
If res = 31 Then
MsgBox "This file is not associated with a program!", vbCritical, "File Association Error"
Exit Sub
End If
End If
End Sub
That didn't display the Word doc when I clicked the Button, so I tried the code from FAQ222-3212 as follows, but it likewise produced no results:
Private Sub cmdButtom2_Click()
Dim WordObj As Word.Application
Set WordObj = CreateObject("Word.Application")
Dim objWord As Word.Document
Set objWord = WordObj.Documents.Open(FileName:=C \ Documents And Settings \ ehicks \ Desktop & "\TrailProvisioningReport.doc")
WordObj.Visible = True
Set objWord = Nothing
End Sub
First thing I notice you click event is
Private Sub cmdButtom2_Click()
instead of
Private Sub cmdButton2_Click()
Thsi makes me think the code isn't firing due to the misspelling.
I put your code in the click event of a button and first you should get an error saying "user definedtype not defined" if you haven;t added a reference to the Word object model to your project. Once you get that done the line:
Set objWord = WordObj.Documents.Open(FileName:=C \ Documents And Settings \ ehicks \ Desktop & "\TrailProvisioningReport.doc")
isn't a valid path statement and will throw and error. Replce that line with this:
Set objWord = WordObj.Documents.Open(FileName:="C:\Documents And Settings\ehicks\Desktop\TrailProvisioningReport.doc")
Sam, I corrected the misspelling and changed the line. My code is now:
Private Sub cmdButton2_Click()
Dim WordObj As Word.Application
Set WordObj = CreateObject("Word.Application")
Dim objWord As Word.Document
Set objWord = WordObj.Documents.Open _(FileName:="C:\Documents And _ Settings\ehicks\Desktop\TrailProvisioningReport.doc")
WordObj.Visible = True
Set objWord = Nothing
End Sub
Unfortunately, still nothing happens when I click the Button. Any thoughts?
Private Sub Command1_Click()
res = ShellExecute(hWnd, vbNullString, "C:\Documents and Settings\ehicks\Desktop\TrailProvisioningReport.doc", vbNullString, vbNullString, vbNormalFocus)
If res = 31 Then
MsgBox "This file is not associated with a program!", vbCritical, "File Association Error"
Exit Sub
End If
End If
End Sub
TO
Private Sub Command2_Click() ' <----------- Changed
res = ShellExecute(hWnd, vbNullString, "C:\Documents and Settings\ehicks\Desktop\TrailProvisioningReport.doc", vbNullString, vbNullString, vbNormalFocus)
If res = 31 Then
MsgBox "This file is not associated with a program!", vbCritical, "File Association Error"
Exit Sub
End If
End If
End Sub
I don't think the sub is firing. You should get an error if Word isn't installed.
Put a breakpoint in the sub to see if it is firing and then step through the code.
or
As a test add the following line at the beginning of the sub:
msgbox "Sub Fired!"
My code will account for Word not being installed because I am checking for that return code. I think that sdraper is correct in his assumption that the event is not being fired.
Private Sub Command1_Click()
res = ShellExecute(hWnd, vbNullString, "C:\Documents and Settings\ehicks\Desktop\TrailProvisioningReport.doc", vbNullString, vbNullString, vbNormalFocus)
If res <> 33 Then ' a successful file open as far as i can tell
MsgBox "YOUR GENERIC ERROR CODE ETC", vbCritical, "File Error"
Exit Sub
End If
End Sub
I do have Word installed and have triple checked my path and it's correct.
Sam, I put the msgbox in the code, which now reads:
Private Sub cmdButton2_Click()
MsgBox "Sub Fired!"
Dim WordObj As Word.Application
Set WordObj = CreateObject("Word.Application")
Dim objWord As Word.Document
Set objWord = WordObj.Documents.Open(FileName:="C:\Documents And Settings\ehicks\Desktop\TrailProvisioningReport.doc")
WordObj.Visible = True
Set objWord = Nothing
End Sub
I tried your code as follows and at least something happened this time:
Private Sub Command2_Click()
res = ShellExecute(hWnd, vbNullString, "C:\Documents and Settings\ehicks\Desktop\TrailProvisioningReport.doc", vbNullString, vbNullString, vbNormalFocus)
If res <> 33 Then ' a successful file open as far as i can tell
MsgBox "YOUR GENERIC ERROR CODE ETC", vbCritical, "File Error"
Exit Sub
End If
End Sub
I got the compile error "Sub or Function not defined" and the ShellExecute was highlighted.
If you include the declarations from Swi's previous post at the top of your projects code it should work fine.
Here they are...
Option Explicit
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
Dim res as Long
Did you put the following under Option Explict at the top of the Form Code:
Option Explicit
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
Dim res as Long
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.