On Wed, 8 Dec 2004 17:59:02 -0800, "Scott VA" <Scott
I am trying to write a macro in WORD to select all text of a particular
style,, copy it and paste it into a new document. I can do it manually, but
the record macro will not recognize either the "select all" using the Style
pane or using the "highlight all'" in the find dialog box. Any help is
apprecaited.
Hi Scott,
Selecting and manipulating multiple areas of text is one of many
recent features that have never been supported in VBA. There is simply
no way to program a macro to do what you can do manually.
However, you can program a macro that accomplishes the same final
result by copying one piece at a time in a loop, like this:
Sub CopyAllBodyText()
Dim NewDoc As Document
Dim SrcRg As Range, DestRg As Range
Set SrcRg = ActiveDocument.Range
Set NewDoc = Documents.Add
With SrcRg.Find
.ClearFormatting
.Text = ""
.Format = True
.Style = ActiveDocument.Styles("Body Text")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
'when a piece of Body Text is found,
'SrcRg covers its range
Set DestRg = NewDoc.Range
DestRg.Collapse wdCollapseEnd
DestRg.FormattedText = SrcRg.FormattedText
SrcRg.Collapse wdCollapseEnd
Loop
End With
NewDoc.Save 'will display Save dialog
Set SrcRg = Nothing
Set DestRg = Nothing
Set NewDoc = Nothing
End Sub
Also notice how the piece of text is transferred between the two
ranges by assigning the .FormattedText property. This doesn't use the
clipboard, so it doesn't zap anything the user may already have put
there, plus it's faster.