Search and copy text after finding an embedded tag

C

Crisp

I am trying to insert a visio diagram into a Word document via a macro.
The diagram file name follows a tag we created to identify it.
Ideally I would like to locate the tag, delete the tag, select the file name
that followed it and use it to open the file and insert it in place.
Most of the find, and find-replace examples I have found don't address the
current character postion. I have tried using the movestart & moveend
methods, but they don't appear to work in the context of a find object
searching a whole document.
If someone could just show me how to select text after I find something
else, I would be happy
 
C

Crisp

I thought an example would be helpful

before the macro...

....some text...
<insertDocumentTag>somefilename.vsd<\>
....some text

After the macro urns the tag and file name would be replaced by the inserted
diagram named in the tag value. Ideally this would be in a loop to process
all insertions.
 
K

Klaus Linke

Crisp said:
I thought an example would be helpful

before the macro...

...some text...
<insertDocumentTag>somefilename.vsd<\>
...some text

After the macro urns the tag and file name would be replaced by the inserted
diagram named in the tag value. Ideally this would be in a loop to process
all insertions.


Hi Crisp,

You can locate the tagged filenames with a wildcard search:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\<insertDocumentTag\>[!\<]@\<\\\>"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
While Selection.Find.Execute
MsgBox "tags found here"
' do something with the found tag
Wend

In case it's a forward slash in the end tag (</> instead of <\>), change \<\\\>
to \</\>

Then, you can find the filename from the selected text:
Dim posStart As Long
Dim posEnd As Long
Dim oldSelection as Range
' in case you need the selection, including tags, later:
Set oldSelection=Selection.Range.Duplicate
posStart = InStr(2, Selection.Text, ">")
posEnd = InStr(posStart, Selection.Text, "<") - 1
ActiveDocument.Range(Selection.start + posStart, _
Selection.start + posEnd).Select
MsgBox Selection.Text, , "File name:"

I don't have Visio installed... Perhaps you can record inserting a Visio file to
get the code for inserting it?

Greetings,
Klaus
 
C

Crisp

Thanks, Klaus - I almost got it working, but am still having some problems.
I had to change the wild cards a bit, but this code can now identify the
whole tag and parse out the file name...

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\<insertDocument\>*\<\/\>"
.Replacement.Text = "tag removed "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True

End With

While Selection.Find.Execute

' in case you need the selection, including tags, later:
Set oldSelection = Selection.Range.Duplicate
posStart = InStr(2, Selection.Text, ">")
posEnd = InStr(posStart, Selection.Text, "<") - 1
ActiveDocument.Range(Selection.Start + posStart, _
Selection.Start + posEnd).Select
documentname = Selection.Text
MsgBox documentname, , "Insert tag found"
Wend

The document name is correct. However, the replacement does not appeart to
work, plus, it only finds the first occurance of the tag and appears to stop.

1) How do I get the replacement to work? (eventually I will replace with
blank)
2) How do I get it to process all tags found?

Here is the sample data...

{\rtf1
\outlinelevel0 \b\f1\fs32 First Heading \b0
\par
\par
\outlinelevel1 \b\f2\fs28\ul Second Heading\b0\ul0
\par
\par \outlinelevel2 \b\f3\fs26 Third Heading \b0
\par
\par\fs20\outlinelevel
Just text
\par
<insertDocument>DiagramOneTest.vsd</>
\par
\par some more text
\par and yet more text
\par
<insertDocument>diagramTwoTest.vsd</>
\par last text
}
 
K

Klaus Linke

Hi Cris,

You need a "Selection.Collapse(wdCollapseEnd)" right before the "Wend", so you
don't match the same text again and again.

The code as it is also doesn't do any replacement.
If you want to delete the Visio file name, you can set Selection.Text="" (...
where it's currently displaying the MsgBox).

As I said, I don't know the code for inserting a Visio file at that point, given
its file name. Maybe you can figure out something from the macro recorder.

{\rtf1
\outlinelevel0 \b\f1\fs32 First Heading \b0

Did the text paste as RTF only in your newsgroup post, or do you edit RTF files
directly by opening them as text files?
If the latter, it's probably a risky business for this kind of thing.
You'd need the replacement text (inserted Visio file) exactly right, else you
easily corrupt the document.

Greetings,
Klaus
 

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