Deleting all Instances of Captions

D

dcuthill

I have tried the attached which I had found on a recent search of this group
but it does not seem to delete all the captions but rather hangs up at "for
each acap in activedocument" stating that this object does not support this
property or method.

I am trying to delete all inline shapes and their corresponding captions
from a document - I have figured out the inline shape bit but the caption
part is a problem.

Any ideas would be welcome.


Dim acap As CaptionLabel
For Each acap In ActiveDocument
acap.Delete
Next acap


Thanks


David
 
J

Jay Freedman

Hi David,

Even if you fix that code (by changing ActiveDocument to
CaptionLabels), it won't do what you're trying to do. A CaptionLabel
is *not* the caption of a shape. It's the kind of label, such as
"Figure" or "Table" -- any one of the items that appear in the Label
dropdown of the Insert > Caption dialog.

Once you insert a caption in a document, it's simply a paragraph that
has Caption style applied to it, just like a heading is a paragraph
that has Heading 1 style. There is no collection of "caption" objects.
To remove all such paragraphs from the active document, use a
Find/Replace like this one:

Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Format = True
.Forward = True
.Text = ""
.Style = ActiveDocument.Styles("Caption")
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
 
G

Greg Maxey

Jay,

I have been struggling with this one for hours. I tried your code and while
it remove "Caption" style in the main text, it is not working with captions
applied to inline shapes. Captions applied to inline shapes are contain in
text boxes and your search code is not including the text boxes. I think is
has something to do with storyranges, but I haven't figured it out yet.
 
G

Greg Maxey

Jay,

I think I have it solved. Extending your style approach:

Sub ScratchMacro()
Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
If oShp.TextFrame.TextRange.Style = ActiveDocument.Styles("Caption")
Then
oShp.Delete
End If
Next oShp
End Sub
 
J

Jay Freedman

Hi Greg,

When I apply a caption to an *inline* shape (which David specified),
the caption is just plain text with Caption style.

When I apply one to a *floating* shape, then I get a Caption paragraph
in a text box. To remove those, you'd have to do something like this,
which removes both the caption and its text box:

Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
With oShp
If (.Type = msoTextBox) Then
If (.TextFrame.TextRange.Style = _
ActiveDocument.Styles("Caption")) Then
.Delete
End If
End If
End With
Next oShp
 
G

Greg Maxey

Jay,

Once again our posts crossed. You are right, I was experimenting with
floating vice inline shapes. I had been working on the delete textbox bit
but couldn't figure out how to delete only textboxes with captions until I
saw your first suggestion using styles. Since the Caption is inserted in a
Textbox to start with, I figured I would just by pass the If msoTextbox bit.

Thanks.
 
J

Jay Freedman

Jay,

Once again our posts crossed. You are right, I was experimenting with
floating vice inline shapes. I had been working on the delete textbox bit
but couldn't figure out how to delete only textboxes with captions until I
saw your first suggestion using styles. Since the Caption is inserted in a
Textbox to start with, I figured I would just by pass the If msoTextbox bit.

Thanks.

I think you need to keep the If msoTextbox bit, depending on what
you're doing.

If you want to get rid of all floating shapes and any captions they
might have, and you know there aren't any textboxes that aren't
captions, then all you need is to zap all Shapes --

Dim oShp As Shape
For Each oShp In ActiveDocument.Shapes
oShp.Delete
Next oShp

But if you need to leave some Shapes in place, the problem is that
(quoting the VBA help) "Some shapes don't support attached text
(lines, freeforms, pictures, and OLE objects, for example). If you
attempt to return or set properties that control text in a text frame
for those objects, an error occurs." By wrapping the style test within
the If msoTextbox test, you prevent the error. The alternative is a
potentially difficult error handler.
 
D

dcuthill

Thank you both for your help on this - I was able to get what I needed to do
from Jay's initial response.

David
 

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