SUB test(anyparam) -- VBA does not allow params!?

S

Sven

Hi group,
this one is strange: I want to write a SUB-procedure with one parameter like
this:
Sub sh_info(shselect As Integer)
Select Case shselect
Case 1
msg = "Some message text"
Case 2
msg = "Different message"
End Select
End Sub

But, the proc won't execute. Not by pressing F5 (nothing happens) nore by
calling it from a different sub. As there are billions of examples on subs
with parameters, this should work. Why not? Security problems?
(Visual Basic 6.0, Word 2003 SP2, XP prof. SP2)

Thanks in advance for any hint
Sven
 
J

Jonathan West

Sven said:
Hi group,
this one is strange: I want to write a SUB-procedure with one parameter
like
this:
Sub sh_info(shselect As Integer)
Select Case shselect
Case 1
msg = "Some message text"
Case 2
msg = "Different message"
End Select
End Sub

But, the proc won't execute. Not by pressing F5 (nothing happens) nore by
calling it from a different sub. As there are billions of examples on subs
with parameters, this should work. Why not? Security problems?
(Visual Basic 6.0, Word 2003 SP2, XP prof. SP2)

Thanks in advance for any hint

It probably is executing, but it doesn't do anything. You are setting a
local variable (msg) to one of two values, and then exiting without doing
anything with that variable. If you are trying to display a message box then
change the code to this

Sub sh_info(shselect As Integer)
Select Case shselect
Case 1
MsgBox "Some message text"
Case 2
MsgBox "Different message"
End Select
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
 
D

Dave Lett

Yes, you can pass arguments to other routines. I would call your routine
this way
Public Sub Test ()
sh_info shselect:=1
sh_info shselect:=2
End Sub

Sub sh_info(shselect As Integer)
Dim msg As String
Select Case shselect
Case 1
msg = "Some message text"
Case 2
msg = "Different message"
End Select
MsgBox msg
End Sub

HTH,
Dave
 
S

Sven

Thanks Dave,
this was what I expected.
But - on the system I am working on, the sub sh_info doesn't show up at all.
I can't see it in the object inspector or anywhere else. Just the code is
there. The very moment I delete the param decl. in the brackets, the macro is
"there" again and can be called. This is true to *any* example I copied into
my file.
We had some huge security updates (Microsoft security patches) here last
week, may be that's why?
 
S

Sven

