Printing word doc from excel

Craig1

Active Member
Joined
Jun 11, 2009
Messages
322
Hi Guys,

I have got this code working but I still have one little (hopefully) issue to resolve.
The code is:-

Private Sub CommandButton1_Click()
' Set a reference to Microsoft.Word X.0 Object Library
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim sPath As String
Dim sFile As String
Dim bPrinted As Boolean

sPath = "Z:\Engineering Costs & issue control\Line 2\"
sFile = Workbooks("Issue Control sheet for SMP's.xls").Worksheets(1).Range("B4").Value
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(sPath & sFile)
wdDoc.PrintOut Background:=False
wdDoc.Close False
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub


It works fine when the range is B4, I now need it to be more dynamic so as the worksheet info starts filling up the range will go down to C4, D4 etc.

Again Guys, any help is appreciated.

Craig.
<!-- / message -->
 
Sorry, I had misunderstood. To print just the last file in column B try

Code:
Private Sub CommandButton1_Click()
' Set a reference to Microsoft.Word X.0 Object Library
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim sPath As String
Dim sFile As String
Dim bPrinted As Boolean
Dim LR As Long, i As Long
Set wdApp = CreateObject("Word.Application")
sPath = "Z:\Engineering Costs & issue control\Line 2\"
With Workbooks("Issue Control sheet for SMP's.xls").Worksheets(1)
    LR = .Range("B" & Rows.Count).End(xlUp).Row
    sFile = .Range("B" & LR).Value
    Set wdDoc = wdApp.Documents.Open(sPath & sFile)
    wdDoc.PrintOut Background:=False
    wdDoc.Close False
End With
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thanks for that Peter it worked perfectly and thank you for your patience.
It is much appreciated.

I'll now post a different thread I posted this morning.

Craig
 
Upvote 0
Hi,
I tried this code and it didn't work. When I pressed "step into" it spotted an error in the first line: wdApp as Word.Application. It gives me a "Compile error:User-Defined type not defined.

Any thoughts?

Thanks
 
Upvote 0
In the VBE, Tools > References and tick Microsoft Word x.x Object Library. x.x is the version number of Word.
 
Upvote 0
Hi guys,

This worksheet has been working great for months and saves a great deal of time for the end user and creating a full log and issue control of the documents.
I have just come up with a little issue I need to resolve with the help of you guys.
Basically it looks in column B for the title of the doc from a drop down list. But some alterations to some of the documents were needed so the number has to change slightly so the VBA cannot find and print the doc because of the title change.
What I need is the code to find the title with the nearest wording or something similar.

e.g.
This is the original document.
Bead Prod SMP DRY L2 003.001 Disconnect or reconnect fines pipework under DE 602

After alterations it is now
Bead Prod SMP DRY L2 003.002 Disconnect or reconnect fines pipework under DE 602

Because of the alterations the issue control number is now 2.

If I'm at work I can modify the validation list but if not the end user gets an error.

Any ideas guys.

Craig.
 
Upvote 0
Hi Guys,

Another little issue has arisen.

The References and tick Microsoft Word x.x Object Library

now staes that

MISSING: Microsoft Word 14.0 Object Library

so it now won't find any word document. The only thing that has changed is another computer that has access to the same folder is now using Office 2010 instead of my Office 2003

Any ideas.

Craig.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,158
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top