Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I am trying to run the following code -
Function applySel(selDim,selVal)
ActiveDocument.Fields(selDim).Clear
msgbox("cleared")
ActiveDocument.Fields(selDim).Select selVal
msgbox("selected")
end Function
sub Apple
set s1 = applySel("fruits","apple")
msgbox("apple selected")
end sub
The msgbox is not getting executed, though I see that the selection was successfully made. Please help.
I assume you mean the "msgbox("apple selected")" is not getting executed.
A function should return something. At a minimum, the end of your applySel function should do something like:
applySel = ""
You function call "set s1=..." is expecting an object return, that's incorrect based on your Function applySel coding. I expect your macro is terminating with an "object expected" error. Since you are not returing an object your call should be:
s1 = applySel("fruits","apple")
without the set. Since you are not returning anything, you may want to consider making applySel a SUB instead.
-Rob
Please check setting
Vikas
I assume you mean the "msgbox("apple selected")" is not getting executed.
A function should return something. At a minimum, the end of your applySel function should do something like:
applySel = ""
You function call "set s1=..." is expecting an object return, that's incorrect based on your Function applySel coding. I expect your macro is terminating with an "object expected" error. Since you are not returing an object your call should be:
s1 = applySel("fruits","apple")
without the set. Since you are not returning anything, you may want to consider making applySel a SUB instead.
-Rob
Thanks Rob. Removing 'set' worked - the details were informative.
And yes, I was talking about the "apples selected" message (will edit the other msgbox out).
I have a few other questions -
1. How do I call a sub in another sub?
2. Using
applySel("fruits","apple")
instead of
s1 = applySel("fruits","apple") throws up error - 'Cannot use parentheses when calling a Sub'
whereas when I call another function clearSel() it works fine without assigning it to any variable.
Function clearSel()
ActiveDocument.ClearAll true
end Function
sub
clearSel()
end sub
Hi,
clearSel() is not receiving any arguments so you dont need to assign any variable, but applySel is having parameters so you have assign a value to applySel variable.
Syntax of function:
[Public [Default] | Private] Function name [(arglist)]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function
Example:
Function ShowSum(value1, value2)
Dim sum
' Determine and display the sum.
sum = value1 + value2
MsgBox "The sum is: " & sum
' Set the function's return value.
ShowSum = sum
End Function
Calling Function:
----------------------
Dim retValue
' Call the function with and without the Call keyword.
Call ShowSum(3, 4)
ShowSum 5, 6
' Omitting the Call keyword enables access
' to the function's return value.
retValue = ShowSum(7, 😎
MsgBox "The function returned: " & retValue
Hope this helps you.
Regards,
Jagan.