Type Mismatch error when calling Sub with apparently correct types

H

Henry Kwan

I have the following code:

Sub ClearSection()
If True Then
For Each t In ActiveDocument.Tables
ClearOneSection (t)
Next t
Else
ClearOneSection (Selection.Tables(1))
End If
End Sub
Sub ClearOneSection(ByVal t As Table)
' Do something
End Sub

For some reason, the Else statement will throw a Type Mismatch error during
runtime. The If statement runs correctly.

Does anyone have any ideas why this might be the case?

Any help is greatly appreciated.
 
D

Doug Robbins - Word MVP

Try

Sub ClearSection()
If True Then
For Each t In ActiveDocument.Tables
ClearOneSection (t)
Next t
Else
t = Selection.Tables(1)
ClearOneSection (t)
End If
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

StevenM

To: Henry Kwan,

You need to remove the parenthesis thusly:

ClearOneSection Selection.Tables(1)

Also, you might consider adding:

Selection.Collapse CollapseEnd
If Selection.Information(wdWithInTable) = True Then
ClearOneSection Selection.Tables(1)
End If

And so:

Sub ClearSection()
Dim t As Table

If False Then
For Each t In ActiveDocument.Tables
ClearOneSection t
Next t
Else
Selection.Collapse CollapseEnd
If Selection.Information(wdWithInTable) = True Then
ClearOneSection Selection.Tables(1)
End If
End If
End Sub

Sub ClearOneSection(ByVal t As Table)
' Do something
End Sub

On the other hand, if you like the parenthesis (as I do), then you can add
"call" to your statements, such as:

Call ClearOneSection(Selection.Tables(1))

Steven Craig Miller
 
T

Tony Jollans

The reason is that you have Selection.Tables(1) in parentheses; this makes
VBA evaluate the expression before passing the returned result. The default
property of a Table object is its Range, hence the type mismatch.

You also have t in parentheses, and this, too, is evaluated. However,
because you have not declared it, it is a Variant Object, not explicitly a
Table, and it doesn't evaluate in the same way. If you declare t as a Table,
you will get a Type mismatch on that call, too.

This will work:

Sub ClearSection()
If True Then
For Each t In ActiveDocument.Tables
ClearOneSection t
Next t
Else
ClearOneSection Selection.Tables(1)
End If
End Sub
Sub ClearOneSection(ByVal t As Table)
' Do something
End Sub
 
H

Henry Kwan

Thanks guys. That was the problem.

For the purposes of these macros, whether or not something (e.g., a table)
is highlighted in full shouldn't matter, so I haven't had a need to collapse
the selection. As long as I have the insertion cursor in the table, I'm fine.

I'll have to start using that Call prefix since I'm so used to using C++ and
Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top