View Single Post
 
Old 07-24-2019, 03:28 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

Greg's code always gives you the above Heading 1 text. If you wanted the nearest preceding heading of any level the code could be
Code:
Sub ScratchMacro()
  Dim oTbl As Table, oRng As Range, rngHead As Range, strTitle As String
  For Each oTbl In ActiveDocument.Tables
    Set oRng = oTbl.Range
    Set rngHead = oRng.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1).Paragraphs(1).Range
    rngHead.MoveEnd Unit:=wdCharacter, Count:=-1
    strTitle = rngHead.Text
    oTbl.Range.InsertCaption Label:="Table", Title:=" - " & strTitle, Position:=wdCaptionPositionAbove, ExcludeLabel:=0
  Next
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote