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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open a MS Word File with VB 6.0

Status
Not open for further replies.

mileseve1

Technical User
Oct 12, 2003
9
0
0
US
Hi All,

I am writing a program that will open a specific MS Word file based upon answers to a series of questions. I tried several different ways and only succeeded in opening the Word application and not the file. Any suggestions would be greatly appreciated.

Thank you
 
Use shell

dim myshell as long
myshell = shell("Path of winword.exe path of file")

I hope this will help.
Good luck

Vikram
 
Try this:

Set wordapp = New Word.Application
Set worddoc = wordapp.Documents.Open(docpath & "\" & docname)
'do your bit
'
worddoc.Close (wdDoNotSaveChanges)
wordapp.Quit
Set wordapp = Nothing


You will find all your definitions in Object Browser by selecting the Word library from the Object browser dropdown

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
include winword.exe in your destination
and fire the file using shell command

or include the following in your module
Public Declare Function WinExec Lib "kernel32" (ByVal lpcmdline As String, ByVal cCmdshow As Long) As Long

then use winexec to fire winword.exe
using vb code

fn = App.Path & "\Your file name"
WinExec App.Path & "\winword.EXE " & fn & "", 3
 
Thanks all.

However, I still can't get it to work. I will open the msword but not the specific file. I am attaching my code, please tell me what I am doing wrong.

Thank you

Private Sub cmdenter_Click()
If Check1.Value = 1 And Check3.Value = 1 And Check5.Value = 1 Then
MsgBox "Based on the information you supplied, this is a catergory one Mutual Non-disclosure agreement"
Dim myshell As Long
'myshell = Shell("C:\Program Files\Microsoft Office\Office10\WINWORD")
myshell = Shell("c:\Documents and Settings\user\My Documents\IS535\sampl mnda.doc")

Else
MsgBox "Based on your answers this is a catergory two issue. Please consult legal"
End If
End Sub
 
A) You have commented out your opening of word.
B) Use johnwm's code method (I have a code piece below of similar nature). The shell method is sloppy and will screw up if MS decides to change anything upon Word's installation.
C) Looks like a legal document printing, so correct 'category' (don't want you to look bad)

D) Here is some code that should do the trick:

' This code was written by BBurke; GTC, Inc.,
' please modify to suit your needs.
' Do not claim it as your own.


'Use these lines to OPEN the document along with Word.
'###
Dim appWord As Word.Application
Dim docWord As Word.Document

Set appWord = CreateObject("Word.Application")
Set docWord = appWord.Documents.Open(YourPathHere)
'###


'Use these lines to CLOSE the document along with Word.
'###
' Close the Word Document
docWord.Close
' Close the Word Application
appWord.Quit

' Clear out variables, prevent memory leak.
Set docToPrint = Nothing
Set appWord = Nothing
'##

With these method types (johnwm's and my own), you must put a reference to Word inside of your project. You can do this through the Project menu, References menu item.

Hope that helps you out.
 
You need to pass the full path to the executable, followed by the full path to the document:

myshell = Shell("C:\Program Files\Microsoft Office\Office10\WINWORD c:\Documents and Settings\user\My Documents\IS535\sampl mnda.doc")

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thank you all!

I got it open the file but it keeps giving me a type mismatch error. Not sure which part of my code is causing this error, but I will keep debugging until I get it right. Again, thank you all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top