Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Search and copy text after finding an embedded tag

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Crisp - 27 Jan 2005 22:05 GMT
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
Crisp - 28 Jan 2005 01:21 GMT
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.
Klaus Linke - 28 Jan 2005 13:31 GMT
> I thought an example would be helpful
>
[quoted text clipped - 7 lines]
> 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
Crisp - 31 Jan 2005 19:47 GMT
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
}
Klaus Linke - 31 Jan 2005 21:07 GMT
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

> 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
[quoted text clipped - 51 lines]
> \par last text
> }

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.