Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2019, 08:16 AM
afif afif is offline Generating automatic caption for table based on document section Windows 7 64bit Generating automatic caption for table based on document section Office 2016
Novice
Generating automatic caption for table based on document section
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default Generating automatic caption for table based on document section

I'm wondering whether it's possible to use VBA to generate automatic table caption based on the section where the table is in (for example there is a document about sales of any kind of vehicle. chapter 3 contains information about car (so the heading is like chapter 3. Car ). Inside this chapter, there is a table about car sales. The caption of the table is generated by reading the heading/the title of the section).

I'm trying to put automatic caption on tens of tables in a document. It is not that good if the caption only contains the table number (Table 1, Table 2, etc) without any other information.
Thanks in advance.
Reply With Quote
  #2  
Old 07-24-2019, 09:48 AM
gmaxey gmaxey is offline Generating automatic caption for table based on document section Windows 10 Generating automatic caption for table based on document section Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

What you are describing are not "sections." Provided your "Chapter 3. Car" is a paragraph with an outline level 1 e.g., Heading1. Then the following may work:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim oRng As Range, oRngCap As Range
Dim strTitle As String
  For Each oTbl In ActiveDocument.Tables
    Set oRng = oTbl.Range
    Set oRngCap = oTbl.Range
    oRng.Collapse wdCollapseStart
    oRngCap.Collapse wdCollapseEnd
    Do
      oRng.MoveStart wdParagraph, -1
      oRng.End = oRng.Paragraphs(1).Range.End - 1
      strTitle = oRng.Text
    Loop Until oRng.Paragraphs(1).OutlineLevel = 1
    oRngCap.InsertCaption Label:="Table", TitleAutoText:="", Title:=" - " & strTitle, _
    Position:=wdCaptionPositionAbove, ExcludeLabel:=0
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-24-2019, 03:28 PM
Guessed's Avatar
Guessed Guessed is offline Generating automatic caption for table based on document section Windows 10 Generating automatic caption for table based on document section Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
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
  #4  
Old 07-25-2019, 08:21 AM
afif afif is offline Generating automatic caption for table based on document section Windows 7 64bit Generating automatic caption for table based on document section Office 2016
Novice
Generating automatic caption for table based on document section
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default

Thanks Greg, Andrew..
Both codes work like a charm..
Reply With Quote
  #5  
Old 07-25-2019, 10:54 AM
gmaxey gmaxey is offline Generating automatic caption for table based on document section Windows 10 Generating automatic caption for table based on document section Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Actually Andrew's code is cleaner ;-). If you really do only want to use a specific heading to fix the table captions (e.g., Heading 1), here is a version which combines his method and mine:

Code:
Sub ScratchMacro()
Dim oTbl As Table, oRngHeading As Range
Dim lngLevel As Long
  lngLevel = 1 'This means only Heading 1 Style is used to define table titles.
  For Each oTbl In ActiveDocument.Tables
    Set oRngHeading = oTbl.Range.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1).Paragraphs(1).Range
    Do Until oRngHeading.Paragraphs(1).OutlineLevel = lngLevel
      Set oRngHeading = oRngHeading.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious, Count:=1).Paragraphs(1).Range
    Loop
    oRngHeading.MoveEnd Unit:=wdCharacter, Count:=-1
    oTbl.Range.InsertCaption Label:="Table", Title:=" - " & oRngHeading.Text, Position:=wdCaptionPositionBelow, ExcludeLabel:=0
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating automatic caption for table based on document section Automatic locking section of document AFTER text typed lou1990lou Word VBA 2 01-20-2019 02:39 PM
Generating automatic caption for table based on document section Automatic table of contents for just one section of document? seanspotatobusiness Word 2 02-06-2017 07:13 AM
section page numbers reverting to following on from previous section after generating index cloudtrapezer Word 1 06-25-2015 01:16 AM
Generating automatic caption for table based on document section Generating Print Labels Dynamically Based on SQL Server expinch Word 1 11-26-2014 10:05 PM
Automatic table of figures includes one of the figures, not just the caption - help! sarahlt Word 1 09-28-2014 09:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:27 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