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!

Command button to open a word document? 4

Status
Not open for further replies.

KraigC

Technical User
Mar 4, 2006
20
0
0
US
I am trying to set up a command button to open a word document is this possible? or what else could i use.
Thanks Kraig
 

This is from some code I use - I stripped out the code that takes info from the current record displayed on a form to populate the word.doc . hope it helps.



Private Sub CmdOpenWordDoc_Click()

On Error GoTo ErrorHandler

Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim strDate As String

Dim Obgor As String

Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)

Rem OPTIONAL - change path from user template dir and force to a controlled location if there is a special template required:
strTemplateDir = "C:\path\"

strLetter = strTemplateDir & "yourtemplatename.dot"

Set docs = appWord.Documents
docs.Add strLetter

With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
.ActiveDocument.Protect wdAllowOnlyFormFields
End With


ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If

End Sub
 
My last post might not work for you - Actually this is a bit cleaner and less confusing - can open a new blank word doc (point to the template name.dot) or an existing doc:

Dim oApp As Object
Dim oDoc As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
Set oDoc = oApp.Documents.Open("full path to file or template you want to open"
 
Why this hasslte ? when there is wizard to help you.

Use command button wizard!!!!

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Will the command button wizard open a specific Word document?

 
I have tried the word wizard but it just opens word itself not the form. And I have also tried assigning a hyperlink address the form opens only after the hyperlink warning. can I get that to not show the warning? Thanks all for helping. Kraig
 
What is your actual code ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I used Trollbro's second code and it worked fine? To add to this topic, Is there anyway of opening a form in Read-Only mode??

Thanks,
Chris.
 
When I said 'form', I meant Word document. Sorry for the confusion.

Cheers,
Chris.
 
yourWordApp.Documents.Open FileName:="\path\to\file.doc", ReadOnly:=True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top