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
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