Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Input box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Rose - 08 Apr 2005 16:51 GMT
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
Greg - 08 Apr 2005 17:11 GMT
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.
David Rose - 08 Apr 2005 17:21 GMT
Thanks Greg.

The code works and the explanation was helpful.

David

> David,
>
[quoted text clipped - 23 lines]
> the Variable.  Next time you run the macro the IF condition is not met
> so the the code is skipped.
David Rose - 08 Apr 2005 17:51 GMT
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

> David,
>
[quoted text clipped - 23 lines]
> the Variable.  Next time you run the macro the IF condition is not met
> so the the code is skipped.
Greg Maxey - 08 Apr 2005 20:08 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> I just realized that elsewhere in the document I have a FILLIN prompt
> which is bookmarked as MtgNo. The FILLIN is performed before the
[quoted text clipped - 31 lines]
>> in the Variable.  Next time you run the macro the IF condition is
>> not met so the the code is skipped.
David Rose - 08 Apr 2005 21:32 GMT
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

> David,
>
[quoted text clipped - 52 lines]
>>> in the Variable.  Next time you run the macro the IF condition is
>>> not met so the the code is skipped.
Greg Maxey - 08 Apr 2005 21:47 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg
> I realized that the IF etc. is not required if I could use the
[quoted text clipped - 81 lines]
>>>> in the Variable.  Next time you run the macro the IF condition is
>>>> not met so the the code is skipped.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.