run-time error 13 - Type mismatch (don't know why)

J

justalostgrunt

Hi,

What worked six months ago seems to give a runtime error now.
Revisting a VBA routine as the project has come out of moth balls.

error 13 - type mismatch on the line:

Set bmrange = destdoc11.Bookmarks("section11").Range ' locate the bookmark

In the snippet of the whole routine,

If Dir(strAtt) = "" Then
MsgBox "File does not exist"
Else
Set srcDoc = Wordapp.Documents.Open(strAtt) 'attachment sheet
End If

'open the letter
Set destdoc11 = Wordapp.Documents.Open(strInputFileName) 'letter

Set bmrange = destdoc11.Bookmarks("section11").Range ' locate the bookmark

'create new section - add a page past the bookmark
destdoc11.Bookmarks("section11").Range.InsertBreak
Type:=wdSectionBreakNextPage

're-insert the bookmark as we have removed it by the section break
destdoc11.Bookmarks.Add "section11", bmrange
'copy the text and replace the bookmark
bmrange.FormattedText = srcDoc.Range.FormattedText
destdoc11.close (wdSaveChanges)

'close the Attachment sheet
srcDoc.close (wdDoNotSaveChanges)


I retraced my steps revisted posts from April and use of bookmarks on the
FAQ word site and I can't see what could cause this glitch.
Corrupt VBA or the bookmarks in the original letter?

Any ideas appreciated.

Regards
bill
 
N

NZ VBA Developer

Bill,

I'm assuming that at some point in your code you have declared "bmrange" as
a Range object, yes? And have you tried putting in some code to check to
ensure that the "section11" bookmark actually exists in "destdoc11"? These
are the only two reasons I can think of for a 'type mismatch' error, and the
first case seems the most likely. If you don't explicitly tell Word that
"bmrange" is a Range object (and don't use Option Explicit) it will create
"bmrange" as a Variant type variable, and you may not be able to load a Range
into it. If it's just a matter of not being able to find the bookmark I would
expect a 'member of collection does not exist' type error instead.
 
J

justalostgrunt

Hi,
Yes bmrange is declared as a range and option explicit is the case for the
module behind this headache - so to speak.

I am automating from an access database - not sure now if thats a problem -
but it worked before so I have to discount that.

The bookmark is there as the first part is to mail merge some data. The
letter is saved.
Destdoc11 becomes the letter.
I've checked the letter that is produced first to find the bookmarks (there
are 11) and appear OK.

Write some code to check the bookmark - ummm.

Regards
Bill
 
N

NZ VBA Developer

Well that blows that theory. <g>

Unfortunately, I'm not familiar enough with Access to determine if it's
playing a role, but as you say, it worked before...

As for checking for the existence of the bookmark, that's just a couple of
lines. Bookmarks support an 'Exists' method, so it's just a matter of:

If destdoc11.Bookmarks.Exists("section11") Then
'do the stuff you want to do with the bookmark
Else
'handle the error however you think it should be handled
End If

I'm afraid that exhausts my supply of suggestions, altho maybe as part of
the debugging process you might want to put something in after you check for
the existence of the bookmark that will let you confirm that the right
bookmark is being used, such as:

MsgBox destdoc11.Bookmarks.("section11").Range.Text

I've also found that sometimes just working with the Bookmark Range object
causes problems, but the workarounds (selecting the Bookmark Range and then
working with the Selection Range object instead) are ugly and probably not
advisable.

If none of this helps, let's hope that somebody else has some ideas.
 
J

justalostgrunt

Hi,
Thanks,
Will place that snipet of code into the routine and see what transpires.

Read somewhere that word is unpredictable and what may work on one computer
may not work on another. Something to do wth defining a page, But a bookmark
is a bookmark - unpredicatable blitters.

Thanks for your help.

Best Regards
Bill
 

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