BruceWayne
BruceWayne

Reputation: 23283

Include code based on ToC existing?

This is more of a general question on how to implement my code, than an issue with the code itself.

I have some code that scans through a Word Document, looks for some words, and puts them in an index. This document may or may not have a Table of Contents.

If there's a ToC, I want my macro to skip the text in that range, so I have a line like this:

Sub find_things()
Dim myDoc as Word.Document
Dim otherVariables here

[some preliminary code]

' the next line checks to see if the current range is in a ToC
  If rngXE.InRange(myDoc.TablesOfContents(1).Range) = False Then
      [code here will find my text, and do some things]
  End If 
End Sub

So, hopefully you can see that if the rngXE range is NOT in a table of contents, run some code.

However, since sometimes there's no ToC, I get an error raised at the If statement line. ("The requested member of the collection does not exist").

Question: What's the best way to handle this? Should I do On Error Resume Next before that? So if there's an error, it will continue on?

Or, is there some way to count the Tables of Contents, and if it's 0, then don't include the If statement lines?

Sub find_things()
Dim myDoc as Word.Document
Dim otherVariables here

[some preliminary code]

' the next line checks to see if the current range is in a ToC
  If myDoc.TablesofContents.Count > 0 Then    
      If rngXE.InRange(myDoc.TablesOfContents(1).Range) = False Then
          [code here will find my text, and do some things]
      End If 
 End If
End Sub

...except if I use this, it's always going to skip my code when there's no TOC. I'd rather not use On Error Goto since I've had it drilled into my head that GoTo is generally a bad idea...but that may be the best option, no?

I hope this is clear at all - kinda hard to verbalize the issue but please let me know if I can clarify anything!

edit: The way I'm handling this now, is if I get that Error, I just Comment out the If rngXE.InRange(myDoc...) = False and corresponding End If lines, and it runs perfectly. I am looking for an automatic way to do this, rather than my personal intervention.

Upvotes: 0

Views: 71

Answers (1)

user1379931
user1379931

Reputation:

Two fairly simple patterns you could use without resorting to either "On Error Goto" or "Goto" are:

If myDoc.TablesofContents.Count = 0 Then
  call asubtodothework(rngXE) ' you may need to pass more parameters...
elseif not rngXE.InRange(myDoc.TablesOfContents(1).Range)
  call asubtodothework(rngXE) ' ditto
end if

Dim bDoIt As Boolean
bDoIt = True
If myDoc.TablesofContents.Count > 0 Then
  If rngXE.InRange(myDoc.TablesOfContents(1).Range) Then
    bDoIt = False
  End If
End If
If bDoIt Then
  ' do the work
End If

(As they stand, both make the same assumption as your existing code, i.e. that you only need to check for TableOfContents(1). My guess is that if you need to check for more ToCs, somewhere you will end up with some code rather like the second pattern anyway).

or even the following...

Dim bDoIt As Boolean
bDoIt = True
If myDoc.TablesofContents.Count > 0 Then
  bDoIt = rngXE.InRange(myDoc.TablesOfContents(1).Range) 
End If
If bDoIt Then
  ' do the work
End If

...or perhaps a single-liner would work (I haven't actually checked the logic, and I have a personal preference for using the full If...End If construct anyway)...

Dim bDoIt As Boolean
bDoIt = True
If myDoc.TablesofContents.Count > 0 Then bDoIt = rngXE.InRange(myDoc.TablesOfContents(1).Range)
If bDoIt Then
  ' do the work
End If

Upvotes: 1

Related Questions