I am a novice at coding in vbscript and I have a problem that has been
bothering me for about a week. I am trying to write a script that will
open a text file and import it into excel (snippet of the code is
below). When I try to use the opentext function as shown below, I
received an error stating that "opentext method of wrokbooks class
failed" can anyone please explain to me why this is happening adn how
to correct it. I am sure the file exist in the same directory as the
script.


Dim excelprog
Set excelprog = WScript.CreateObject("Excel.Application")
excelprog.Application.Visible = True

excelprog.Workbooks.OpenText ("c:\test.txt", 437, 1, xlDelimited, 1,
False, False, True, False, False, False, , , comma, comma)

Re: Help with opentext function in vbscript by Groups

Groups
Thu Dec 01 19:50:44 CST 2005

There are a few problems. They all reside with the last line. Here is
the syntax for OpenText.

Sub OpenText(Filename As String, [Origin], [StartRow], [DataType],
[TextQualifier As XlTextQualifier = xlTextQualifierDoubleQuote],
[ConsecutiveDelimiter], [Tab], [Semicolon], [Comma], [Space], [Other],
[OtherChar], [FieldInfo], [TextVisualLayout], [DecimalSeparator],
[ThousandsSeparator], [TrailingMinusNumbers], [Local])

If you are simply trying to open the file then just change it to the
following.
excelprog.Workbooks.OpenText("c:\test.txt")

You also have a constant that isn't defined in vbscript ie.
xlDelimited. If you want to use these, you need to define them.
Const xlDelimited = 1

Hope this helps.

Michael Reese
http://www.sproik.com/tools.htm


Re: Help with opentext function in vbscript by pete

pete
Fri Dec 02 07:43:01 CST 2005

Thanks Micheal, I tried just
"excelprog.Workbooks.OpenText("c:\test.txt")" and it worked. The
problem is that the file also semicolon delimited. If i would like to
use the delimited option in the in the OpenText sub do I have to
specify all the other optional values as well. For example would I have
to specify the origin, startrow, datatype, textqualifier..... I tried
that option but I recieved the error "cannot use parenthesis when
calling a sub". How can I only specify the filename and the delimited
option.

Thanks!


Re: Help with opentext function in vbscript by Groups

Groups
Fri Dec 02 22:30:48 CST 2005

I'm not exactly sure why, but I get the same error sometimes when I use
the msgbox function within an hta. To get around it, I just assign the
task to some variant.

Well, this works for me to open a semicolon delimited text file into
excel. I basically made a macro that did the same thing, then looked
at the vba code of the macro to figure this out.

***Watch for word wrap***
const xlDelimited = 1
Dim excelprog
Set excelprog = WScript.CreateObject("Excel.Application")
excelprog.Application.Visible = True

'***** The below items should all be on one line ******
strSomething =
excelprog.Workbooks.OpenText("c:\test.txt",437,1,xlDelimited,1,False,False,True,False,False,False)

Michael Reese
http://www.sproik.com/tools.htm


Re: Help with opentext function in vbscript by pete

pete
Sun Dec 04 12:51:13 CST 2005

Thnaks Mike it works fine