Input box

D

David Rose

I have a document which is the minutes of a meeting. I have a macro, which
prompts for the meeting number, that I access numerous times during the
editing of the document. I want the prompt to appear only the first time
that the macro is run. The code I wrote is as follows:

Sub Indent()

Dim MtgNo As String

If MtgNo = "" Then
MtgNo = InputBox("Enter Meeting Number:")
End If

....rest of macro...

However, the prompt appears each time the macro is run. What's the fix?

TIA
David
 
G

Greg

David,

The IF condition doesn't know the value of your string MtgNo and so
treats it as "".

You need to establish in advance of the IF statement what the value of
MtgNo is. You can store this value when you initially set it as a
Varialbe. Here is one way (not sure if it is the best or most
efficient way)

Sub Indent()
Dim MtgNo As String
On Error Resume Next
MtgNo = ActiveDocument.Variables("MeetingNum").Value
On Error GoTo 0
If MtgNo = "" Then
MtgNo = InputBox("Enter Meeting Number:")
ActiveDocument.Variables("MeetingNum").Value = MtgNo
End If
End Sub

If you have never established a meeting number an error will be
generated when you attempt to set the value MtgNo to the docVariable.
We know this so skip the error. Since a MtgNo doesn't exist the IF
condition will let you set one. When you set it you save its value in
the Variable. Next time you run the macro the IF condition is not met
so the the code is skipped.
 
D

David Rose

I just realized that elsewhere in the document I have a FILLIN prompt which
is bookmarked as MtgNo. The FILLIN is performed before the first macro
access. Can the bookmark value be used in the macro, eliminating the need
for the InputBox, and if so, how?

TIA
David
 
G

Greg Maxey

David,

Well yes, but if you are setting your meeting number with a Fillin then why
do you need the IF Condition in the macro.

{ SET "MtgNo" {FILLIN "Enter your meeting number"}}{REF MtgNo} displays the
meeting number in the text

You can use the bookmark to assign the value to MtgNo in the macro, but what
do you need it for?

Sub Indent()
Dim MtgNo As String
On Error Resume Next
MtgNo = ActiveDocument.Bookmarks("MtgNo").Range.Text
On Error GoTo 0
If MtgNo = "" Then 'It won't be if you used the fillin

End If
End Sub
 
D

David Rose

Greg
I realized that the IF etc. is not required if I could use the bookmarked
FILLIN instead of the whole InputBox thing. What I didn't go into on the
original post is that the meeting number is displayed in other locations of
the document (e.g. header) which I accomplished with {REF MtgNo}, as well as
being needed further into the macro. I have a statement:
With Selection
code
code
code
.TypeText Text:="." & ActiveDocument.Bookmarks("MtgNo").Range

BTW, is there a difference between:
ActiveDocument.Bookmarks("MtgNo").Range
and
ActiveDocument.Bookmarks("MtgNo").Range.Text

The former works in the macro.

Thanks
David
 
G

Greg Maxey

David,

You could set the bookmark with

{ Set "MtgNo" {Fillin "Type the meeting number"}}

Put it in our document where you want it displayed with {REF MtgNo}

and use this code:

Sub Indent()
Dim MtgNo As String
MtgNo = ActiveDocument.Bookmarks("MtgNo").Range
With Selection
'Code
'Code
.TypeText Text:="." & MtgNo
End With
End Sub

I think I gave a poor example of using .Range.Text
I am just a smidge above a VBA novice and I don't feel qualified to provide
and explanation of the difference.
 

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