Copying fields from headers

U

Usha Vas

Sorry if this question has been asked before, but I searched through this
newsgroup and found different variations but not quite....

I have 2 documents and am trying to create a 3rd from the first 2. 1st
document has a header with some fields and text (like page number, date and
company info) followed by the content, and 2nd document just has content (no
header). I create the 3rd document by inserting second document at the end of
the first and saving it as the 3rd document. However, I can't seem to get the
fields of the header from the first document into the 3rd document.


Doing the following:

Dim Text As String = objDocument1.Sections(1).Headers(1).Range.Text
objDocument3.Sections(1).Headers(1).Range.Text = Text

only copies the text, and the field values are copied as text from the first
document, so on all pages, I get 1 of 1 ({PageNumber} of {NumPages}).

Is there a way I can copy the field like PageNumber and Date over to the 3rd
document and then update them?

I need to do the update after the 3rd document is created, since I need the
header on the pages of the 2nd document as well.

TIA,

Usha

Here is the code:

Dim myApp As Word.Application = New Word.Application
myApp.Application.Visible = False
Dim objDocument1 As Word.Document =
myApp.Documents.Open("c:\doc1.doc", ReadOnly:=True)

With myApp.Selection.Find
.ClearFormatting()
.Text = "<!firstname!>"
With .Replacement
.ClearFormatting()
.Text = "Usha"
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

With myApp.Selection.Find
.ClearFormatting()
.Text = "<!lastname!>"
With .Replacement
.ClearFormatting()
.Text = "Vas"
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

Dim objDocument3 As Word.Document = myApp.Documents.Add()

Dim Range1 As Word.Range = objDocument1.Range.FormattedText

Dim Range3 As Word.Range = objDocument3.Range
Dim Selection3 As Word.Selection = objDocument3.ActiveWindow.Selection

Range3.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Range3.FormattedText = Range1.FormattedText
Range3.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Selection3.EndOf(Unit:=Word.WdUnits.wdStory,
Extend:=Word.WdMovementType.wdMove)

With Selection3
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.InsertFile(FileName:="c:\doc2.doc", ConfirmConversions:=False)
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
End With

Dim Text As String = objDocument1.Sections(1).Headers(1).Range.Text
objDocument3.Sections(1).Headers(1).Range.Text = Text

objDocument3.SaveAs("c:\Development\PreAuth\docs\doc3.doc")

objDocument1.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
objDocument3.Close()

objDocument1 = Nothing
objDocument3 = Nothing
myApp = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top