Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use vbscript to copy a header from one Word document to another

459 views
Skip to first unread message

Marc B. Hankin

unread,
Feb 16, 2005, 10:16:43 AM2/16/05
to
I'm having trouble figuring out how to use vbscript to copy a header from
one Word document to another.
The code below generates an error message telling me that the requested
object doesn't
exist i.e., the error is produced by this line:
Set objSourcRange =
objSourcDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.text

However, the document does have two sections, and each section does have a
header. So, I'm stumped.

Any suggestions (preferably with explanation, or a clue where I can learn
what I'm doing wrong, etc.)
would be much appreciated.
Thanks, marceepoo

Here's the code:

Dim sSourcDocFulnam, sTargetDocFulnam
sSourcDocFulnam = "C:\Apps\test.speedText.doc.dot"
sTargetDocFulnam = "C:\Apps\New Microsoft Word Document.doc"
'
Set objWord = CreateObject("Word.Application")
'
Set objSourcDoc = objWord.Documents.Open(sSourcDocFulnam)
Set objSourcRange =
objSourcDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.text
'
Set objTargetDoc = objWord.Documents.Open(sTargetDocFulnam)
Set objTargetRange =
objTargetDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.text
'
objTargetRange = objSourcRange


y sakuda

unread,
Feb 16, 2005, 10:59:00 AM2/16/05
to
"Marc B. Hankin" <mbh...@aol.com> wrote in message news:%23PYNHqD...@TK2MSFTNGP09.phx.gbl...

> I'm having trouble figuring out how to use vbscript to copy a header from
> one Word document to another.
> The code below generates an error message telling me that the requested
> object doesn't
> exist i.e., the error is produced by this line:
> Set objSourcRange =
> objSourcDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.text
>
First. In VBS wdHeaderFooterPrimary denote literal. You must define constant.
Second. Range.Text is not object.
Modify script like this.

set bjSourcRange = objSourcDoc.Sections(1).Headers(1).Range
wscript.echo bjSourcRange.Text

Y Sakuda from JPN

0 new messages