Copy information between style

J

Jaypee

I have an existing word document and I want to lift all the data which does
not have the style "Heading 1" and paste it into a new document.

This works fine by iterating thru the document and checking for
Paragraph.Style

the problem right now is that it converts tables into text, each field is
converted as a single paragraph.

Is there something I can do to work around this? thanks
 
J

Jaypee

Here is the code I am using... breaks when it encounters tables :(

Sub CopyData()
Dim docSource As Document
Dim docDest As Document
Dim Paragraph As Paragraph
Dim tmpName As String


Set docSource = ActiveDocument
Set docDest = Documents.Add


For Each Paragraph In docSource.Paragraphs
docSource.Activate
If Paragraph.Style <> docSource.Styles(wdStyleHeading1) Then
Paragraph.Range.Select
Paragraph.Range.Copy

docDest.Activate
Selection.Paste
Selection.EndKey unit:=wdLine
End If
Next

Set docSource = Nothing
Set docDest = Nothing
End Sub
 
D

Dave Lett

Hi,

I think you might be making this more difficult (and longer) than it needs
to be:

With Selection
.HomeKey Unit:=wdStory, Extend:=wdMove
With .Find
.ClearFormatting
.Text = ""
.Style = "Heading 1"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End With
ActActiveDocument.SaveAs _
FileName:=ActiveDocument.Path & _
Replace(ActiveDocument.Name, ".doc", "_NoHeading1.doc")


This routine replaces any "Heading 1" formatted paragraph with ... nothing.
That is, it removes all the "Heading 1" paragraphs. The last line of the
routine saves the document

HTH,
Dave
 
J

Jaypee

Thanks Dave, this is an elegant solution to the problem I originally portrayed.

Unfortunately, I did not elaborate the entire scenario I am dealing with....

I have one master document and several source documents.. for all comparison
purposes, they have the same number of Sections. Sections are differentiated
by the headers (having "Heading 1" style).

That is the reason I was using the paragraph style to lift out the
paragraphs in between -- the next step was to paste it in the corresponding
section.

Anyway, thanks... given the lack of context I provided, the solution you
gave was excellent.
 

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