PDA

View Full Version : Application.Run method vs. backtick subsitution



mansky
02-06-2006, 12:56 PM
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:


Dim MyString As String
Dim AnotherString As String

MyString = "ActiveDocument.Name"
AnotherString = "Name = " & Application.Run MacroName:=MyString


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:

AnotherString = "Name = " & `ActiveDocument.Name`




Ed

fumei
02-07-2006, 09:16 AM
AnotherString = "Name = " & Application.Run MacroName:=MyString would get an error as Application.Run is not a string. As forAnotherString = "Name = " & `ActiveDocument.Name`if the document name was Blah.doc, then AnotherString would be:

"Name = Blah.doc"

What, exactly, have you tried so far?

TonyJollans
02-07-2006, 11:08 AM
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.

mansky
02-07-2006, 12:56 PM
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:


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


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:

Set HashValue = ActiveDocument.Pname


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 http://vbaexpress.com/forum/images/smilies/think.gif


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.

Norie
02-07-2006, 01:09 PM
Is using this 'backtick operator' like using piping in DOS?

mansky
02-07-2006, 03:04 PM
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

Norie
02-07-2006, 03:28 PM
I really think you'll have to explain exactly what you want to do.

TonyJollans
02-07-2006, 04:42 PM
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:

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

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.

fumei
02-07-2006, 07:05 PM
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????