Hi All,

if anyone can help i'd be very greatful.

i have an xml file as below:

----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NoOfPages>3</NoOfPages>
<ID>2082468</ID>
<Attributes>
<Attribute>
<Name>Batch No.</Name>
<Value>40360</Value>
</Attribute>
</Attributes>
<DocumentProperty>
<Originator>Initial MSR Data</Originator>
<Created>2004-10-18T08:07:18.903</Created>
<Declared>2004-10-18T08:07:18.903</Declared>
<FolderID>779</FolderID>
<NumberOfDocuments>3</NumberOfDocuments>
<Cabinet />
<Folder />
</DocumentProperty>
<SavePath>C:\Hub Export\Out</SavePath>
</Document>
----------------------------------------------------------------------------

what i need to do is to put the <FolderID> into a constant within my
script, i was looking at the Readline method, but i'm not sure if this
is right - the line number may change, but the number i want will
always be the <FolderID>.

Thnaks For Any Help.

Jason

Re: Extracting data from an XML to put into a constant by mayayana

mayayana
Tue Mar 25 09:19:13 CDT 2008


I think you can do that using the XML library,
but personally I prefer to keep it simple rather
than getting out the big guns. The following sample
shows a function that extracts a given value from
XML text. It would be used by doing a ReadAll and
then sending that text along with the tag string to
the function, which returns the tag "innertext".

----------------------------------------

Dim s
s = "<Declared>2004-10-18T08:07:18.903</Declared>" & vbCrLf &
"<FolderID>779</FolderID>" & vbCrLf &
"<NumberOfDocuments>3</NumberOfDocuments>"

MsgBox XMLVal(s, "<FolderID>")

Function XMLVal(sText, sTag)
Dim Pt1, Pt2, LTag
XMLVal = ""
LTag = Len(sTag)
Pt1 = InStr(1, sText, sTag, 1)
If Pt1 = 0 Then Exit Function
Pt2 = InStr((Pt1 + 1), sText, "<", 1)
If (Pt2 = 0) Or ((Pt2 - (Pt1 + LTag)) < 1) Then Exit Function
XMLVal = Mid(sText, (Pt1 + LTag), (Pt2 - (Pt1 + LTag)))
End Function

-------------------------------------

You might want to also do a CLng on the result
if it's supposed to be numeric, though the WSH
will probably figure that out if you don't.


>
> if anyone can help i'd be very greatful.
>
> i have an xml file as below:
>
> ----------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <NoOfPages>3</NoOfPages>
> <ID>2082468</ID>
> <Attributes>
> <Attribute>
> <Name>Batch No.</Name>
> <Value>40360</Value>
> </Attribute>
> </Attributes>
> <DocumentProperty>
> <Originator>Initial MSR Data</Originator>
> <Created>2004-10-18T08:07:18.903</Created>
> <Declared>2004-10-18T08:07:18.903</Declared>
> <FolderID>779</FolderID>
> <NumberOfDocuments>3</NumberOfDocuments>
> <Cabinet />
> <Folder />
> </DocumentProperty>
> <SavePath>C:\Hub Export\Out</SavePath>
> </Document>
> --------------------------------------------------------------------------
--
>
> what i need to do is to put the <FolderID> into a constant within my
> script, i was looking at the Readline method, but i'm not sure if this
> is right - the line number may change, but the number i want will
> always be the <FolderID>.
>
> Thnaks For Any Help.
>
> Jason



Re: Extracting data from an XML to put into a constant by Bob

Bob
Tue Mar 25 10:23:44 CDT 2008

wills.jason@gmail.com wrote:
>
> what i need to do is to put the <FolderID> into a constant within my
> script,

Well, you cannot set a contant's value at runtime: constants can only be
set at compile time, which means you can only use literal values when
defining the constants. You have to set the value to a variable instead.
My preference would be to use an xml document, like this:

set xmldoc=createobject("msxml2.domdocument")
xmldoc.load("p:\ath\to\filename.xml")
set node=nothing
set node=xmldoc.selectsinglenode("//FolderID")
in not node is nothing then
msgbox node.text
'or assign it to a variable
end if

