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

Re: Copying fields from headers by Charles

Charles
Tue Oct 26 16:51:49 CDT 2004

Before inserting the second document, insert a section break and change the
headers/footers so that they are not "same as previous." Then insert your
second document into the second section of the new document.
http://addbalance.com/usersguide/sections.htm
--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

"Usha Vas" <UshaVas@discussions.microsoft.com> wrote in message
news:C0864F49-81E9-4E82-AF0E-65E8989D21FE@microsoft.com...
> 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
>
>
>