Consulting

Results 1 to 9 of 9

Thread: Application.Run method vs. backtick subsitution

  1. #1
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    Application.Run method vs. backtick subsitution

    Hi all,
    I know about using Application.Run MacroName:=xxx to run a subroutine/function called xxx at a particular point in code.

    However, can one do the following in VBA:

    [VBA]
    Dim MyString As String
    Dim AnotherString As String

    MyString = "ActiveDocument.Name"
    AnotherString = "Name = " & Application.Run MacroName:=MyString
    [/VBA]

    where, instead of a subroutine or function, the object I want executed is a variable storing a method (for example).

    The use of the backtick substitution operator is common in UNIX shell programming to get the shell to execute the enclosed command stored in the variable at that point in the code.

    Does VBA have anything similar to the backtick subsitution operation in shell programming?

    Something like:
    [VBA]
    AnotherString = "Name = " & `ActiveDocument.Name`
    [/VBA]



    Ed

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]AnotherString = "Name = " & Application.Run MacroName:=MyString [/vba]would get an error as Application.Run is not a string. As for[vba]AnotherString = "Name = " & `ActiveDocument.Name`[/vba]if the document name was Blah.doc, then AnotherString would be:

    "Name = Blah.doc"

    What, exactly, have you tried so far?

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I think I'm as confused as Gerry.

    I think the answer to your question is probably Yes - but I'm not quite sure what you're trying to do - and I don't know about backticks.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    Object variables

    Hi Gerry and Tony,
    Sorry if my original post was confusing. After I posted my query on Application.Run I read more about Variant data types and I think the correct way to pose my query is:

    "How do I store a property or method name in an object variable for later execution in the same module?"

    It seems that Application.Run is intended for executing subroutines or functions. In UNIX shell programming one also has the ability to execute subroutines and functions. One also can, via the backtick operator, indicate to the interpreting shell program that the contents of a given variable be passed to the shell for execution. This is also, in some sense, what one can do via the system command in C. I was wondering if anything equivalent is possible in VBA.

    Here's where I am so far. I think I need to declare the variable I want to store the property I want executed as an Object:

    [VBA]
    Dim HashValue As Object

    ' ... (code defining range F1 omitted for clarity)
    ' ... (code defining Variant Pname also omitted for clarity)

    Set HashValue = Pname
    F1.InsertAfter = HashValue
    [/VBA]

    When I execute my macro containing the above snippet, I get an error "Object required" both when I declare HashValue As Object and As Document. Note in this case Pname was equal to "ActiveDocument.Name" or "ActiveDocument.path".

    If I change the set statement above to:
    [VBA]
    Set HashValue = ActiveDocument.Pname
    [/VBA]

    and Pname is now either "Name" or "path", I get the error "Object doesn't support this property or method"


    I am looping thru an array of variants whose values are: ActiveDocument.Name and ActiveDocument.path (case #1 above). I am trying to get the content of this variant to be "executed" (not sure if this is correct terminology in VBA) and place the current ActiveDocument name and path in subsequent Text values of a specified range (F1) in the document.

    The purpose of the code is to put 3 lines of text in a new document as follows:

    Subject:
    Filename: Document3
    Pathname: path_to_file

    for a new Word document called Document3 (default name). The string "path_to_file" is the path to Document3. I suspect I am making this much more complex than it is, there is still a lot of VBA for me to learn.

    To sum up then, is it possible in VBA to store in a variable a property name for later execution/interpolation ?

    Hopefully this is a little clearer


    Thanks for your help to this VBA newbie!



    Ed

    ps. Thanks for the tip on how to eliminate the loop I had over the BuiltInDocumentProperties. I didn't realize I could use the Index property of that collection class.

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Is using this 'backtick operator' like using piping in DOS?

  6. #6
    VBAX Regular
    Joined
    Jan 2006
    Posts
    56
    Location

    pipes vs. backticks

    Hi Norie,
    No, a pipe command in both DOS and UNIX allows one to "chain" several commands together since pipe sends the standard output from one command to standard input for a input to a second command.

    The backtick operators then allow one to put this "chain" of piped commands all together into a variable.

    For example (in Perl):

    $MyChain = `cat * | grep :`

    will print the contents of all the files in a given folder (cat command) and then, via pipe (|), will filter out all lines except those containing the colon characater via the grep command.

    Together, the backtick and pipe commands can provide a powerful tool for variable subsitution in shell programming.


    Ed

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    I really think you'll have to explain exactly what you want to do.

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi mansky,

    I'm thoroughly confused by this

    I think I can give you the answer you want - but I don't think it's the answer you need!

    Firstly, for information, try this:

    [VBA]Dim Pname As String
    Dim MyResult as string

    Pname = "Name"
    Myresult = CallByName(ActiveDocument, aaaa, VbGet)
    debug.print myresult

    Pname = "Path"
    Myresult = CallByName(ActiveDocument, aaaa, VbGet)
    debug.print myresult[/VBA]

    And then let's see if we can get towards what you really want.

    1. You should be able just to code the name of the routine you want to execute without Application.Run.

    2. New documents don't have a Path so there's no point in looking for it.

    3. Fields in the document will give you things like document name and path - put them in the template and they will be automatically put in the document when the template is copied as the document base - you might not need the VBA at all.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Tony is right on...again.

    The new document does not HAVE a path. At least not until it is saved.
    To sum up then, is it possible in VBA to store in a variable a property name for later execution/interpolation ?
    Well I suppose, but as Tony mentions you can use them directly anyway...so...hmmmm, why the variable????

Posting Permissions

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