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

Open up a word document 1

Status
Not open for further replies.

Cballe

Programmer
Apr 14, 2002
16
ZA
Hi guys,

Can someone help me with the following:

How do one open up a word document in a Visual Basic?

For example: click on a button and open up a word document
say for instance the document`s name is example.doc.

Thanks guys
Best Regards
Cballe
 
Dim Test As Object

' If Word isn't open VB will raise an error
' so this statement takes care of that
On Error Resume Next

' Check if Word is open
Set Test = GetObject(, "Word.application")

' If Word is not open
If Test Is Nothing Then
Set Test = CreateObject("Word.Application")
Test.Visible = False
End If

' If Word is open
If Not Test Is Nothing Then
' This is the location of the template I created
Test.WindowState = 2
Test.Documents.Add "D:\Example.doc"
End If

' ***************** DO STUFF *****************

' I didn't save the document you can if you
' want to, just remove DoNot
Test.ActiveDocument.Close wdDoNotSaveChanges
Word.Application.Quit wdDoNotSaveChanges
' Remove word from memory
Set Test = Nothing

This should get you started.
 
I forgot to mention that you will need to add Microsoft Word 8.0 Object Library in your References for this to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top