You should go to msdn and read the documentation for xpath query syntax
(the "//FolderID" string is a simple xpath query that will be used by
the selectSingleNode method to retrieve the first node with that name).

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 10:52:36 CDT 2008


"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
> wills.jason@gmail.com wrote:
>>
>> what i need to do is to put the <FolderID> into a constant within
>> my
>> script,
>
> Well, you cannot set a contant's value at runtime: constants can
> only be
> set at compile time, which means you can only use literal values
> when
> defining the constants. You have to set the value to a variable
> instead.
> My preference would be to use an xml document, like this:

'you cannot ...' seems incorrect to me. I thought a script could
build strings containing almost any sequence of valid VBScript
statements and use the Execute or ExecuteGlobal statements to do
almost anything that can be done during normal execution of predefined
scripts. Do you have a list of VBScript statements that can't be
executed?

-Paul Randall



Re: Extracting data from an XML to put into a constant by ekkehard

ekkehard
Tue Mar 25 11:01:20 CDT 2008

wills.jason@gmail.com schrieb:
> Hi All,
>
> if anyone can help i'd be very greatful.
>
> i have an xml file as below:
>
> ----------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <NoOfPages>3</NoOfPages>
> <ID>2082468</ID>
> <Attributes>
> <Attribute>
> <Name>Batch No.</Name>
> <Value>40360</Value>
> </Attribute>
> </Attributes>
> <DocumentProperty>
> <Originator>Initial MSR Data</Originator>
> <Created>2004-10-18T08:07:18.903</Created>
> <Declared>2004-10-18T08:07:18.903</Declared>
> <FolderID>779</FolderID>
> <NumberOfDocuments>3</NumberOfDocuments>
> <Cabinet />
> <Folder />
> </DocumentProperty>
> <SavePath>C:\Hub Export\Out</SavePath>
> </Document>
> ----------------------------------------------------------------------------
>
> what i need to do is to put the <FolderID> into a constant within my
> script, i was looking at the Readline method, but i'm not sure if this
> is right - the line number may change, but the number i want will
> always be the <FolderID>.
>
> Thnaks For Any Help.
>
> Jason
A variation on Bob's code:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( ".\jwills.xml" )
Dim sXPath : sXPath = "//FolderID"
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.Load sFSpec
If 0 <> oXDoc.ParseError Then
WScript.Echo oXDoc.ParseError.Reason
Else
Dim sFolderID : sFolderID = "?"
Dim ndFolderID : Set ndFolderID = oXDoc.SelectSingleNode( sXPath )
If ndFolderID Is Nothing Then
WScript.Echo sXPath, "not found."
Else
sFolderID = ndFolderID.Text
WScript.Echo "FolderID", sFolderID, "found."
End If
End If

Re: Extracting data from an XML to put into a constant by ekkehard

ekkehard
Tue Mar 25 11:16:42 CDT 2008

Paul Randall schrieb:
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>> wills.jason@gmail.com wrote:
>>> what i need to do is to put the <FolderID> into a constant within
>>> my
>>> script,
>> Well, you cannot set a contant's value at runtime: constants can
>> only be
>> set at compile time, which means you can only use literal values
>> when
>> defining the constants. You have to set the value to a variable
>> instead.
>> My preference would be to use an xml document, like this:
>
> 'you cannot ...' seems incorrect to me. I thought a script could
> build strings containing almost any sequence of valid VBScript
> statements and use the Execute or ExecuteGlobal statements to do
> almost anything that can be done during normal execution of predefined
> scripts. Do you have a list of VBScript statements that can't be
> executed?
>
> -Paul Randall

While it's not wrong/technically correct to say that you can create
constants at runtime by Execute[Global] strings like

"Const csDIRTY = ""trick"""

e.g.

ExecuteGlobal "Const csDIRTY = ""trick"""
WScript.Echo csDIRTY
On Error Resume Next
csDIRTY = "best practice"
WScript.Echo Err.Description
On Error GoTo 0

