Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Function - how to use

    Word97,SR-2

    Forgive me if my language is mangled between macro and scripting and code. New to this and confused:
    I have some Function code that I would like to use BUT I can't figure out, after it's in the Code window, how to get it to run.

    Am used to running Macros and I understand this is a different animal, but how do I get it to "run"?

    Thank you for instructions.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Function - how to use

    Do you mean how do you run it from the VB editor? If so, F5 or select Run-->Run Macro from the menu. If you have the immediate window open, you can type in a ? and the function name and hit enter. Or are you asking how to run it from the document?
    Charlotte

  3. #3
    calacuccia
    Guest

    Re: Function - how to use

    Hi Acerf,

    The difference between a function and a macro or procedure is that basically they are the smae, meaning you can put identical code in both types, but a function can only be called from inside a procedure, and returns a value to the Function variable, while a procedure, is a series of commands which are executed as the procedure is executes.

    A simple example:

    This procedure, will first declare the variable, myVar as a string and lengthOfString as a integer, then read some user input from an Inputbox and store the string in the string variable, and then finally put the number of characters into the variable lengthOfString.

    Sub MyProcedure()
    Dim myVar As String
    Dim lengthOfString As Integer

    myVar = InputBox("Please enter your full name")
    lengthOfString = Len(myVar)

    End Sub

    The part Len(myVar) is a VBA built-in function, where an operation with myVar is done, and the result is returned and can be used in procedures.

    Below is an example of a simple function:

    Function doubleLength(myString As String) As Integer
    doubleLength = Len(myString)*2
    End Function

    What will this function do? It will simply return the double of the number of character of the string myString which is passed as a variable to the function.

    Now, the procedure below shows how to use the function:

    Sub TestFunction()
    Dim myLength As Integer
    Dim myVar As String

    myVar = InputBox("Enter yor name")

    myLength = doubleLength(myVar)

    End Sub

    So, ... the function is used, in fact as a separated procedure which will perform a calculation on the variable passed to the function (myVar) in the procedure above, and then return the result and assign it to a variable.

    Hope this helps
    Calacuccia

  4. #4
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    Yes, I do mean how to run it from the document. I had tried running it by F5,nothing. So, I didn't know if I had to select the text for the Function to work on, or what.

    (The function is supposed to spell numeric figures).

    If this can be run from the document, great--how??

    Thank you in advance.

  5. #5
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    Thank you for your explanation.

    Guess what? I still can't figure out what coding to enter before/after the Function routine to get it to work.

    SO, as you will see in my response to Charlotte, I am requesting info how to run it from the document.

    I do appreciate your post.

  6. #6
    calacuccia
    Guest

    Re: Function - how to use

    Can you post the function in question?

    A macro can be ran from the document, using Alt+F8 and selecting the macro in question.

    ...

    Calacuccia

  7. #7
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function - how to use

    Hi,

    It looks as if, what you want is to convert something like a highlighted text from numbers into text- is taht what you're trying to do? Or are you trying just to run the function?

    If you're trying to run from a highlighted text, add a sub to call your function:

    Sub ConvertNum
    selection.text = YourFunction(selection.text)
    end sub
    (This is in its most basic form- you can add bells and whistles)

    A function won't be visible when you press Alt+F8; Subroutines with arguments will not be visible either.

    Alternatively, convert your function to a sub using the selection as I've done above.

    Of course, I may have missed the point, it won't be the first time.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Function - how to use

    Another way of putting it is: you can run a macro directly from a document, but you can't run a function directly from a document.

    To run a function from a document, you first need to create a macro that will call the function. Then when you run the macro from the document, the macro will call the function.

    There's a little more to it than that, in that typically the macro will need to pass arguments to the function. What the arguments are, and how they are passed, really depends on how the function is built, what arguments it requires, and what it does with them.

    So posting the function would help in terms of determining what the macro that will call it, should look like.

    Hope this helps.

  9. #9
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function - how to use

    Something else here. A person in the Word forum is looking for a routine to convert numbers to words.

    Is that what you've done? Is that something you can help with?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Function - how to use

    Looks like the same person.[img]/w3timages/icons/wink.gif[/img]
    Most likely they did get hold of the code they were asking about in the Word forum, in the form of a function. The trick now is wrapping the function in something they can use.

    I'd been meaning to reply to that post in the Word forum, to the effect that there are a number of convert numbers to words macros floating around. One can be found in VBA Developer's Handbook by Ken Getz and Mike Gilbert.

    I may still have a copy of a different one of unknown origin, on my PC at work, that someone once asked me to customize.
    It works in the way you described in your previous post, where you select some numbers and then run the macro.
    If I can find it tomorrow I'll post it.
    Phil R. possibly may have a copy of it as it was originally posted on Daily Word Tips - he keeps everything![img]/w3timages/icons/clever.gif[/img].

    Gary

  11. #11
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function - how to use

    Whoops, I forgot to check for the poster in the Word forum!

    Please ignore my previous post.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  12. #12
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    Geoff -

    No, you haven't missed the point at all!

    I tried, when sleepy, your suggestion, but when it got to substituting the Function I had with your line, I got tangled up. Will try again. I do thank you for your explanation and help!
    ACM

  13. #13
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    YUP - 'twas I!

    I think I have the code you mean, from Word Tips, but as I explained elsewhere, it's in WordBasic so the de###### stops all the time.

    But, I'd like very much to see the macros you might have the time to "unfloat." I had gone on the boards looking for such a macro before I started these threads, to no avail.

    So, many thanks, to you and all.

  14. #14
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    Afternoon -

    I posted the Function on Word board, to Andrew.

    Again, my thanks.

  15. #15
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Function - how to use

    Thank you for your clear explanation. I sorta "gete it," but writing it is another animal, for me.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •