ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Printing MS Word Doc From MS Excel (https://www.excelbanter.com/excel-discussion-misc-queries/17700-printing-ms-word-doc-ms-excel.html)

Anthony

Printing MS Word Doc From MS Excel
 
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

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

Anthony

Dave,

Blimey, think I follow and will give it a try,

Many thanks for your efforts

Anthony

"Dave Peterson" wrote:

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



All times are GMT +1. The time now is 05:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com