Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 02-19-2012, 01:17 AM
macropod's Avatar
macropod macropod is offline VBA to insert captions without appending to existing captions Windows 7 64bit VBA to insert captions without appending to existing captions Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Marrick,

Try:
Code:
Sub ApplyCaptions()
Application.ScreenUpdating = True
Dim oCap As CaptionLabel, bCap As Boolean, iShp As InlineShape, oTbl As Table, TmpRng As Range
With ActiveDocument
  For Each iShp In .InlineShapes
    Set TmpRng = iShp.Range.Paragraphs.First.Range
    With TmpRng
      If .Style = "Caption" Then bCap = ChkCaption(TmpRng)
      If .Paragraphs.Last.Next.Range.Style = "Caption" And bCap = False Then
        bCap = ChkCaption(TmpRng)
      End If
      If bCap = False Then
        iShp.Range.InsertCaption Label:="Figure", TitleAutoText:="", _
          Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
      End If
    End With
  Next
  For Each oTbl In .Tables
    Set TmpRng = oTbl.Range.Paragraphs.Last.Range
    With TmpRng
      If .Style = "Caption" Then bCap = ChkCaption(TmpRng)
      If .Paragraphs.Last.Next.Range.Style = "Caption" And bCap = False Then
        bCap = ChkCaption(TmpRng)
      End If
      If bCap = False Then
        oTbl.Range.InsertCaption Label:="Table", TitleAutoText:="", _
          Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
      End If
    End With
  Next
End With
Set TmpRng = Nothing
Application.ScreenUpdating = False
End Sub
 
Function ChkCaption(TmpRng As Range) As Boolean
Dim oCap As CaptionLabel
ChkCaption = False
For Each oCap In CaptionLabels
  If InStr(TmpRng.Text, CaptionLabels(oCap)) > 0 Then
    ChkCaption = True
    Exit For
  End If
Next
End Function
Depending on the # of tables & inlineshapes, this might take a while.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #17  
Old 02-19-2012, 06:06 AM
Marrick13 Marrick13 is offline VBA to insert captions without appending to existing captions Windows XP VBA to insert captions without appending to existing captions Office 2010 32bit
Competent Performer
VBA to insert captions without appending to existing captions
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

Hits the spot, Paul. Thanks so much!
Reply With Quote
  #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
Reply



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 08:32 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