works as expected. But users/advocates of such hacks will come to grieve
(and deserve it). The problem with 'on the fly Consts' is that you can't
use them where you would like to use them most:

ExecuteGlobal "Const cnUB = 9"
WScript.Echo cnUB
On Error Resume Next
Dim aFix( cnUB )
WScript.Echo Err.Description
On Error GoTo 0

this will break at *compile time*, because "Dim aFix( cnUB )" is compiled
(fails to compile) before 'ExecuteGlobal "Const cnUB = 9"' is executed.

So you should py attention to Bob's warning and make sure you understand
constants and variables correctly, before you even think of Execute[Global].







Re: Extracting data from an XML to put into a constant by mr_unreliable

mr_unreliable
Tue Mar 25 12:07:49 CDT 2008

Bob Barrows [MVP] wrote:
> Well, you cannot set a contant's value at runtime: constants can only be
> set at compile time,

If one is willing to use the "wsf" construct, then one may
access the constants in an object's typelib by using the
"reference tag".

Here is an example:

--- <code> ---
<job>
<reference object="Excel.Sheet.8" />
<script language="VBScript">
MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
</script>
</job>
--- </code> ---

Copy out the code into a file, and name it "testXLConstant.wsf".

cheers, jw

Re: Extracting data from an XML to put into a constant by Bob

Bob
Tue Mar 25 12:07:25 CDT 2008

Paul Randall wrote:
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>> wills.jason@gmail.com wrote:
>>>
>>> what i need to do is to put the <FolderID> into a constant within
>>> my
>>> script,
>>
>> Well, you cannot set a contant's value at runtime: constants can
>> only be
>> set at compile time, which means you can only use literal values
>> when
>> defining the constants. You have to set the value to a variable
>> instead.
>> My preference would be to use an xml document, like this:
>
> 'you cannot ...' seems incorrect to me. I thought a script could
> build strings containing almost any sequence of valid VBScript
> statements and use the Execute or ExecuteGlobal statements to do
> almost anything that can be done during normal execution of predefined
> scripts.

Actually, I think that even in the context of using ExecuteGlobal, since
a new compiler instance is launched when that method is called, my
statement that constants are defined at compile-time is still
technically correct. And, as ekkehard says, there are scope issues
inherent in using Execute and ExecuteGlobal.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Extracting data from an XML to put into a constant by Bob

Bob
Tue Mar 25 12:13:07 CDT 2008

mr_unreliable wrote:
> Bob Barrows [MVP] wrote:
>> Well, you cannot set a contant's value at runtime: constants can
>> only be set at compile time,
>
> If one is willing to use the "wsf" construct, then one may
> access the constants in an object's typelib by using the
> "reference tag".
>
> Here is an example:
>
> --- <code> ---
> <job>
> <reference object="Excel.Sheet.8" />
> <script language="VBScript">
> MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
> </script>
> </job>
> --- </code> ---
>
> Copy out the code into a file, and name it "testXLConstant.wsf".
>
Interesting, but I don't understand the relevance. You don't seem to be
talking about setting a constant's value at runtime.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 12:40:47 CDT 2008


"ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
news:47e9256b$0$4852$9b4e6d93@newsspool4.arcor-online.net...
> Paul Randall schrieb:
>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>>> wills.jason@gmail.com wrote:
>>>> what i need to do is to put the <FolderID> into a constant within
>>>> my
>>>> script,
>>> Well, you cannot set a contant's value at runtime: constants can
>>> only be
>>> set at compile time, which means you can only use literal values
>>> when
>>> defining the constants. You have to set the value to a variable
>>> instead.
>>> My preference would be to use an xml document, like this:
>>
>> 'you cannot ...' seems incorrect to me. I thought a script could
>> build strings containing almost any sequence of valid VBScript
>> statements and use the Execute or ExecuteGlobal statements to do
>> almost anything that can be done during normal execution of
>> predefined scripts. Do you have a list of VBScript statements that
>> can't be executed?
>>
>> -Paul Randall
>
> While it's not wrong/technically correct to say that you can create
> constants at runtime by Execute[Global] strings like
>
> "Const csDIRTY = ""trick"""
>
> e.g.
>
> ExecuteGlobal "Const csDIRTY = ""trick"""
> WScript.Echo csDIRTY
> On Error Resume Next
> csDIRTY = "best practice"
> WScript.Echo Err.Description
> On Error GoTo 0
>
> works as expected. But users/advocates of such hacks will come to
> grieve
> (and deserve it). The problem with 'on the fly Consts' is that you
> can't
> use them where you would like to use them most:
>
> ExecuteGlobal "Const cnUB = 9"
> WScript.Echo cnUB
> On Error Resume Next
> Dim aFix( cnUB )
> WScript.Echo Err.Description
> On Error GoTo 0
>
> this will break at *compile time*, because "Dim aFix( cnUB )" is
> compiled
> (fails to compile) before 'ExecuteGlobal "Const cnUB = 9"' is
> executed.
>
> So you should py attention to Bob's warning and make sure you
> understand
> constants and variables correctly, before you even think of
> Execute[Global].


Thank you for posting sample code to demonstrate the problem. But it
actually demonstrates a problem that has nothing to do with
Execute[Global]. The following script causes the same error.

Const cnUB = 9
WScript.Echo cnUB
On Error Resume Next
Dim aFix( cnUB )
WScript.Echo Err.Description
On Error GoTo 0

The Dim statement expects an integer constant which is not quite the
same as a Constant with an integer value.

I would like to see sample code that demonstrates problems with using
Execute[Global].

-Paul Randall



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 13:21:13 CDT 2008


"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:uOjszqpjIHA.2268@TK2MSFTNGP02.phx.gbl...
> Paul Randall wrote:
>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>>> wills.jason@gmail.com wrote:
>>>>
>>>> what i need to do is to put the <FolderID> into a constant within
>>>> my
>>>> script,
>>>
>>> Well, you cannot set a contant's value at runtime: constants can
>>> only be
>>> set at compile time, which means you can only use literal values
>>> when
>>> defining the constants. You have to set the value to a variable
>>> instead.
>>> My preference would be to use an xml document, like this:
>>
>> 'you cannot ...' seems incorrect to me. I thought a script could
>> build strings containing almost any sequence of valid VBScript
>> statements and use the Execute or ExecuteGlobal statements to do
>> almost anything that can be done during normal execution of
>> predefined
>> scripts.
>
> Actually, I think that even in the context of using ExecuteGlobal,
> since
> a new compiler instance is launched when that method is called, my
> statement that constants are defined at compile-time is still
> technically correct. And, as ekkehard says, there are scope issues
> inherent in using Execute and ExecuteGlobal.

Perhaps one statement's compile time can be another statement's run
time. I haven't had any problems using Execute and ExecuteGlobal.

I would like to see some sample scripts that demonstrate pitfalls of
using Execute and ExecuteGlobal. I didn't see it in ekkehard's
example.

-Paul Randall



Re: Extracting data from an XML to put into a constant by ekkehard

ekkehard
Tue Mar 25 13:39:10 CDT 2008