Thanks, Jonathan,
I posted into "Beginners" because I am new to VBA - but not to programming
(and I thought it's me doing something wrong).
The sub I posted was just a short example of what I am trying to do. But
even your example is not working. When I delete any param declaration (like
sub sh_info()) then it's fine.

Btw, thx to all for that VERY fast reply. Great!
 
R

Russ

Hi Sven,
I know this might be grasping at straws, but here's what I would try:

1. Export all VBA Code, especially with "argument list examples."
2. Delete all code in VBA editor, now that it has been exported.
3. Close Word.
4. Open that exported file(s) in Note Pad and resave under new name.
(to hopefully help cleanup any corruption in code)
5. Find and rename the Normal.dot to Normal_old.dot to have Word start
fresh.
6. Reopen Word with a new blank document (not your old template) and VBA
editor.
7. Import that new file into VBA editor and try to compile code and if OK,
try running it.
8. If OK, save as new Template, not as Normal.dot
 
J

Jonathan West

Sven said:
Thanks, Jonathan,
I posted into "Beginners" because I am new to VBA - but not to programming
(and I thought it's me doing something wrong).
The sub I posted was just a short example of what I am trying to do. But
even your example is not working. When I delete any param declaration
(like
sub sh_info()) then it's fine.

Btw, thx to all for that VERY fast reply. Great!

Hi Sven,

OK, some of these suggestions might insult your intelligence - please
forgive me if they do. The difficulty is that an "it doesn't work" problem
is very hard to troubleshoot, especially when I can't see what you are
doing.

First of all, set a breakpoint on the first line of the routine. Do this by
positioning the cursor on that line & pressing F9. The breakpoint will show
up as a red highlight on that line.

Then open the Immediate window, by pressing Ctrl-G.

In the Immediate window, type the following

sh_info 1

and then press Enter.

The line where you placed the breakpoint should now have a yellow highlight,
indicating that execution has stopped at that line. Now press F8. This will
move the code on by one step. Continue to do so until the routine is exited.

Now remove the breakpoint. (Position the cursor on the line and press F9
again). Type the same text again into the Immediate window, and press Enter.
The whole routine should now run.

Let me know the results of this, and whether and where you see anything
different.


--
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
 
K

Karl E. Peterson

Sven said:
Hi group,
this one is strange: I want to write a SUB-procedure with one
parameter like this:
Sub sh_info(shselect As Integer)
Select Case shselect
Case 1
msg = "Some message text"
Case 2
msg = "Different message"
End Select
End Sub

But, the proc won't execute. Not by pressing F5 (nothing happens)
nore by calling it from a different sub. As there are billions of
examples on subs with parameters, this should work. Why not? Security
problems? (Visual Basic 6.0, Word 2003 SP2, XP prof. SP2)

Coming from ClassicVB myself, this was confusing to me at first as well.
You're right. Word will *not* execute a parameterized procedure from either
the Macros menu or by pressing F5 in the IDE. You must always start
execution with a parameterless procedure, as Dave suggested. Just the way
it is -- think about it, how _would_ you pass the parameters from a menu
selection?
 
J

Jean-Guy Marcil

Sven was telling us:
Sven nous racontait que :
Thanks Dave,
this was what I expected.
But - on the system I am working on, the sub sh_info doesn't show up
at all. I can't see it in the object inspector or anywhere else. Just
the code is there. The very moment I delete the param decl. in the
brackets, the macro is "there" again and can be called. This is true

How and from where are you calling the sh_info sub?
Dave's code works as expected on my machine.
to *any* example I copied into my file.
We had some huge security updates (Microsoft security patches) here
last week, may be that's why?


Not related, at least, I doubt it very much.


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

Sven

Thanks, Karl, that's the solution!

The following works fine if "main()" is called from the macromenu:
Sub wparam(x)
Debug.Print x
End Sub

Sub main()
wparam ("print this")
End Sub

I didn't expect to have some "automatic" parameter dialog coming up when
choosing a "parameterized macro", but I expected such in the debugger. Ok, it
isn't there...
But: Everything works fine when calling "main" from that direct window. But
I can't call "wparam 2" from the direct window ("Sub or function not
defined"-error). And that is strange, isn't it?

Thanks, Jonathan for the "direct window" hint (didn't know that yet)
Thanks Russ for the "cleaning" instructions. I tried that first, but it
didn't help.
Lbnl, thanks to Jean-guy (it's too easy to blame any problem to the IT
security people :)
 
J

Jean-Guy Marcil

Sven was telling us:
Sven nous racontait que :
Lbnl, thanks to Jean-guy (it's too easy to blame any problem to the IT
security people :)

lol

And I love your signature!
I wonder how often people try to point out that there is a mistake?

Recently there was a discussion about shibboleths in another newsgroup...
your signature is a perfect example!

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

Russ

Hi Sven,
Message within text...
Thanks, Karl, that's the solution!

The following works fine if "main()" is called from the macromenu:
Sub wparam(x)
Debug.Print x
End Sub

Sub main()
wparam ("print this")
End Sub

I didn't expect to have some "automatic" parameter dialog coming up when
choosing a "parameterized macro", but I expected such in the debugger. Ok, it
isn't there...
But: Everything works fine when calling "main" from that direct window. But
I can't call "wparam 2" from the direct window ("Sub or function not
defined"-error). And that is strange, isn't it?

Thanks, Jonathan for the "direct window" hint (didn't know that yet)
Thanks Russ for the "cleaning" instructions. I tried that first, but it
didn't help.

I gave the "cleaning" instructions because I thought you had tried Dave
Lett's method like at the start of this message and it *didn't* work. I
missed the fact that you *weren't* trying to call a "parameterized macro"
from within running code, because I thought through running code (or
immediate window) was the only way to give it the parameters it was looking
for.
 
K

Karl E. Peterson

Hi Sven --
Thanks, Karl, that's the solution!

The following works fine if "main()" is called from the macromenu:
Sub wparam(x)
Debug.Print x
End Sub

Sub main()
wparam ("print this")
End Sub

I didn't expect to have some "automatic" parameter dialog coming up
when choosing a "parameterized macro", but I expected such in the
debugger. Ok, it isn't there...
But: Everything works fine when calling "main" from that direct
window.

Good deal. As soon as I pressed Send on that, I kicked myself for not
explicitly comparing the situation to Sub Main in ClassicVB -- no way to
pass it parameters, either, eh?
But I can't call "wparam 2" from the direct window ("Sub or
function not defined"-error). And that is strange, isn't it?

That's strange, yep. Is the routine declared with Public (trust *no*
defaults!)? Maybe try:

call wparam(2)

This works fine, here:

Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo Hell
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Input(LOF(hFile), #hFile)
Close #hFile
Hell:
End Function

Then, in the Immediate window:

?readfile("c:\adaptec.err.txt")

Just tested it in Access, but it's the same VBE either way. <shrug>

Later... Karl
 
S

Sven

Hi again,
may be I should explain, what I am (still) intending to do:
on my document I have some form fields. These must be edited by the user
prior to printing. To let her/him know which kind of data is expected, I open
a balloon tip with some hints. I wanted to write *one* function accepting
the fields id as a parameter in order to display the appropriate message. Now
it seems like I have to copy my *sub fieldinfo()* as many times as I have
numbers of fields (fieldinfo1(), fieldinfo2() ...). There must be a more
elegant way?!

Thx, guys!
 
J

Jean-Guy Marcil

Sven was telling us:
Sven nous racontait que :
I have to admit that I just pirated that signature... I love Simon
Singh's books and found some good jokes on his site:
http://www.simonsingh.com/Jokes_Cartoons.html

And now, I go and find out what *shibboleths* means... :O)


LOL
And you thought that you would only learn about coding in this group!

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

Jean-Guy Marcil

Sven was telling us:
Sven nous racontait que :
Hi again,
may be I should explain, what I am (still) intending to do:
on my document I have some form fields. These must be edited by the
user prior to printing. To let her/him know which kind of data is
expected, I open a balloon tip with some hints. I wanted to write
*one* function accepting the fields id as a parameter in order to
display the appropriate message. Now it seems like I have to copy my
*sub fieldinfo()* as many times as I have numbers of fields
(fieldinfo1(), fieldinfo2() ...). There must be a more elegant way?!

Or why not use the formfield built-in Help system?
If you go to the formfield dialog, there is a button in the bottom left
corner labelled "Add Help Text...".
Click on that and you can use Auto-Text (useful if the same help is needed
for different fields) or type your own. If it is short, the help can appear
in the status bar, or user can press F1 to have a pop-up with the help text.
No code needed.

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

Sven

Hi Jay,
thank you very much! Not quite self explanatory (bookmark? textfield?), but
it works!
Greetinx,
Sven
 
J

Jay Freedman

Hi Sven,

I'm glad you got it working.

In case you're still reading, maybe I can clear up your confusion: Every
form field (text, check box, or dropdown) has a built-in bookmark that
serves as its identifier. When you open the form field's Properties dialog,
the Bookmark item in the dialog lets you change that name.

The check box and dropdown types of form field are well-behaved. That is, if
the Selection object contains one of these fields, the VBA expression
Selection.FormFields(1) points to the form field. Then
Selection.FormFields(1).Name contains the form field's name.

The text form field is not well behaved. That is, if the Selection object
contains a text form field, then the expression Selection.FormFields(1)
causes an error and the value of Selection.FormFields(1).Count is zero. The
workaround in the article consists of getting the name of the form field's
built-in bookmark from the Selection.Bookmarks collection.

The workaround shouldn't be necessary, but it's been required in every
version of Word since the introduction of VBA in Word 97.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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