![]() |
|
#1
|
|||
|
|||
|
I routinely work with large documents that have many tables and figures. I have code that iterates through a document looking at all tables for captions. It looks for the presence of a field code above the table. If there isn't one, it adds a caption. I believe Andrew Lockton (a/k/a Guessed) graciously helped with this years ago. I have recently attempted to adapt the code for figures (inline shapes). Unfortunately, it adds a caption below all inline shapes whether or not a caption already exists. I am hoping an expert, here, can look at the code and identify the error(s) or tell me whether what I'm trying to accomplish can even be done. Thank you in advance for considering my request for help. Code:
Sub CheckAllFigures()
Dim iShape As InlineShape
Dim myBigRange As Range
Dim MyRange As Range
Application.ScreenUpdating = False
On Error GoTo ErrorHandler
Set myBigRange = ActiveDocument.Range
'Process figures within the entire document
For Each iShape In myBigRange.InlineShapes
iShape.Select
'Look at para below the figure. Caption there? If not, add one.
Selection.MoveDown Unit:=wdLine, Count:=1
Set MyRange = Selection.Paragraphs(1).Range
objShape.Select
If MyRange.Fields.Count = 0 Then
Selection.InsertCaption Label:="Figure", TitleAutoText:="", _
Title:=". ", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
ElseIf MyRange.Fields(1).Type <> wdFieldSequence Then
Selection.InsertCaption Label:="Figure", TitleAutoText:="", _
Title:=". ", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
End If
Next iShape 'Go to next shape
Set MyRange = Nothing
Set myBigRange = Nothing
Application.ScreenUpdating = True
Application.ScreenRefresh
MsgBox "Done!", vbInformation
Exit Sub 'If you get this far - avoid the error handler
ErrorHandler:
MsgBox "Error: " & Err.Number & vbCr & Err.Description
End Sub
|
|
#2
|
||||
|
||||
|
Firstly, your code has an objShape which looks like it should be an iShape
The code is looking at the first field in the paragraph below the inline shape. Can you confirm that the first field is a sequence field because it could also be a styleref field (if your captions include the chapter number). This might need adjustment to dig further to decide whether a 'caption' already exists there. You could test for a style or look through all fields in that paragraph for instance
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
Oops! That was a typo and should have been iShape. I wanted to tidy up my code before posting and overlooked that one. As far as the first field below the inline shape, all are a sequence fields (SEQ Figure). We rarely include the chapter number in captions. You make a good point. I had not considered styleref fields.
I was able to write a macro that checks for the caption style following the inline shape, and it seems to work okay. More often than not, though, authors will use the caption style but not insert a caption to include the number, so I thought looking for a field might be a better approach. |
|
#4
|
|||
|
|||
|
Hey, Andrew.
Some of our captions are followed immediately by a style separator and descriptive text in another style. I discovered this morning that it is in those instances where the macro fails (adding a caption where one already exists). Do you think there is a workaround for this scenario? Many thanks for looking at the code I posted and for considering my request for help. |
|
#5
|
||||
|
||||
|
You haven't indicated whether you are expecting the seq field to be on the Next paragraph or the Next.Next paragraph when your code runs into the style separator. You will have to do some testing to debug the code and deal with situations like that. Try this variation and step through the code to see what is being tested for the presence of the Seq field. The Select statements don't need to be in the code but they help you see what the macro is working on.
Code:
Sub CheckAllFigures()
Dim iShape As InlineShape, myBigRange As Range, rngCaption As Range
Application.ScreenUpdating = False
On Error GoTo ErrorHandler
Set myBigRange = ActiveDocument.Range
For Each iShape In myBigRange.InlineShapes 'Process figures within the entire document
'Look at para below the figure. Caption there? If not, add one.
Set rngCaption = iShape.Range.Paragraphs(1).Next.Range
rngCaption.Select 'this will help you debug the range being considered for the presence of the caption
If rngCaption.Fields.Count = 0 Then
If rngCaption.Characters.Last.Font.Hidden = True Then 'has a style separator
Set rngCaption = rngCaption.Paragraphs.Last.Next.Range
rngCaption.Select
Debug.Print "What now"
Else
iShape.Range.InsertCaption Label:="Figure", Title:=". ", Position:=wdCaptionPositionBelow, ExcludeLabel:=False
End If
ElseIf rngCaption.Fields(1).Type <> wdFieldSequence Then
iShape.Range.InsertCaption Label:="Figure", Title:=". ", Position:=wdCaptionPositionBelow, ExcludeLabel:=False
End If
Next iShape 'Go to next shape
Set rngCaption = Nothing
Set myBigRange = Nothing
Application.ScreenUpdating = True
Application.ScreenRefresh
MsgBox "Done!", vbInformation
Exit Sub 'If you get this far - avoid the error handler
ErrorHandler:
MsgBox "Error: " & Err.Number & vbCr & Err.Description
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#6
|
|||
|
|||
|
Good morning, Andrew.
The seq field is expected immediately after the figure (inline shape). I've tested your code against several documents this morning, and it has worked perfectly. THANK YOU for reviewing and rewriting my code. I appreciate it so much. I can't express how helpful this will be. |
|
| Tags |
| caption inline shapes, field code caption, inline shapes |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Figure number cross-referencing mess; figure numbers not updating.
|
seanspotatobusiness | Word | 25 | 08-06-2018 05:00 PM |
| Table of Contents causes error when checking style | NevilleT | Word VBA | 14 | 05-02-2018 01:23 AM |
| Trying to figure out this code | sharpied | Word | 0 | 07-27-2016 12:09 PM |
I want to add multiple rows into my document but I can not figure out the code
|
jlw15931 | Mail Merge | 1 | 02-24-2015 05:03 AM |
| Can Word automatically update figure numbers? | gib65 | Word | 7 | 02-22-2013 05:52 AM |