Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 06-27-2024, 05:45 PM
Guessed's Avatar
Guessed Guessed is offline Error in Code for Checking Figure Numbers Windows 10 Error in Code for Checking Figure Numbers Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
 

Tags
caption inline shapes, field code caption, inline shapes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Code for Checking Figure Numbers 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
Error in Code for Checking Figure Numbers 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:49 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft