PDA

View Full Version : Saving a command as a Variable.



mattreade
04-15-2011, 01:34 PM
Is there anyway to save a command in a variable so that in can be evaluated later?
For instance options.revisedLinesColor equals 0 on my computer.

I would like to save the command "options.revisedLinesColor" to an array and run it again later.

Below is a rough try that did not work. In Excel I have heard of an Eval function but nothing in Word VBA.

Sub testcommand()
Dim varCommand As Variant
varCommand = "'Options.RevisedLinesColor'"
MsgBox Options.RevisedLinesColor & " = " & Mid((varCommand), 2, Len(varCommand) - 2)
End Sub

What I want is 0 = 0
What I get is 0 = Optioins.RevisedLineColor


Thanks for any ideas.

Matt

Frosty
04-16-2011, 11:25 AM
Really don't know what you mean by this question, after reading it a few times. And your pseudo code isn't helping. I don't have access to vba help right now, but what is Options.RevisedLinesColor? That's a *setting* right? I have a feeling it's a long data type. You want the message box to show 0 = 0? Then don't store Options.RevisedLinesColor as a string... store the value of it (which is 0). Something like:

Sub Test
dim lLineColor as Long

lLineColor = Options.RevisedLinesColor
msgbox Options.RevisedLinesColor & " = " & lLineColor

'(you don't even need the cstr functions above, because vba will do it for you)
End Sub
But that seems too easy...

Why don't you describe what you want to do, because I think you might be confusing what I typically call the "data" and the "doings"...

That said, a good topic to look up would be scope and variables. You don't need to store stuff in arrays, necessarily, although that is one way to do it. However, if you're looking to store something like the initial value of Options.RevisedLineColor across multiple procedures (because that value would be changed), you could store it in a global variable... or you could pass it as a parameter through your procedures. The proper approach (and I'm typically not a fan of a lot of global variables), would depend on what you want to do.

It kind of sounds like you've decided on a methodology and you want help doing it... I would suggest taking a step back and explaining what you want to do.

mattreade
04-16-2011, 01:17 PM
Hello Frosty, thanks for the reply.

What I want to do is save a large group of "settings" and the name of those settings in a XML file. Then reading in the XML file I could read the name of the setting (Options.RevisedLineColor) get its current value and compare it to the value saved in the XML file.

Yes, I could just use the code I used to create the XML file in the first place, but thought it would be nice to just read in the name of the setting from the XML file and get the current value of that setting.

I don't have the actual XML file with me but it looks like:

<Root>
<Option>
<Name>Options.RevisedLineColor</Name>
<Default>0</Default>
<Description>Color used in Redline document.</Description>
</Option>
.... additional options...
</Root>

The reason I am doing this is to placate some users who have a tendency to start changing settings when they do not know how to do some task. I want to be able to walk up to the computer and compare the settings from the last time I ran the macro to what is currently set.

Well thanks for asking.
Hope this helps.
Matt

Frosty
04-16-2011, 01:25 PM
Gotcha. There is no way (that I know of) to actually store the object name automatically (i.e., you can't do some kind of For Each... Loop for all of the items in the Options object).

So, I believe you'd have to manually store it... probably as a multi-dimensional array along the lines of:

myOptionsArray(0,0) = "Options.RevisedLineColor"
myOptionsArray(0,1)= "0"

And then you can build your xml from there. But you'll still have to do you translation back...

Options.RevisedLineColor = myOptionsArray(0,1)

There is no kind of function along the lines of (the following is a made up vba function)...
EvalToActualVBACommand(myOptionsArray(0,0)) = myOptionsArray(0,1)

...so you're basically just making your code readable. It would probably be easier to leave it out of the xml entirely, and simply store your "Company Defaults" in a particular location... and then read from those in some kind of UserScrewedUpSettingsSoRestoreDefaults procedure.

mattreade
04-17-2011, 09:00 AM
Frosty, your "made up vba function" is exactly what I am looking for....
Wish you could really make one...
Your answer does help, in that I will not spend a lot more time trying to make that work.
Now I will be looking for the fastest way to lookup my "company defaults" stored in an array and compare them to the current setting. That is where it would have been nice to have your "Made Up" function, because I could have just walked down the array and evaluate the "command" one row at a time.

Thanks again.
Matt

Frosty
04-17-2011, 09:51 AM
I'm not at a computer, but just verify that application.run doesn't do what you're looking for. I think it's only for procedures, not built in functions, and there are issues with using it regularly (not the least of which is the ability to error trap), but there might be some way of using it.