Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-30-2018, 01:24 AM
NevilleT NevilleT is offline Table of Contents causes error when checking style Windows 7 64bit Table of Contents causes error when checking style Office 2003
Novice
Table of Contents causes error when checking style
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default Table of Contents causes error when checking style

I have a macro that loops through each paragraph in a document and finds a particular style. It then takes that text, and puts it in an array. So far so good. Where I came unstuck is with a table of contents which has no style. It causes an error.

I have trimmed down the function as it is much more complex than the one below, but hopefully this will provide the basis to understand the problem.

How do I cater for hitting a ToC with no style when looping through the document?

Public Function funGetHeadingFromStyle(ByVal strStyleToFind As String) As String
Dim rngCurrentRange As Range
Dim blnWholeDocumentSearched As Boolean
Dim blnHeadingFound As Boolean
Dim intHeadersFound
Dim strTest As String

'---------------------------------------------------------------
' Initialise the variables
Set rngCurrentRange = Selection.Range
blnWholeDocumentSearched = False
blnHeadingFound = False
intHeadersFound = 0

Do Until blnWholeDocumentSearched ' Loop until you reach the end of the document

If rngCurrentRange.Style = strStyleToFind Then ' Found a heading

strTest = Trim(rngCurrentRange.Text)
strTest = Left(strTest, Len(strTest) - 1) ' Remove paragraph mark

' Add a comma if it is not the first header
If intHeadersFound > 0 Then
funGetHeadingFromStyle = funGetHeadingFromStyle & ", "
End If

' Store the header text
funGetHeadingFromStyle = funGetHeadingFromStyle & strTest

' Update the counter - number of headers found
intHeadersFound = intHeadersFound + 1 ' Number of records found
End If

Set rngCurrentRange = rngCurrentRange.Next(wdParagraph, 1) ' Select the next paragraph

blnWholeDocumentSearched = ActiveDocument.Range(0, rngCurrentRange.Paragraphs(1).Range.End).Paragraph s.Count = 0 ' End of the document
Loop



End Function
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Table of Contents causes error when checking style Table of contents vs headings: what style settings need to be changed? boxhamster Word 5 08-15-2015 03:25 PM
LaTeX style table of contents in MS Word MichaelHenrotte Word 1 11-23-2014 03:16 AM
Table of Contents causes error when checking style Creating Table of Contents From a Custom Style Jetheat Word 4 12-15-2013 03:54 PM
How to have this Heading – Table of contents style? Jamal NUMAN Word 0 01-13-2011 06:02 PM
Word 2003 Table of Contents Error Message ktcolburn Word 1 10-30-2005 06:14 PM

Other Forums: Access Forums

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


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