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

opening word document 2

Status
Not open for further replies.

mattbold

Programmer
Oct 5, 2001
45
0
0
GB
good evening,

just a quick question.. can anyone tell me how to make a word document open from pressing a button on a form?
i can get word to open but i don't know how to make it open with a particular document...?

any help would be greatly appreciated!

thanks,
- matt
 
From the Office Automation Help file:

Create a Word Document
The following code creates a document and records its creation date.

Sub createDoc()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRng As Word.Range
Dim wordPara As Word.Paragraph

Set wordApp = CreateObject("Word.Application")

With WordApp

.WindowState = wdWindowStateMaximize
.Documents.Add
Set wordDoc = wordApp.ActiveDocument
Set wordRng = wordDoc.Range

With wordRng

.Font.Bold = True
.Font.Italic = True
.Font.Size = 16
.InsertAfter "Running Word Using Automation"
.InsertParagraphAfter

' insert a blank paragraph between the two paragraphs
.InsertParagraphAfter

End With

Set wordPara = wordRng.Paragraphs(3)

With wordPara.Range

.Bold = True
.Italic = False
.Font.Size = 12
.InsertAfter "Report Created: "
.Collapse Direction:=wdCollapseEnd
.InsertDateTime DateTimeFormat:="MM-DD-YY HH:MM:SS"

End With

.ActiveDocument.SaveAs "c:\My Documents\createDoc.Doc"
.Quit

End With

Set wordPara = Nothing
Set wordRng = Nothing
Set wordDoc = Nothing
Set wordApp = Nothing

End Sub
 
If all you want to do is open a Word doc, as opposed to creating one:
Public Sub OpenWordDoc(strDocName As String)
dim gobjWord As Word.Application
dim gobjDoc As Word.Document


'Set the directory for any letters generated
Const strDir As String = "U:\MyFolder"

strDocName = strDir & strDocName
'Open the letter in Word
On Error GoTo WordError

Set gobjWord = New Word.Application
gobjWord.Application.Visible = True
Set gobjDoc = gobjWord.Documents.Open(strDocName)
'Dim WordInstance As Object
'Make document visible for editing
gobjWord.Application.Visible = True
'reset the variables
Set gobjWord = Nothing
Set gobjDoc = Nothing
Exit Sub
WordError:
MsgBox "Err #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Word Error"
gobjWord.Quit

End Sub
 
hello,

yep thats the one, cheers it's working marvellously!

- matt
 
Great, I'm glad you got it worked out. And thanks for the star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top