Error trapping an Input Box

C

Compass Rose

I have to error trap the following line of code:

ActiveDocument.Variables("varmeetingnumber").Value = InputBox("Meeting
Number")

If the user doesn't enter any number, I want to display a Message Box and
prompt the user again for a number.

Being new to VBA, I don't know the code to do this. Your help is appreciated.

TIA, David
 
H

Helmut Weber

Hi Compass Rose,

in what format should a meeting number be?
Largest number?
Smallest number?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Compass Rose

Perhaps I didn't explain myself fully. I have to prepare and distribute the
minutes of a weekly meeting that I chair. The meetings are numbered
sequentially, and the number appears in various places in the document, hence
the prompt, and I place a DOCVARIABLE in those places in the document where
they are needed.

If, by mistake, a wrong meeting number is entered or a null value is
entered, I want to be able to error trap those conditions. I don't know the
syntax to accomplish this.

I hope this is clearer now.
 
J

Jean-Guy Marcil

Compass Rose was telling us:
Compass Rose nous racontait que :
Perhaps I didn't explain myself fully. I have to prepare and
distribute the minutes of a weekly meeting that I chair. The meetings
are numbered sequentially, and the number appears in various places
in the document, hence the prompt, and I place a DOCVARIABLE in those
places in the document where they are needed.

If, by mistake, a wrong meeting number is entered or a null value is
entered, I want to be able to error trap those conditions. I don't
know the syntax to accomplish this.

I hope this is clearer now.

You were perfectly clear.

What Helmut wanted to know were the parameters to use for the logic that
will be needed to decide if a given number is acceptable.
For example, the following are numbers, but probably unacceptable as meeting
numbers: "123.45", "-56" or "567,678".

In any case, here is a sample that accepts any number..

Dim lngMeet As Variant
Dim boolOK As Boolean

Do
lngMeet = InputBox("Meeting Number")
'lngMeet = "" when user clicks on "Cancel"
If lngMeet = "" Then Exit Sub
If Not IsNumeric(lngMeet) Then
MsgBox "You must type a valid standard number."
boolOK = False
Else
boolOK = True
ActiveDocument.Variables("varmeetingnumber").Value = CStr(lngMeet)
End If
Loop While Not boolOK

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Compass Rose

Merci beacoup, Jean-Guy. This does exactly what I want.

However, I found one small glitch. I changed

If Not IsNumeric(lngMeet) Then

to

If Val(lngMeet) <> Int(Val(lngMeet)) Then

Thanks again,
David
 

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