selecting field codes and copying and pasting them

C

CatMarieS

I have been trying to make a simple macro so that I can copy certain field
codes from a document and paste them by themselves into a new document so I
can look at them. When I record the macro, things work out fine as I do
them: I first search to highlight the term "^d SET" (the SET is part of the
code I want) then copy and paste it into a new document. It works fine when
I do it, but the macro doesn't want to run. Here's the macro:

Sub Macro1()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^d SET"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
End Sub

I've been doing some research and it appears that the problem is that I need
to use "expression" in the place of "selection", but merely replacing the
term doesn't help.

In another approach to the problem - is there a way to convert field codes
into plain text? If I did that, then I wouldn't need to worry about
selection expressions vs. selecting normal text.
 
H

Helmut Weber

Hi CatMarie,

Sub Macro18()
Dim Dcm1 As Document
Dim Dcm2 As Document
Dim oFld As Field
Dim sTmp As String
Set Dcm1 = ActiveDocument
Set Dcm2 = Documents.Add
For Each oFld In Dcm1.Fields
sTmp = Trim(oFld.Code)
Dcm2.Range.InsertAfter sTmp & vbCr
Next
' ....
End Sub

You may check for the field-type in addition, like
if ofld.Type =wdFieldSet then ...

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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