|
-
Jan 10th, 2005, 04:32 PM
#1
Thread Starter
Addicted Member
Open File for Input erring out[RESOLVED]
Im trying to code a macro using VBA for excel...and im having a problem trying to open a txt file for input.
I'll let the following code do the talking:
Const ICDUsersFile = 1
Open "C:\Program Files\MicroStrategy\Administrator\Command Manager\ICD_Users.txt" For Input As ICDUsersFile
Dim counter As Integer
counter = 1
Dim strTemp As String
While Not EOF(ICDUsersFile)
Line Input #ICDUsersFile, strTemp
UserArray(i) = strTemp
counter = counter + 1
Close #1
Wend
Close ICDUsersFile
I have made sure that the file specified in the path exists at the location. The syntax seems to be right...I have spent so much time on this, and i am still not being able to debug this issue...any correspondence will be highly appreciated!! The code errs out right after the open file statement...
Thanks in advance!!
Last edited by perlmonk; Jan 11th, 2005 at 12:21 PM.
Reason: resolved
-
Jan 10th, 2005, 04:49 PM
#2
Re: Open File for Input erring out
Welcome to the Forums.
You need to either use the pound character '#' or use the FreeFile function
to assign the file handle number.
VB Code:
Open "C:\Program Files\MicroStrategy\Administrator\Command Manager\ICD_Users.txt" For Input As FreeFile
'Or
Open "C:\Program Files\MicroStrategy\Administrator\Command Manager\ICD_Users.txt" For Input As #1
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 10th, 2005, 05:02 PM
#3
Thread Starter
Addicted Member
Re: Open File for Input erring out
Thank you so much for your help...one more question...how would I apply #1 to the while loop im using...
I tried While not EOF(#1)
but that doesnt seem to work.
Thanks again!
-
Jan 10th, 2005, 05:04 PM
#4
Re: Open File for Input erring out
No prob.
Use this syntax.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 10th, 2005, 05:12 PM
#5
Thread Starter
Addicted Member
Re: Open File for Input erring out
Okay...I tried both the modifications you suggested....i tried #1 and FreeFile both....its still not proceeding beyond that line (the open file for input statement).
Is it some declaration issue...?
Thanks,
Kunal
-
Jan 10th, 2005, 05:17 PM
#6
Re: Open File for Input erring out
Post your modified code and what is the actual error message?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 10th, 2005, 05:21 PM
#7
Thread Starter
Addicted Member
Re: Open File for Input erring out
The code is as follows:
VB Code:
Open "C:\Program Files\MicroStrategy\Administrator\Command Manager\ICD_Users.txt" For Input As #1
Dim counter As Integer
counter = 1
Dim strTemp As String
While Not (EOF(1))
Line Input #1, strTemp
UserArray(i) = strTemp
counter = counter + 1
Wend
Close #1
The code skips onto the following errorhandler after that statement:
VB Code:
Err_createUsersScript:
Call GlobalErrorHandler(Err.Number, Err.Description, "Subroutine: createUsersScript")
Resume Exit_createUsersScript
-
Jan 10th, 2005, 05:31 PM
#8
Re: Open File for Input erring out
If its erroring out on the Open line then I would say that either the file is not
there or spelling is different or are there any permissions on that directory?
This is how I would write it.
VB Code:
Open "C:\Program Files\MicroStrategy\Administrator\Command Manager\ICD_Users.txt" For Input As #1
Dim counter As Integer
counter = 1
Dim strTemp As String
Do While EOF(1) = False
Line Input #1, strTemp
UserArray(counter) = strTemp 'Should it be counter instead of i ?
counter = counter + 1
Loop
Close #1
You should have four references at a minimum for a standard exe project:
1. Visual Basic For Applications
2. Visual Basic Runtime Objects and Procedures
3. Visual Basic Objects and Procedures
4. OLE Automation
HTH
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 10th, 2005, 05:39 PM
#9
Re: Open File for Input erring out
Just realized that this is in Excel VBA so your references should be:
1. Visual Basic For Applications
2. Microsoft Excel xx.0 Object Library
3. OLE Automation
4. Microsoft Office xx.0 Object Library
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 10th, 2005, 05:46 PM
#10
Thread Starter
Addicted Member
Re: Open File for Input erring out
The References I have are:
Visual Basic for Applications
Microsoft Excel 10.0 Object Library
OLE Automation
Microsoft Office 10.0 Object Library
Im not too sure, if those are the same references as you were suggesting...but, i tried to find the references that you mentioned in your previous post, and I could not find two of them in the available list (Visual Basic Runtime Objects and Procedures and Visual Basic Objects and Procedures).
Regarding the file name...the spelling is right..there is no permission on the directory...I'm being able to create a file in the same directory using open append, so there are no apparent issues with that. Concerning this input file (ICD_Users.txt), I have tried putting in integer data, character data, no data in this file...still, its not being able to open.
I am more of a perl hacker..and was kind of thrown into this VB project..so, I hope you can see where I am coming from. Once again, thanks for your interest and support!!
-
Jan 10th, 2005, 06:04 PM
#11
Re: Open File for Input erring out
Those are the correct references for Excel VBA. The other ones I posted were for VB.
Try changing the textfile name to something like test.txt and see if that
works. Also, place a breakpoint before your error handler function to get the
original error number and description.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 11th, 2005, 11:57 AM
#12
Thread Starter
Addicted Member
Re: Open File for Input erring out
I tried changing the name from "text.txt" to "text" in the folder and it worked from the VB code, when i called for "test.txt". Bizzare stuff...but hey, it works.
Im facing another problem now...in the same while loop, I have this array which is string type and Im trying to equate it with a variable which is also string type (strTemp):
VB Code:
Dim strTemp As String
Do While EOF(1) = False
Line Input #1, strTemp 'XXX
UserArray(counter) = strTemp
counter = counter + 1
Loop
Close #1
The error that I get when I try to equate the above, is "Subscript out of range" which is error number 9.
The input right now is purely numeric, but I believe that a string shud be able to hold a numeric value as string...nevertheless, I also tried changing both the array type and the variable (strTemp) type to integer...and then i get the error "Type mismatch" at the line in the code marked XXX
I tried to google for equating strings....dint really find anything in connection to what i was doing...any suggestions??
-
Jan 11th, 2005, 12:03 PM
#13
Re: Open File for Input erring out
Ahh! I have seen that before. What it is is that you have your folder view
setting to hide extensions. So when you had named your file ICD_Users.txt it
was actually ICD_Users.txt.txt so thats why we were getting the error that
the file could not be found. That is why I wanted the original err msg.
As for the array error 9, arrays start at element 0 unless otherwise specified
in code. So change counter to start at 0 and you need to ReDim Preserve
UserArray in your loop to increase the number of elements since you dont
know how many lines are in the file.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 11th, 2005, 12:17 PM
#14
Thread Starter
Addicted Member
Re: Open File for Input erring out [RESOLVED]
Hey, that actually did work..thanks so much.
I have some other bugs in the code, but I'll open up another thread if I need assistance...thanks again!! Your assistance was really helpful.
-
Jan 11th, 2005, 12:37 PM
#15
Re: Open File for Input erring out[RESOLVED]
No prob. Anytime.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|