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
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
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
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.