Macro for selecting text and a diagram that follows it

K

KGollan

I am a beginner with macros, and I am struggling to solve a Word problem with
a macro.

Essentially, I begin with a generated document (from a UML modelling
package)that contains text describing a diagram, and then a diagram. This
document contains around 150 diagrams, structured as above.

I have to write a macro that will find particular types of diagrams (using
the text which is above each diagram) and delete them from the Word document.

Can anyone advise me on the best way to achieve this?

Many thanks,
Katy
 
D

Doug Robbins - Word MVP

It depends a bit on how the diagrams are inserted into the document. That
is, whether they are Shapes or InLineShapes.

But basically, you would use an .Find loop to find the text, then set a
Range to the found text, extend the Range.End to the end of the Range of the
document and then delete the first Shape or InlineShape in that range. The
code would be something like:

Dim myrange As Range
Dim strFind As String
strFind = "Text to find"
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:=strFind, Forward:=True, _
MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindStop) =
True
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.ShapeRange(1).Delete
'or myrange.InlineShapes(1).delete
'Now delete the found paragraph
myrange.Paragraphs(1).Range.Delete
Loop
End With
Next i




--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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