Word VBA, Removing Picture Captions
In this article I will explain how you can remove caption from all the images in a document.
As explained in the article Word VBA, Loop Through Images there are basically 2 types of images:
- Floating
- Inline
The 2 image types have different types of caption and therefore require 2 different types of macros to remove their caption.
Remove Caption From Floating Pictures:
Assume we have the following images with caption in our document:
The caption used for floating images are text boxes. What we need is a macro that will search all text boxes in the document. If the text box starts with the word “Figure” then it is probably a caption and it must be deleted:
Sub Example()
'number of shapes in document
Dim intCount As Integer
Dim i As Integer
'number of deleted captions
Dim intDeleted As Integer
'the text in the caption
Dim strText As String
intCount = Shapes.Count
intDeleted = 0
'loops through all the shapes in the document
For i = 1 To intCount
'checks if the current shape is a text box
If Shapes.Item(i - intDeleted).Type = _
msoTextBox Then
Shapes.Item(i - intDeleted).Select
'gets the text in the text box
strText = Selection.Range
'checks if the text box is a caption
If Strings.Left(strText, 6) = "Figure" Then
'removes the text box
Shapes.Item(i - intDeleted).Delete
'increments the variable
intDeleted = intDeleted + 1
End If
End If
Next i
End Sub
Result:
The for i loop literates through all the shapes in the document:
For i = 1 To intCount
....
Next i
The code below checks if the shape is a text box (Note that captions are actually a text box inserted below the picture);
If Shapes.Item(i - intDeleted).Type = _
msoTextBox Then
...
End If
The lines below select the text box and retrieve the text:
Shapes.Item(i - intDeleted).Select
'gets the text in the text box
strText = Selection.Range
The If statement below checks if the text box starts with the string “Figure”:
If Strings.Left(strText, 6) = "Figure" Then
...
End If
If it does then the text box will be deleted:
Shapes.Item(i - intDeleted).Delete
'increments the variable
intDeleted = intDeleted + 1
Remove Caption From Inline Pictures:
The caption for inline pictures is regular text. In this text there is a field with the code:
" SEQ Figure *"
This field is what causes the numbers to increment per image. This was obtained by right clicking the field and clicking on edit field:
What we need is a macro that will loop through all the fields in the document. If the field starts with the code above then it must select the entire line the field is in and delete it. The code below does this:
Sub Example()
Dim i As Integer
Dim strField As String
Dim intDeleted As Integer
'loop through all the fields
For i = 1 To Fields.Count
'get the field code
strField = Fields.Item(i - intDeleted).Code
'check the field code
If Strings.Left(strField, 14) = _
" SEQ Figure *" Then
'select the field
Fields.Item(i - intDeleted).Select
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, _
Extend:=wdExtend
'delete it
Selection.Delete
intDeleted = intDeleted + 1
End If
Next i
End Sub
The for i loop iterates through all the fields in the document:
For i = 1 To Fields.Count
...
End Sub
The If statement checks if the field is a caption field:
If Strings.Left(strField, 14) = _
" SEQ Figure *" Then
...
End If
The lines below removes everything on the field line:
Fields.Item(i - intDeleted).Select
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, _
Extend:=wdExtend
'delete it
Selection.Delete
Result:
You can download the files and code related to this article from the links below:
See also:
- Word VBA, Get Picture Horizontal Position
- Word VBA, Re-Adjust Caption
- Word VBA, Loop Through Images
- Word VBA, Adding Caption to Images
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com