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!

Command Button to Open specific Word Document

Status
Not open for further replies.

Donny

Technical User
May 3, 2001
19
0
0
US
I need a command button that both opens word and opens the merge file i have already created. I want to avoid using macros if possible. Could someone send me the code for this.... Please
 
Display a word document based on variables in a form


Private Sub YourCommandButton_Click()

'Dim objects and variables
Dim objWord As Object
Dim File
'Set file path & name, stored in tblTitle
File = Me.txtFile 'This is on your form
'Make sure there's something there
'Launch word
Set objWord = New Word.Application
With objWord
'Open the file
.Documents.Open FileName:=Chr(34) & File & Chr(34) ' Note the chr(34) to prevent problems with ""
'Make it visible
.Visible = True
End With
'Maximize the window
objWord.Application.WindowState = wdWindowStateMaximize
End Sub

Tyrone Lumley
augerinn@gte.net
 
Ensure that you have the Word Library Referenced in your References (Open a Module in Design View, Click on Tools then References then Microsoft Word x.0 Library, and close the dialog box, the x.0 depends on which version of Word you have)

Dim wordobj As Word.Application
On Error GoTo ErrHandler
Set wordobj = GetObject(, "Word.Application")
wordobj.Application.Visible = True
wordobj.Documents.Open ("YourPath&FileName")
Exit Sub
ErrHandler:
Set wordobj = CreateObject("Word.Application")
Resume Next


PaulF
 
Than you, I dropped the code in but there appears to be a compile error. This is what i have so far, perhaps you could tell me what to modify??

Option Compare Database

Dim wordobj As Word.Application
On Error GoTo ErrHandler
Set wordobj = GetObject(, "Word.Application")
wordobj.Application.Visible = True
wordobj.Documents.Open ("I:\MGM\TQ&S\TIGS\WORKING\Grant\Database\Siteworks Database\siteworks request form.doc")
Exit Sub
ErrHandler:
Set wordobj = CreateObject("Word.Application")
Resume Next

Option Explicit

 
Option Explicit

comes before the code, and you need to attach the code to a control, or make it a sub, or function. This example ties it to the Click Event of a command button on a form. The name of the command button is cmdOpenMyWordDoc


Option Compare Database
Option Explicit

Private Sub cmdOpenMyWordDoc_Click()
Dim wordobj As Word.Application
On Error GoTo ErrHandler
Set wordobj = GetObject(, "Word.Application")
wordobj.Application.Visible = True
wordobj.Documents.Open ("I:\MGM\TQ&S\TIGS\WORKING\Grant\Database\Siteworks Database\siteworks request form.doc")
Exit Sub
ErrHandler:
Set wordobj = CreateObject("Word.Application")
Resume Next
End Sub

PaulF
 
PaulF

Your a deadset legend,thankyou, works perfectly.

Donny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top