Printing word doc from excel, but the header is not printed

Mick17

New Member
Joined
Dec 1, 2016
Messages
7
Hi everyone,

I have several word documents named 001 002 003....... 999. I need to print a selection of all these documents. From the beginning till the word END is found. When I print these documents I want to place some information in the header.
I have written the code below. it does what i want, it prints the selection of the needed documents. the header is created. But the header is not on the printed documents. Any idea where i go wrong?

Sub printdocs()




Dim i As Long, wdApp As Object, wdDoc As Object, wdRng As Object


Set wdApp = CreateObject("Word.Application")
With wdApp
.Visible = True
For i = 1 To 2
Set wdDoc = .Documents.Open("\\path" & Format(i, "000") & ".docx")
With wdDoc
.Sections(1).Headers(1).Range.Text = "mytext"
Set wdRng = .Range(0, 0)
With .Range
With .Find
.Text = "END"
.Forward = True
.MatchWholeWord = True
.MatchCase = True
.Execute
End With
If .Find.found = True Then
wdRng.End = .Duplicate.Start
wdRng.Select
wdDoc.PrintOut , Range:=1
End If
End With
.Sections(1).Headers(1).Range.Text = ""
.Close False
End With
Next
.Quit
End With
Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing


End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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