Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2023, 03:48 AM
lina2299 lina2299 is offline Word Macro: Create Table of Figures by hand from manually numbered figures Mac OS X Word Macro: Create Table of Figures by hand from manually numbered figures Office 2021
Novice
Word Macro: Create Table of Figures by hand from manually numbered figures
 
Join Date: Dec 2023
Posts: 1
lina2299 is on a distinguished road
Default Word Macro: Create Table of Figures by hand from manually numbered figures


I have a Word document with figures, and for some reason, Word is completely messing up the figure numbers, so I basically re-numbered them on my own. However, when I create the Table of Figures, the numbers and pages don't match at all.

I tried writing my own macro to go through the doc, scan for image captions and add a Table of Figures, but it doesn't really seem to work. Any suggestions on the code?

Code:
Sub AbbVerzeichnis()
'
' AbbVerzeichnis Makro
'
'
 
Dim rng As Range
Dim docText As String
Dim match As Object
Dim regexPattern As String
Dim abbildungsverzeichnis As String

' Initialisiere das Abbildungsverzeichnis
abbildungsverzeichnis = "Abbildungsverzeichnis:" & vbCrLf

' Text des gesamten Dokuments in eine Variable laden
docText = ActiveDocument.Range.text

' Definiere das reguläre Ausdrucksmuster
regexPattern = "Abb\. (\d+): ([^\r\n]+)"

' Suche nach Übereinstimmungen im Dokument
Set match = GetMatch(docText, regexPattern)

' Durchlaufe die gefundenen Übereinstimmungen und füge sie zum Abbildungsverzeichnis hinzu
Do While Not match Is Nothing
    abbildungsverzeichnis = abbildungsverzeichnis & "Abbildung " & match.SubMatches(0) & ": " & match.SubMatches(1) & vbCrLf
    Set match = GetMatch(docText, regexPattern, match.FirstIndex + Len(match.Value))
Loop

' Füge das Abbildungsverzeichnis am Ende des Dokuments ein
Set rng = ActiveDocument.Range
rng.Collapse Direction:=wdCollapseEnd
rng.text = abbildungsverzeichnis
End Sub
 
Function GetMatch(text As String, pattern As String, Optional startPos As Long = 1) As Object
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")

With regex
    .Global = False
    .MultiLine = False
    .IgnoreCase = True
    .pattern = pattern
End With

If regex.Test(Mid(text, startPos)) Then
    Set GetMatch = regex.Execute(Mid(text, startPos))(0)
End If
Reply With Quote
  #2  
Old 12-19-2023, 07:11 AM
Italophile Italophile is offline Word Macro: Create Table of Figures by hand from manually numbered figures Windows 11 Word Macro: Create Table of Figures by hand from manually numbered figures Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Cross-posted at vba - Word Macro: Create Table of Figures by hand from manually numbered figures - Stack Overflow
For cross-posting etiquette, please read: A message to forum cross posters - Excelguru
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Macro: Create Table of Figures by hand from manually numbered figures Table of Figures Failing to Capture all figures RKS-P Word 2 09-19-2019 08:23 AM
List of figures with several labels (caption) - Table of Figures sorting ibra_ca Word 2 10-11-2017 07:02 AM
Word Macro: Create Table of Figures by hand from manually numbered figures word 2010 table of figures disabled Tinga Word 2 07-22-2015 11:56 AM
Automatic table of figures includes one of the figures, not just the caption - help! sarahlt Word 1 09-28-2014 09:34 AM
Figures and Table of Figures not updating tcp01 Word 1 12-05-2013 08:25 PM

Other Forums: Access Forums

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