Paul Randall schrieb:
> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
> news:47e9256b$0$4852$9b4e6d93@newsspool4.arcor-online.net...
>> Paul Randall schrieb:
>>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>>> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>>>> wills.jason@gmail.com wrote:
>>>>> what i need to do is to put the <FolderID> into a constant within
>>>>> my
>>>>> script,
>>>> Well, you cannot set a contant's value at runtime: constants can
>>>> only be
>>>> set at compile time, which means you can only use literal values
>>>> when
>>>> defining the constants. You have to set the value to a variable
>>>> instead.
[...]
>>> 'you cannot ...' seems incorrect to me. I thought a script could
>>> build strings containing almost any sequence of valid VBScript
>>> statements and use the Execute or ExecuteGlobal statements to do
>>> almost anything that can be done during normal execution of
>>> predefined scripts.
[...]
>>> -Paul Randall
>> While it's not wrong/technically correct to say that you can create
>> constants at runtime by Execute[Global] strings like
[...]
>> works as expected. But users/advocates of such hacks will come to
>> grieve
>> (and deserve it). The problem with 'on the fly Consts' is that you
>> can't
>> use them where you would like to use them most:
>>
>> ExecuteGlobal "Const cnUB = 9"
>> WScript.Echo cnUB
>> On Error Resume Next
>> Dim aFix( cnUB )
>> WScript.Echo Err.Description
>> On Error GoTo 0
>>
>> this will break at *compile time*, because "Dim aFix( cnUB )" is
>> compiled
>> (fails to compile) before 'ExecuteGlobal "Const cnUB = 9"' is
>> executed.
[...]
> Thank you for posting sample code to demonstrate the problem. But it
> actually demonstrates a problem that has nothing to do with
> Execute[Global]. The following script causes the same error.
>
> Const cnUB = 9
> WScript.Echo cnUB
> On Error Resume Next
> Dim aFix( cnUB )
> WScript.Echo Err.Description
> On Error GoTo 0
>
> The Dim statement expects an integer constant which is not quite the
> same as a Constant with an integer value.
>
> I would like to see sample code that demonstrates problems with using
> Execute[Global].
>
> -Paul Randall

Thanks, Paul, for pointing out my error. I forgot/repressed that VBScript
doesn't accept even 'real' Consts when dimming a fixed array. But the
special treatment of 'real' Consts ('moving up' to start of scope) that
isn't emulated by Execute-Consts can be seen from this code

Dim sSecs1 : sSecs1 = 10 * cnSecsPerMin
Dim sSecs2 : sSecs2 = 10 * cnSecsPerHour
ExecuteGlobal "cnSecsPerMin = 60"
Const cnSecsPerHour = 3600
WScript.Echo "sSecs1:", sSecs1
WScript.Echo "sSecs2:", sSecs2

and output:

cscript execconst.vbs
sSecs1: 0
sSecs2: 36000

