VB Script Outlook

I am building an Outlook form that has several Combo Boxes that i intend to populate from >CSV files. I have written the follow VB code that work great but VBScript is not liking the Line -- Open wFname For Input As wFileNum

I have removed the extra declarations and replaced the variable with #1 . It tells me that end of line is expected.

Any general direction would be great.

Thanks


Public Sub ReadCategoryText()
Dim wFname As String
Dim wStr As String
Dim wFileNum As Integer
wFname = "c:\KACEData\Category.csv"
wFileNum = FreeFile()
Open wFname For Input As wFileNum
  Do While Not EOF(wFileNum)
        Line Input #wFileNum, wStr
    Loop
    Close wFileNum
End Sub
ChuckRushAsked:
Who is Participating?
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Since the declarations included typing I assumed it is a vba script rather than vbs!

Nice to see you again David ... I'm not so active here these days so don't cross your path very often it seems.

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Wfname is Defined as string ,.. Set it as object

Chris
0
 
David LeeCommented:
Hi, ChuckRush.

If I remember correctly VBScript doesn't support the "Open Filename For Input" syntax.  You have to use the FileSystemObject to read a file.  Something like this.
Sub ReadCategoryText()
	Const ForReading = 1
	Dim objFSO, objFile, wStr
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile("c:\KACEData\Category.csv", ForReading)
	Do While Not objFile.AtEndOfStream
		wStr = objFile.ReadLine
	Loop
	objFile.Close
	Set objFile = Nothing
	Set objFSO = Nothing
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
David LeeCommented:
Hey, Chris.

For some unfathomable reason Microsoft decided to use VBS for forms and VBA elsewhere.  It's confusing.  Since the author says the code is in a form it'd have to be VBS which, as you noted, doesn't support typing.

Good to see you too.  I've not been as active on EE either.  Hope all is well with you.
0
 
ChuckRushAuthor Commented:
Chris

Yes this is VBS. I have no issue writing this in VBA. I used your example and it does read thru the file I added the populate line (which works fine in VBA). THe runtime says it needs an object. I realize i am just not understanding the VBS model yet . I am studying Sue Mosher's book but my mistake is not clear.
THanks

Sub ReadCategoryText()
      Const ForReading = 1
      Dim objFSO, objFile, wStr
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile("c:\KACEData\Category.csv", ForReading)
      Do While Not objFile.AtEndOfStream
      '      wStr = objFile.ReadLine
            cmbCategory.AddItem wStr
      Loop
      objFile.Close
      Set objFile = Nothing
      Set objFSO = Nothing
End Sub
0
 
ChuckRushAuthor Commented:
Chris
the ' is removed from the line :       wStr = objFile.ReadLine
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
You're using DAvids code at this point...

I would wonder if cmbCategory is in scope for the function ... does it work without error if that line is commented out ... or is it readline one you commented out that fails?

Also are you sure of the filename ... if it isn't found then it will fail to instantiate objFile.

Chris
0
 
ChuckRushAuthor Commented:
Thanks Chris your example allowed me to figure it all out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.