I put a commandbutton from the Control Toolbox toolbar on a worksheet.
This is the code I used (double click on that commandbutton and paste this
code):
Option Explicit
Private Sub CommandButton1_Click()
'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String
myDocName = "C:\my documents\word\document1.doc"
testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If
WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If
WDApp.Visible = True 'at least for testing!
Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False
If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If
Set WDDoc = Nothing
Set WDApp = Nothing
End Sub
Adjust the commandbutton1 name and the drive/folder/name of the .doc file.
Anthony wrote:
Hi,
I have a workbook in Excel where I want to be able to
place a button that will print a letter that has been
created in a seperate MS Word documnet.
I have tried recording a macro but nothing happened.
Is there a way to link the two together to enable
this 'button' to produce the letter ??
Thanks
Anthony
--
Dave Peterson
|