![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Variable within Object Reference Line Is there a way to use a variable within an object reference line? The particular situation is with an "InternetExplorer.Application" object, but I think this is really just a generic vbscript question that could apply to other situations. I would like to pass an argument to be used within an object reference for a script that manipulates some web form data. The argument would come from the command line or be passed into a function. For example, I'd like to replace something like: Set IE = CreateObject("InternetExplorer.Application") Set frmFieldA = IE.Document.Form1.Field1 with the functional equivilant of: arg1 = Wscript.Arguments(0) arg2 = Wscript.Arguments(1) Set IE = CreateObject("InternetExplorer.Application") Set frmFieldA = IE.Document.<arg1>.<arg2> I've tried basic string concatentation with &, but since VBS is expecting a non-quoted literal object reference, I'm not sure if this is possible. Has anyone done something similar with whatever object you were using? Thanks in advance. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Variable within Object Reference Line Set frmFieldA = IE.Document.Forms(arg1).Elements(arg2) Tim "tReg" <tReg@xxxxxx> wrote in message news:BEEE9701-657E-407C-94B5-7642A6814C22@xxxxxx Quote: > Is there a way to use a variable within an object reference line? The > particular situation is with an "InternetExplorer.Application" object, but > I > think this is really just a generic vbscript question that could apply to > other situations. > > I would like to pass an argument to be used within an object reference for > a > script that manipulates some web form data. The argument would come from > the > command line or be passed into a function. For example, I'd like to > replace > something like: > > Set IE = CreateObject("InternetExplorer.Application") > Set frmFieldA = IE.Document.Form1.Field1 > > with the functional equivilant of: > > arg1 = Wscript.Arguments(0) > arg2 = Wscript.Arguments(1) > Set IE = CreateObject("InternetExplorer.Application") > Set frmFieldA = IE.Document.<arg1>.<arg2> > > I've tried basic string concatentation with &, but since VBS is expecting > a > non-quoted literal object reference, I'm not sure if this is possible. > Has > anyone done something similar with whatever object you were using? > > Thanks in advance. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Variable within Object Reference Line Or, possibly: arg1 = Wscript.Arguments(0) arg2 = Wscript.Arguments(1) Set IE = CreateObject("InternetExplorer.Application") execute "Set frmFieldA = IE.Document." & arg1 & "." & arg2 In either case, you would likely want to also do some error checking. /Al "Tim Williams" <timjwilliams at gmail dot com> wrote in message news:eFkPcK39IHA.2324@xxxxxx Quote: > Set frmFieldA = IE.Document.Forms(arg1).Elements(arg2) > > Tim > > > "tReg" <tReg@xxxxxx> wrote in message > news:BEEE9701-657E-407C-94B5-7642A6814C22@xxxxxx Quote: >> Is there a way to use a variable within an object reference line? The >> particular situation is with an "InternetExplorer.Application" object, >> but I >> think this is really just a generic vbscript question that could apply to >> other situations. >> >> I would like to pass an argument to be used within an object reference >> for a >> script that manipulates some web form data. The argument would come from >> the >> command line or be passed into a function. For example, I'd like to >> replace >> something like: >> >> Set IE = CreateObject("InternetExplorer.Application") >> Set frmFieldA = IE.Document.Form1.Field1 >> >> with the functional equivilant of: >> >> arg1 = Wscript.Arguments(0) >> arg2 = Wscript.Arguments(1) >> Set IE = CreateObject("InternetExplorer.Application") >> Set frmFieldA = IE.Document.<arg1>.<arg2> >> >> I've tried basic string concatentation with &, but since VBS is expecting >> a >> non-quoted literal object reference, I'm not sure if this is possible. >> Has >> anyone done something similar with whatever object you were using? >> >> Thanks in advance. > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Variable within Object Reference Line No luck with the above suggestions. Here's a generic example of the scenario where I'm trying to insert a variable into an object reference: --------------------- Set objFSO = CreateObject("Scripting.FileSystemObject") strFileMethod = "OpenTextFile" Set objFile = objFSO. & strFileMethod & ("Text_File.txt", 1, False) ------------------------- And a quick IE object example: -------------------------- Set IE = CreateObject("InternetExplorer.Application") IE.Height = 600 IE.Width = 800 IE.Left = 0 IE.Top = 0 IE.Visible = 1 IE.Navigate "http://www.google.com" Wscript.Sleep 2000 Do While IE.Busy Loop 'This echo will work IE.Document.F.Q.Value = "Foo" Wscript.Echo IE.Document.F.Q.Value 'This echo will give an error frmName = "F.Q" Wscript.Echo IE.Document.(frmName).Value --------------------------- I'm running into VBScript compilation error: Expected identifier in either case. My specific scenarios are obviously more complicated than the above, but the general principle is the same. Any ideas on a way to make this work? |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Variable within Object Reference Line "tReg" wrote: Quote: > No luck with the above suggestions. > > Here's a generic example of the scenario where I'm trying to insert a > variable into an object reference: > > --------------------- > Set objFSO = CreateObject("Scripting.FileSystemObject") > > strFileMethod = "OpenTextFile" > Set objFile = objFSO. & strFileMethod & ("Text_File.txt", 1, False) > ------------------------- Dunbar was *SPECIFIC* to the InternetExplorer.Application object. The FileSystemObject has no similar "selector" capabilities, so of course you can't use that technique. Now, Mr. Dunbar's technique *should* have worked, if you followed it correctly. To wit: strFileMethod = "OpenTextFile" Execute "Set objFile = objFSO." & strFileMethod & "(""Text_File.txt"", 1, False)" However... Execute is a very very SLOW thing to use, and you should avoid it in "mission critical" situations. Besides, it really makes no sense to use it in this particular scenario, because there isn't any other metho on FileSystemObject that has the same "signature" as OpenTextFile. I'm *guessing* that you wanted to choose between OpenTextFile and CreateTextFile, no? But where the arguments to OpenTextFile are in the form: object.OpenTextFile(filename[, iomode[, create[, format]]]) For CreateTextFile you need object.CreateTextFile(filename[, overwrite[, unicode]]) So you really need completely different arguments, not just a different method name. Which means this whole exercise is nonsense, really. Do it the *right* way: If strFileMethod = "OpenTextFile" Then Set objFile = FSO.OpenTextFile( filename, 1, False ) Else Set objFile = FSO.CreateTextFile( filename, False ) End If It may *look* like more code, but it's actually a *MUCH* shorter path of execution (by milliseconds, almost surely) and has the advantage that it will work. Try to avoid Execute unless you are desparate. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Variable within Object Reference Line I appreciate all the quick replies to my thread. My example scenarios were given as a general vbscript language question in order to determine if a variable can be included within an object reference line that isn't necessarily expecting concatenated input. I understand the actual practical application of my FileSystemObject doesn't serve any specific real world value, but that wasn't necessarily the point. In other object reference situations it might, and I figured the code would basically look the same (since the vbscript error messages are the same), so I provided it as a basic demonstration. I couldn't get Tim's suggestion to work with named elements within my IE form example, but IE object scripting isn't my strong suit. If the method he provided can be made to work within my Google search example replacing the named object reference to the form, I'd like to see it. Thanks again. |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Variable within Object Reference Line Can you show the exact code you tried with IE (and maybe also the relevant HTML source) ? For google search: IE.document.forms("f").elements("q") or IE.document.getElementById("q") should both work. Tim "tReg" <tReg@xxxxxx> wrote in message news:1B90D56E-3D3E-4805-8429-26E99CE55443@xxxxxx Quote: >I appreciate all the quick replies to my thread. > > My example scenarios were given as a general vbscript language question in > order to determine if a variable can be included within an object > reference > line that isn't necessarily expecting concatenated input. > > I understand the actual practical application of my FileSystemObject > doesn't > serve any specific real world value, but that wasn't necessarily the > point. > In other object reference situations it might, and I figured the code > would > basically look the same (since the vbscript error messages are the same), > so > I provided it as a basic demonstration. > > I couldn't get Tim's suggestion to work with named elements within my IE > form example, but IE object scripting isn't my strong suit. If the method > he > provided can be made to work within my Google search example replacing the > named object reference to the form, I'd like to see it. > > Thanks again. > |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Variable within Object Reference Line "Old Pedant" <OldPedant@xxxxxx> wrote in message news:40C66953-026F-4582-8E02-459E1909E97C@xxxxxx Quote: > > > "tReg" wrote: > Quote: >> No luck with the above suggestions. >> >> Here's a generic example of the scenario where I'm trying to insert a >> variable into an object reference: >> >> --------------------- >> Set objFSO = CreateObject("Scripting.FileSystemObject") >> >> strFileMethod = "OpenTextFile" >> Set objFile = objFSO. & strFileMethod & ("Text_File.txt", 1, False) >> ------------------------- > Different objects require different methods. The scheme suggested by Mr. > Dunbar was *SPECIFIC* to the InternetExplorer.Application object. > > The FileSystemObject has no similar "selector" capabilities, so of course > you can't use that technique. > > Now, Mr. Dunbar's technique *should* have worked, if you followed it > correctly. > > To wit: > > strFileMethod = "OpenTextFile" > Execute "Set objFile = objFSO." & strFileMethod & "(""Text_File.txt"", > 1, > False)" > > However... > > Execute is a very very SLOW thing to use, and you should avoid it in > "mission critical" situations. reliability. No, I'm not saying there is anything intrinsically wrong with the execute statement itself. But the code it attempts to execute could be buggy, *especially* if input values are plugged in without first verifying their validity. /Al Quote: > Besides, it really makes no sense to use it in this particular scenario, > because there isn't any other metho on FileSystemObject that has the same > "signature" as OpenTextFile. > > I'm *guessing* that you wanted to choose between OpenTextFile and > CreateTextFile, no? > > But where the arguments to OpenTextFile are in the form: > object.OpenTextFile(filename[, iomode[, create[, format]]]) > For CreateTextFile you need > object.CreateTextFile(filename[, overwrite[, unicode]]) > > So you really need completely different arguments, not just a different > method name. > > Which means this whole exercise is nonsense, really. > > Do it the *right* way: > > If strFileMethod = "OpenTextFile" Then > Set objFile = FSO.OpenTextFile( filename, 1, False ) > Else > Set objFile = FSO.CreateTextFile( filename, False ) > End If > > It may *look* like more code, but it's actually a *MUCH* shorter path of > execution (by milliseconds, almost surely) and has the advantage that it > will > work. > > Try to avoid Execute unless you are desparate. > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Powershell Object Reference | PowerShell | |||
| Variable assignment and clone or objet reference | PowerShell | |||
| datalist -- Object reference not set to an instance of an object. | .NET General | |||
| False IE doc body error - "Object reference not set to an instance of an object" | PowerShell | |||
| passing variable by reference | PowerShell | |||