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

From: Marc B. Hankin (mbh2010_at_aol.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 07:16:43 -0800

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



Relevant Pages

  • Re: How to use vbscript to copy a header from one Word document to another
    ... > 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 ...
    (microsoft.public.scripting.vbscript)
  • Re: Error when Pasteing a word table into visio 2007
    ... The letterhead is in the header of the word document and is ... visio drawing but get an error message. ...
    (microsoft.public.visio.general)
  • Re: dsofile CustomProperties in document header
    ... fields in the header and footer area. ... Every time a user views a document they will have to switch in ... word document is opened up has a few problems and the task to install it ... word-automation in the server is something that Microsoft strongly recommends ...
    (microsoft.public.office.developer.automation)
  • Re: Confusing POST behavior -- doing it twice?
    ... The header you set will just do that: ... > 1 - When I start, there is no error message. ... Some just fetch the HTML from cache and display that. ... I have no idea how the current mostly used browser behave. ...
    (comp.lang.php)
  • Re: resuming swsusp twice
    ... it should only be an error message, ... the console was left on the swsusp VT ... [snip severe ext3 damage] ... it would verify that the header and the image matched, ...
    (Linux-Kernel)