Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO DISPLAY A WORD DOCUMENT FROM VB6 5

Status
Not open for further replies.

Hiccup

Programmer
Jan 15, 2003
266
US
Can someone tell me the VB6 code for a Command Button so that when it is clicked it will display a Microsoft Word document.

Thanks in advance!
 
You could use ShellExecute:


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

Swi
 
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

Do either of you know what I'm doing wrong?
 
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
 
Thanks Sam, I'll give it a try and let you know what results.
 
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?
 
One question...

Do you have Word installed, or are you trying to bypass word and use vb to open and display the document manually?

You have to have Word installed for the above examples to work right...

Otherwise, good luck trying to figure out the Word File Format...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
You said you used my code but you need to change:

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

Swi
 
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!"

This will tell you if the sub is firing

Sam
 
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.

Swi
 
Hi Hiccup,

Are you sure the path for the document is correct???

I tried Swi's code and pointed it to a document that doesn't exist and it displays the same behaviour (ie appears to do nothing) as you experienced.

Hope this helps

Harleyquinn
 
You could try something along these lines...

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

That worked for me.

Harleyquinn

 
Gentlemen, thanks for all the help!

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

Still nothing happens when I click Command2.?????
 
Harleyquinn,

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.

Any thoughts anyone??

Thanks in advance!
 
Hiccup,

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

Cheers

Harleyquinn
 
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

Swi
 
Swi - That did it! Mant thanks and stars to all of you!!

Best,

Ed Hicks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top