PDA

View Full Version : [SOLVED:] Copy word content excluding header and footer



anandbabu65
10-11-2011, 06:08 AM
Hi,

I have to copy all the contents in the active word document excluding the header and footer. Could anyone provide the vbscript for the same.

Frosty
10-12-2011, 10:47 AM
There are at least two good approaches to this problem:

1. Copy the contents of each section into a new document, without including the section break (so that you use the header/footer info of the new document). Where this breaks is if you want to retain some section info (like columns, landscape formatting, specific page trays to print to, etc), but don't want the header/footer info

2. Copy the entire document, and then go through and delete all the header/footer info (this breaks down if you're trying to also get rid of stuff in #1, or if you're specifically trying to avoid document corruption, which can often be hidden in section breaks), or you want to retain header/footer info in the destination document.

Here's some sample code for the #1 methodology. There is a lot of code out there for #2... but it would probably help to be a bit more specific on *why* you want to do what you want to do.



Sub CopyEverythingButSectionBreaks()
Dim oDocSource As Document
Dim oDocDestination As Document
Dim oSec As Section
Dim rngCopy As Range
Dim rngPaste As Range
Set oDocSource = ActiveDocument
Set oDocDestination = Documents.Add
For Each oSec In oDocSource.Sections
Set rngCopy = oSec.Range
'adjust to avoid the section break (which contains header/footer info)
rngCopy.MoveEnd wdCharacter, -1
rngCopy.Copy
Set rngPaste = oDocDestination.Content
rngPaste.Collapse
rngPaste.Paste
Next
End Sub

anandbabu65
10-13-2011, 02:05 AM
There are at least two good approaches to this problem:

1. Copy the contents of each section into a new document, without including the section break (so that you use the header/footer info of the new document). Where this breaks is if you want to retain some section info (like columns, landscape formatting, specific page trays to print to, etc), but don't want the header/footer info

2. Copy the entire document, and then go through and delete all the header/footer info (this breaks down if you're trying to also get rid of stuff in #1, or if you're specifically trying to avoid document corruption, which can often be hidden in section breaks), or you want to retain header/footer info in the destination document.

Here's some sample code for the #1 methodology. There is a lot of code out there for #2... but it would probably help to be a bit more specific on *why* you want to do what you want to do.



Sub CopyEverythingButSectionBreaks()
Dim oDocSource As Document
Dim oDocDestination As Document
Dim oSec As Section
Dim rngCopy As Range
Dim rngPaste As Range
Set oDocSource = ActiveDocument
Set oDocDestination = Documents.Add
For Each oSec In oDocSource.Sections
Set rngCopy = oSec.Range
'adjust to avoid the section break (which contains header/footer info)
rngCopy.MoveEnd wdCharacter, -1
rngCopy.Copy
Set rngPaste = oDocDestination.Content
rngPaste.Collapse
rngPaste.Paste
Next
End Sub

Hi Frosty,

Thanks for the reply.

Regarding my requirement, I have to copy all the contents from 1st word document to 2nd. If the 2nd word document is empty, I observed that the header and footer from 1st document was copied to the 2nd. I need to avoid this scenario. I was using Selection.wholeStory to select the contents.

Frosty
10-13-2011, 07:48 AM
Why don't you try the code I posted, and specify why it doesn't work?

Also, try to reply to my two scenarios. I don't have any more information now than I had from your first post. You don't want header/footer info... but I don't know if you have multiple section documents or not, if you want to retain some "section" level formatting or not, etc.

If you're just looking for code to copy the entire document except for the last paragraph, it will be...

[Code]
Sub SelectAllButLastParagraph
Dim rngWhere
Set rngWhere = ActiveDocument.Content
rngWhere.MoveEnd wdCharacter, -1
rngWhere.Select
End Sub
[Code]
If you manually copy the above code, and then manually paste into whatever document you want... does it work or not?

anandbabu65
10-14-2011, 04:00 AM
Hi,

Thanks for the response. The changes work for me

macropod
10-15-2011, 06:01 AM
Since your goal seems to be to simply replicate a document without the headers & footers, why not simply delete all headers & footers, then save the file under the new name?

anandbabu65
10-17-2011, 01:47 AM
That could be another option. Thanks for the reply

anandbabu65
10-17-2011, 03:10 AM
There were 2 possible solutions discussed.