Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #18  
Old 03-21-2023, 07:51 PM
FrostKo FrostKo is offline VBA to insert captions without appending to existing captions Windows 10 VBA to insert captions without appending to existing captions Office 2021
Novice
 
Join Date: Mar 2023
Location: Dallas
Posts: 1
FrostKo is on a distinguished road
Default

This helped tremendously! I ended taking what I needed but it was generating an error if there wasn't two empty paragraphs after the last image. I wanted to share my code. This allows for the error to occur but adds the caption anyway without any empty paragraphs after the last image.

Code:
Sub Captions()
On Error GoTo Error
Application.ScreenUpdating = True
Dim RngStry As Range, iShp As InlineShape, TmpRng As Range
For Each RngStry In ActiveDocument.StoryRanges
  For Each iShp In RngStry.InlineShapes
    Set TmpRng = iShp.Range.Paragraphs.First.Range
    With TmpRng
      If .Style <> "Caption" Then
        If .Paragraphs.Last.Next.Style <> "Caption" Then
        Exit Sub
Error:
          iShp.Range.InsertCaption Label:="Photo", TitleAutoText:="", _
           Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
        End If
      End If
    End With
  Next
Next
Set TmpRng = Nothing
Application.ScreenUpdating = False
Resume Next
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to insert captions without appending to existing captions Move existing table captions bcarlier Word Tables 17 05-10-2014 02:36 PM
Captions/Styles NWoffice Word 5 10-06-2011 10:26 AM
VBA to insert captions without appending to existing captions creating tables for row of figures and captions gib65 Word 2 08-12-2011 01:25 PM
VBA to insert captions without appending to existing captions Captions (tables and figures) mcjohn Word 1 02-11-2010 10:36 PM
Captions dwilliams Word 0 10-07-2009 08:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:48 AM.


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