Bookmark as range

S

Sol Apache

I have this code which works fine EXCEPT that in Windows Word (not Mac Word)
it gives an error message that the cell does not exist (because there is a
vertically merged cell before it) - the code then works after giving the
error message.

Naturally an error message will alarm users and I do not want this to
happen.

What I want to do as a workaround is to use a bookmark instead, but I cannot
make a working code which selects the bookmark as range. This is the current
working code, selecting a cell as range.

Dim myRg as range
Set myRg = _
ActiveDocument.Tables(2).Cell(Row:=5, Column:=2).Range
With myRg
..Delete
.Text = TxtAlt1Addr1 & Chr(13)
.Style = ("BaseAddress Back")
.Collapse wdCollapseEnd
.Text = TxtAlt1Addr2.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
..Text = TxtAlt1Addr3.Value & Chr(13)
.Style = ("BaseAddress Back")
.Collapse wdCollapseEnd
.Text = "Telephone: +" & TxtAlt1Phone & Chr(13)
.Style = ("DirectLine Back")
.Collapse wdCollapseEnd
.Text = "Direct Line: +" & TxtAlt1Direct.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
.Text = "Facsimile: +" & TxtAlt1Fax.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
.Text = TxtAltemail.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
.Text = TxtAlt1WWW.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
.Text = TxtAltWWW2.Value & Chr(13)
.Style = ("BaseAddress Back")
..Collapse wdCollapseEnd
End With


Would appreciate any help.

Thanks
 
S

Sol Apache

Hello

Just realised I was missing some quote marks. This works:

Set myRg = ActiveDocument.Bookmarks("BM2").Range
 
J

Jonathan West

Hi Sol

You need to add error-handling code to trap the error and decide what to do.

In your case, it might be something as simple as adding this line to the
start of the macro

On Error Resume Next

which tells Word if it encounters an error to carry on with the next line as
if nothing had happened.

More sophisticated error handling schemes can be something like this

Sub DoSomething
On Error Goto Errhandle

' your main code goes here

Exit Sub

Errhandle:
If Err.number = 1234 Then 'change this number to the specific error
'you are expecting
' put your error handling code here
Resume Next 'carry on from the line following the error
End If

' If you want, put general-purpose error handling code here for any errors
' You weren't expecting to see

End Sub


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Sol Apache

Hi Jonathan

Thanks for this very handy tip. My client is happy with the bookmarks but I
know what to do next time.

Sol
 

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