Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a Table Caption using VBA

Status
Not open for further replies.

tyang01

Technical User
Feb 1, 2007
46
0
0
US
I was wondering if there was a method of deleting table captions or figures in vba? I've checked alot of sources and they only tell you how to add it in VBA. I'm trying to do this in MS Word 2003.

Thanks for the help!
 
Did you try the macro-recorder ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV;

Yeah I tried the recorder this is what I got.

Sub Macro1() "This is by pressing backspace."
'
' Macro1 Macro

'
Selection.TypeBackspace
End Sub

Sub Macro2()This is by pressing delete."
'
' Macro2 Macro

'
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

I was only able to do this with them highlighted already though.
 
It sounds like you're turning on the recorder once you are already in the Title field and it is only picking up you pressing [Backspace] or [Delete].

Start the macro recorder with the table NOT YET selected. Once you start recording, then click on the table, click on the title and delete it.

[tt][blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
I cannot select the table caption with the macro recorder on for some reason.
 
Hi tyang01,

Since you're deleting the caption, that's best done in conjunction with the deletion of the object to which the caption refers. If the object to be deleted is formatted in-line-with-text, you can do that by testing whether the caption is the next word. For example:
Code:
Sub DeleteCaptions()
Dim oCap As CaptionLabel
Dim iShp As InlineShape
Dim i As Integer
Dim TmpRng As Range
Dim TmpStr As String
For Each oCap In CaptionLabels
  TmpStr = TmpStr & CaptionLabels(oCap) & " "
Next
With ActiveDocument
  For Each iShp In .InlineShapes
    i = 0
    Set TmpRng = iShp.Range.Words.Last
    With TmpRng
      Do While Len(.Text) = 1
        i = i - 1
        .MoveEnd wdWord, 1
        .MoveStart wdWord, 1
      Loop
      MsgBox .Text
      If InStr(TmpStr, .Text) > 0 Then
        .MoveEnd wdWord, 1
        .MoveStart wdWord, i
        .Delete
      End If
    End With
  Next
End With
End Sub
Cheers

[MS MVP - Word]
 
Hold on here. There is stuff we do not know.

Normally, Table (or Figure) Captions are not objects. They are not Shapes, or InlineShapes. They are text (paragraphs) using the Caption style. Nothing more. They can be deleted by simply deleting paragraphs that use that style.

Conversely, it is possible to delete a table (or figure) that DOES have a caption...and the Caption will stay.

There is indeed a CaptionLabel collection, but there is NO direct connection to anything, really.

You can have "Table 1", "Table 2" as a CaptionLabel, but deleting the paragraph will not remove it from the collection. Conversely, you can remove it from the collection, and the text - "Table 1" - can still be in the document.

As it is, if they simply want to remove the paragraphs like:

Table 1
Figure 2

etc. Then - unless there is something wonky - the following should remove them.
Code:
Dim oPara As Paragraph
For each oPara in ActiveDocument.Paragraphs
   If oPara.Range.Style = "Caption" Then
      oPara.Range.Delete
   End If
Next

If the OP is talking about something else, then we need to know what that is.

Gerry
 
Thanks for the replies. I have yet to try these methods but hopefully I'll be able to give these a try a little later on today. Thanks a bunch guys I have been searching endlessly trying to find a way to do this. I will post whatever I find out.
 
Hi Gerry,

I agree there is a bunch of stuff we don't *know*. However, my response was aimed at showing what can be done.

Whilst I agree that it's a simple matter to delete all paragraphs formatted in the Caption Style, I chose to limit my code's action to just the inline shape and the caption name & #. To extend the code's action to the caption paragraph (assuming the Caption Style is in use and no non-caption text is present), it's a simple matter to insert:
If .Paragraphs(1).Style = "Caption" Then .MoveEnd wdParagraph, 1
before:
.MoveStart wdWord, i

Cheers


[MS MVP - Word]
 
Sorry macropod, I simply do not understand.

What is with the InlineShapes??? WHAT InlineShape? There has been no mention whatsoever of InlineShapes. The OP wrote:

"I was wondering if there was a method of deleting table captions or figures in vba? "

Where is there anything about an InlineShape? True, when one uses "figures", then it is edging towards InlineShapes, as most inserted figures are InlineShapes. But tables are not.

Even if you do insert a graphic, say, and it gets a caption "Figure 1", there is still no direct relationship to the InlineShape. You can select the InlineShape and delete it...the caption remains. You can insert text between the Caption and the InlineShape (or table for that matter). Say the caption is set for below the figure...

[InlineShape_A]
Figure 1

There is no problem putting other stuff between them.

[InlineShape_A]
Some text going into here, blah blah.
Figure 1

There is no problem putting another InlineShape(s) (or table) between a figure and its caption, or more than one.

[InlineShape_A]
Some text yadda yadda
[InlineShape_B]
Figure 2
Some more other text blah blah
[InlineShape_C]
Figure 3
Figure 1

In my mind there such a disconnection between Caption and its "owner" that I am very wary of them. For example, if you put ONE picture (Insert > Picture > From File...) into a document, and you have its set up for automatic caption you end up with (assuming caption is for "below"):

PICTURE
Figure 1

Let's suppose you have a bunch of paragraphs below the figure, for convenience, let's make them empty (<p>).

PICTURE
Figure 1
<p>
<p>
<p>
<p>
<p>

Now select PICTURE - an InlineShape. Drag it to the second <p>. What do you get?

Figure 1
<p>
PICTURE
Figure 2<p>
<p>
<p>
<p>

Now drag it again, to the third <p>.

Figure 1
<p>
Figure 2<p>
PICTURE
Figure 3<p>
<p>
<p>

And so on. It is quite possible - I just did it - to end up with:

Figure 1
<p>
Figure 2<p>
Figure 3<p>
<p>
Figure 4<p>
PICTURE
Figure 5<p>

FIVE Captions for ONE InlineShape. That is because the caption is actually text plus a SEQ field. Captions do not have any direct relationship to the object that generated them. Once a caption is generated, it is stand-alone.

But (I admit I am having a stupid day), I still do not understand the logic of your code re: InlineShape and
Code:
iShp.Range.Words.Last
It may - or may not - have anything to do with the last word.

Gerry
 
Hi Gerry,

I accept all of that but, as I made quite clear in my original post and in my previously reply to you, I was simply offerening a suggestion how how things might be done. I used InlineShapes as an example - the same principles can be applied to tables.

The only shapes that present a problem with the approach I suggested are of the non-inline type. And that's because shape anchor positions have no necessary relationship with whatever captions might exist. For those shapes, one would need to work out where the bottom of the shape is, then look below it for the caption. This could get tricky in multi-column documents.

As for 'Set TmpRng = iShp.Range.Words.Last', OK, '.Words.Last' is superfluous and the statement could be reduced to 'Set TmpRng = iShp.Range'. Similarly, 'MsgBox .Text' can be deleted - it was left over from testing.

Cheers

[MS MVP - Word]
 
And that's because shape anchor positions have no necessary relationship with whatever captions might exist. "

My point however is that InlineShapes have no relationship either. InlineShapes have no relationship with whatever captions that might exist.

My point is that tables, also, do not have a relationship with whatever captions that might exist.

Shapes, InlineShapes, Tables...none of them have a relationship to whatever captions that might exist.

You could have a table with its caption just above it (or below it...whatever). Both on page 4. You can move the Caption to page 137...and Word has NO idea of this, because there is no relationship - be it a Shape, a Table, or an InlineShape.

Gerry
 
Gerry,

I think you're being just a bit precious about the possibility of the caption being somewhere unrelated to whatever it's captioning.

In the real world, the caption is likely to be the first thing that appears in the document after whatever the caption refers to.

Cheers

[MS MVP - Word]
 
Perhaps, yes. But you are stating (or rather implying) a relationship that does not exist.

"about the possibility of the caption being somewhere unrelated to whatever it's captioning."

A caption anywhere, even right after the thing it is "captioning", is still actually unrelated. There is no relationship.

OK, I will drop it. I should know better.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top