In my opinion, compiling/executing code generated/loaded at runtime
(Exec[Global] and Eval in VBScript) is a valuable feature of scripting
languages - suited for special purposes and with their own set of
problems (e.g. security). I use ExecGlobal regularly to load 'libraries'
(from a CPP host application, where the src attribute of a script tag
can't be used as in .wsf, .html, or .hta files), but I'd consider creating
variables and/or constants by this method as a second best practice (to
be replaced by using Dictionaries or Classes with restricted access to
memebers most of the time).





Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 13:50:46 CDT 2008


"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:%23Uwu$tpjIHA.248@TK2MSFTNGP04.phx.gbl...
> mr_unreliable wrote:
>> Bob Barrows [MVP] wrote:
>>> Well, you cannot set a contant's value at runtime: constants can
>>> only be set at compile time,
>>
>> If one is willing to use the "wsf" construct, then one may
>> access the constants in an object's typelib by using the
>> "reference tag".
>>
>> Here is an example:
>>
>> --- <code> ---
>> <job>
>> <reference object="Excel.Sheet.8" />
>> <script language="VBScript">
>> MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
>> </script>
>> </job>
>> --- </code> ---
>>
>> Copy out the code into a file, and name it "testXLConstant.wsf".
>>
> Interesting, but I don't understand the relevance. You don't seem to
> be
> talking about setting a constant's value at runtime.

If it is not 'runtime', what time context is the <reference object=
...> construct running in?

I suspect that behind the scenes, it is doing something similar what I
do in the DefineConstants routine I posted last year, but maybe not in
the same time context.

-Paul Randall



Re: Extracting data from an XML to put into a constant by Bob

Bob
Tue Mar 25 14:52:49 CDT 2008

Paul Randall wrote:
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:%23Uwu$tpjIHA.248@TK2MSFTNGP04.phx.gbl...
>> mr_unreliable wrote:
>>> Bob Barrows [MVP] wrote:
>>>> Well, you cannot set a contant's value at runtime: constants can
>>>> only be set at compile time,
>>>
>>> If one is willing to use the "wsf" construct, then one may
>>> access the constants in an object's typelib by using the
>>> "reference tag".
>>>
>>> Here is an example:
>>>
>>> --- <code> ---
>>> <job>
>>> <reference object="Excel.Sheet.8" />
>>> <script language="VBScript">
>>> MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
>>> </script>
>>> </job>
>>> --- </code> ---
>>>
>>> Copy out the code into a file, and name it "testXLConstant.wsf".
>>>
>> Interesting, but I don't understand the relevance. You don't seem to
>> be
>> talking about setting a constant's value at runtime.
>
> If it is not 'runtime', what time context is the <reference object=
> ...> construct running in?

I don't know, I've never seen this before. You tell me.

>
> I suspect that behind the scenes, it is doing something similar what I
> do in the DefineConstants routine I posted last year, but maybe not in
> the same time context.
>
Ok, so you don't know either. Why don't you suspect this type library
isn't being accessed at the script's compile time? Similar to what
occurs when setting a reference to a type library in VB?

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 17:09:08 CDT 2008


"ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
news:47e946d0$0$4749$9b4e6d93@newsspool3.arcor-online.net...
> Paul Randall schrieb:
>> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
>> news:47e9256b$0$4852$9b4e6d93@newsspool4.arcor-online.net...
>>> Paul Randall schrieb:
>>>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>>>> news:%23n413wojIHA.5660@TK2MSFTNGP02.phx.gbl...
>>>>> wills.jason@gmail.com wrote:
>>>>>> what i need to do is to put the <FolderID> into a constant
>>>>>> within my
>>>>>> script,
>>>>> Well, you cannot set a contant's value at runtime: constants can
>>>>> only be
>>>>> set at compile time, which means you can only use literal values
>>>>> when
>>>>> defining the constants. You have to set the value to a variable
>>>>> instead.
> [...]
>>>> 'you cannot ...' seems incorrect to me. I thought a script could
>>>> build strings containing almost any sequence of valid VBScript
>>>> statements and use the Execute or ExecuteGlobal statements to do
>>>> almost anything that can be done during normal execution of
>>>> predefined scripts.
> [...]
>>>> -Paul Randall
>>> While it's not wrong/technically correct to say that you can
>>> create
>>> constants at runtime by Execute[Global] strings like
> [...]
>>> works as expected. But users/advocates of such hacks will come to
>>> grieve
>>> (and deserve it). The problem with 'on the fly Consts' is that you
>>> can't
>>> use them where you would like to use them most:
>>>
>>> ExecuteGlobal "Const cnUB = 9"
>>> WScript.Echo cnUB
>>> On Error Resume Next
>>> Dim aFix( cnUB )
>>> WScript.Echo Err.Description
>>> On Error GoTo 0
>>>
>>> this will break at *compile time*, because "Dim aFix( cnUB )" is
>>> compiled
>>> (fails to compile) before 'ExecuteGlobal "Const cnUB = 9"' is
>>> executed.
> [...]
>> Thank you for posting sample code to demonstrate the problem. But
>> it actually demonstrates a problem that has nothing to do with
>> Execute[Global]. The following script causes the same error.
>>
>> Const cnUB = 9
>> WScript.Echo cnUB
>> On Error Resume Next
>> Dim aFix( cnUB )
>> WScript.Echo Err.Description
>> On Error GoTo 0
>>
>> The Dim statement expects an integer constant which is not quite
>> the same as a Constant with an integer value.
>>
>> I would like to see sample code that demonstrates problems with
>> using Execute[Global].
>>
>> -Paul Randall
>
> Thanks, Paul, for pointing out my error. I forgot/repressed that
> VBScript
> doesn't accept even 'real' Consts when dimming a fixed array. But
> the
> special treatment of 'real' Consts ('moving up' to start of scope)
> that
> isn't emulated by Execute-Consts can be seen from this code
>
> Dim sSecs1 : sSecs1 = 10 * cnSecsPerMin
> Dim sSecs2 : sSecs2 = 10 * cnSecsPerHour
> ExecuteGlobal "cnSecsPerMin = 60"
> Const cnSecsPerHour = 3600
> WScript.Echo "sSecs1:", sSecs1
> WScript.Echo "sSecs2:", sSecs2
>
> and output:
>
> cscript execconst.vbs
> sSecs1: 0
> sSecs2: 36000
>
> In my opinion, compiling/executing code generated/loaded at runtime
> (Exec[Global] and Eval in VBScript) is a valuable feature of
> scripting
> languages - suited for special purposes and with their own set of
> problems (e.g. security). I use ExecGlobal regularly to load
> 'libraries'
> (from a CPP host application, where the src attribute of a script
> tag
> can't be used as in .wsf, .html, or .hta files), but I'd consider
> creating
> variables and/or constants by this method as a second best practice
> (to
> be replaced by using Dictionaries or Classes with restricted access
> to
> memebers most of the time).

OK, now I understand what you are trying to demonstrate; I think this
script demonstrates it better:

'Start of code
MsgBox "PrescanConstant = " & PrescanConstant & vbCrLf & _
"GlobalExecConstant = " & GlobalExecConstant & vbCrLf & _
"OrdinaryVariable = " & OrdinaryVariable

Dim GlobalExecConstant
ExecuteGlobal "GlobalExecConstant = 2"
MsgBox "PrescanConstant = " & PrescanConstant & vbCrLf & _
"GlobalExecConstant = " & GlobalExecConstant & vbCrLf & _
"OrdinaryVariable = " & OrdinaryVariable

Dim OrdinaryVariable: OrdinaryVariable = 3
MsgBox "PrescanConstant = " & PrescanConstant & vbCrLf & _
"GlobalExecConstant = " & GlobalExecConstant & vbCrLf & _
"OrdinaryVariable = " & OrdinaryVariable

Const PrescanConstant = 1
'End of code

Like subroutine and function definitions, the value of constants is
done during the prescan of a script. This means that the subroutine
or function can be called from the first line of code, and that the
value of a constant can also be used from the first line of code.
Ordinary variables and constants defined with execute statements have
no value until their defining statement is executed at run time.

But sometimes, the a constant's value is difficult to come by when you
write the code, but a simple subroutine can determine the names of the
constants and their values. This is what I do in do in the
DefineConstants routine I posted last year; it access the typlib to
get the info and uses ExecuteGlobal to do the definitions. So an
ExecuteGlobal statement executed prior to the constant's being used is
a good solution, especially for a person of limited resources who just
wants to make use of objects already installed on their computer.

-Paul Randall



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 17:22:01 CDT 2008


"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:Op%23rPHrjIHA.1168@TK2MSFTNGP02.phx.gbl...
> Paul Randall wrote:
>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>> news:%23Uwu$tpjIHA.248@TK2MSFTNGP04.phx.gbl...
>>> mr_unreliable wrote:
>>>> Bob Barrows [MVP] wrote:
>>>>> Well, you cannot set a contant's value at runtime: constants can
>>>>> only be set at compile time,
>>>>
>>>> If one is willing to use the "wsf" construct, then one may
>>>> access the constants in an object's typelib by using the
>>>> "reference tag".
>>>>
>>>> Here is an example:
>>>>
>>>> --- <code> ---
>>>> <job>
>>>> <reference object="Excel.Sheet.8" />
>>>> <script language="VBScript">
>>>> MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
>>>> </script>
>>>> </job>
>>>> --- </code> ---
>>>>
>>>> Copy out the code into a file, and name it "testXLConstant.wsf".
>>>>
>>> Interesting, but I don't understand the relevance. You don't seem
>>> to
>>> be
>>> talking about setting a constant's value at runtime.
>>
>> If it is not 'runtime', what time context is the <reference object=
>> ...> construct running in?
>
> I don't know, I've never seen this before. You tell me.
>
>>
>> I suspect that behind the scenes, it is doing something similar
>> what I
>> do in the DefineConstants routine I posted last year, but maybe not
>> in
>> the same time context.
>>
> Ok, so you don't know either. Why don't you suspect this type
> library
> isn't being accessed at the script's compile time? Similar to what
> occurs when setting a reference to a type library in VB?

You are correct -- I don't know. I don't have a reference that
discusses the process of 'running' a .vbs or .wsf file. I'm trying to
create a model in my mind that seems to be consistent with what I see.
I appreciate your input.

In the following example
<job>
<script language="VBScript">
MsgBox("test XL ref. TristateUseDefault = " &
CStr(TristateUseDefault))
</script>
<reference object="wscript.shell" />
</job>

the value of TristateUseDefault is known to the script which indicates
some prescan mechanism is occurring. Having played with multiple
<script > ... </script> blocks in HTAs, I think that each block has
its own prescan, but perhaps the <job>...</job> block has a prescan
too.

-Paul Randall



Re: Extracting data from an XML to put into a constant by Paul

Paul
Tue Mar 25 17:38:43 CDT 2008


"Paul Randall" <paulr901@cableone.net> wrote in message
news:uuFxTasjIHA.5724@TK2MSFTNGP03.phx.gbl...
>
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:Op%23rPHrjIHA.1168@TK2MSFTNGP02.phx.gbl...
>> Paul Randall wrote:
>>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>>> news:%23Uwu$tpjIHA.248@TK2MSFTNGP04.phx.gbl...
>>>> mr_unreliable wrote:
>>>>> Bob Barrows [MVP] wrote:
>>>>>> Well, you cannot set a contant's value at runtime: constants
>>>>>> can
>>>>>> only be set at compile time,
>>>>>
>>>>> If one is willing to use the "wsf" construct, then one may
>>>>> access the constants in an object's typelib by using the
>>>>> "reference tag".
>>>>>
>>>>> Here is an example:
>>>>>
>>>>> --- <code> ---
>>>>> <job>
>>>>> <reference object="Excel.Sheet.8" />
>>>>> <script language="VBScript">
>>>>> MsgBox("test XL ref. xlTextFormat = " & CStr(xlTextFormat))
>>>>> </script>
>>>>> </job>
>>>>> --- </code> ---
>>>>>
>>>>> Copy out the code into a file, and name it "testXLConstant.wsf".
>>>>>
>>>> Interesting, but I don't understand the relevance. You don't seem
>>>> to
>>>> be
>>>> talking about setting a constant's value at runtime.
>>>
>>> If it is not 'runtime', what time context is the <reference
>>> object=
>>> ...> construct running in?
>>
>> I don't know, I've never seen this before. You tell me.
>>
>>>
>>> I suspect that behind the scenes, it is doing something similar
>>> what I
>>> do in the DefineConstants routine I posted last year, but maybe
>>> not in
>>> the same time context.
>>>
>> Ok, so you don't know either. Why don't you suspect this type
>> library
>> isn't being accessed at the script's compile time? Similar to what
>> occurs when setting a reference to a type library in VB?
>
> You are correct -- I don't know. I don't have a reference that
> discusses the process of 'running' a .vbs or .wsf file. I'm trying
> to create a model in my mind that seems to be consistent with what I
> see. I appreciate your input.
>
> In the following example
> <job>
> <script language="VBScript">
> MsgBox("test XL ref. TristateUseDefault = " &
> CStr(TristateUseDefault))
> </script>
> <reference object="wscript.shell" />
> </job>
>
> the value of TristateUseDefault is known to the script which
> indicates some prescan mechanism is occurring. Having played with
> multiple <script > ... </script> blocks in HTAs, I think that each
> block has its own prescan, but perhaps the <job>...</job> block has
> a prescan too.

Sorry - bad example. This is better:
<job>
<script language="VBScript">
MsgBox("test adodb ref. adAffectAllChapters = " &
CStr(adAffectAllChapters))
</script>
//<reference object="ADODB.Stream" />
</job>



Re: Extracting data from an XML to put into a constant by Anthony

Anthony
Thu Mar 27 03:41:37 CDT 2008

"Paul Randall" <paulr901@cableone.net> wrote in message
news:eFlZwTqjIHA.424@TK2MSFTNGP06.phx.gbl...
>
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMco