|
-
Aug 21st, 2006, 12:05 AM
#1
Thread Starter
Addicted Member
data from txt / excel
hello ,
can some one plz tell me how can i use a data from a text pad or a excel file actually i have to do some editing in those ( for example sum up all the amounts written etc) and then take a print of that . i have 2 option either data from a notepad( but that is un formatted) or i can manually open it with excel save it there then use it ,it comes in some format so looks better .
some one plz tell me how to start.
-
Aug 21st, 2006, 12:23 AM
#2
Re: data from txt / excel
If your textfile is a comma separated values listing then just open it with Excel programmatically. Apply a formula to SUM up the needed column and thats it.
VB Code:
Option explicit
'add a reference to ms excel xx.0 object library
private sub command1_click()
dim oapp as excel.application
dim owb as excel.workbook
set oapp = new excel.application
set owb = oapp.workbooks.open("C:\MyFile.xls")
owb.cells(21, 3).value = "=SUM(C1:C20)"
Msgbox owb.cells(21, 3).value
end sub
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 
-
Aug 21st, 2006, 12:48 AM
#3
Thread Starter
Addicted Member
Re: data from txt / excel
sir, if i open the txt file manually in excel it asks me for what type of seprations are used (i give ; ) and the file is opened perfectly how can i do so programatically as i open the file programatically it opens but semicoluns in between are also shown also sir i want some of the columns not to be shown , how can i do that .
-
Aug 21st, 2006, 01:11 AM
#4
Thread Starter
Addicted Member
Re: data from txt / excel
sir also tellme how to insert a new row in between two rows
-
Aug 21st, 2006, 01:22 AM
#5
Re: data from txt / excel
VB Code:
set owb = oapp.Workbooks.OpenText Filename:="C:\Test.txt", StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, [b]Semicolon:=True[/b], Comma:=False, Space:=False,Other:=False
'...
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 
-
Aug 21st, 2006, 01:37 AM
#6
Thread Starter
Addicted Member
Re: data from txt / excel
sir the following code gives syntax error and at run time says end of statement required ( at filename).
sir cud u plz tell me how to insert a new row in sheet
VB Code:
set owb = oapp.Workbooks.OpenText Filename:="C:\Test.txt", StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False,Other:=False
-
Aug 21st, 2006, 02:04 AM
#7
Re: data from txt / excel
Note the space and underscore chars at the end as its a line continuation 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 
-
Aug 21st, 2006, 02:13 AM
#8
Thread Starter
Addicted Member
Re: data from txt / excel
sir that i know it is ok but there is a error saying expected end of statement and filename is highlighted
-
Aug 21st, 2006, 02:20 AM
#9
Re: data from txt / excel
Forgot to add the parenthesis and charset. 437 is United States. If your not in the usa then change it to your locale. 
VB Code:
set owb = oapp.Workbooks.OpenText(Filename:="C:\Test.txt", Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False,Other:=False)
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 
-
Aug 21st, 2006, 03:26 AM
#10
Thread Starter
Addicted Member
Re: data from txt / excel
now it is giving error expected function or variable and highlights .opentext
i am really stuck
-
Aug 21st, 2006, 04:00 AM
#11
Re: data from txt / excel
Your not just using it by itself are you?
VB Code:
Option explicit
'add a reference to ms excel xx.0 object library
private sub command1_click()
dim oapp as excel.application
dim owb as excel.workbook
set oapp = new excel.application
set owb = oapp.Workbooks.OpenText(Filename:="C:\Test.txt", Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False,Other:=False)
'owb.cells(21, 3).value = "=SUM(C1:C20)"
'Msgbox owb.cells(21, 3).value
end sub
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 
-
Aug 21st, 2006, 06:09 AM
#12
Thread Starter
Addicted Member
Re: data from txt / excel
only god knows why this is still not working
-
Aug 21st, 2006, 10:29 AM
#13
Re: data from txt / excel
Did you add a reference to Excel? Did you change the textfile location in the .OpenText function?
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 
-
Aug 21st, 2006, 11:11 PM
#14
Thread Starter
Addicted Member
Re: data from txt / excel
yes sir i had , but what's the significance of doing that as it is giving error function or variable required. and highlightes .opentext in the line workbook.opentext
-
Aug 21st, 2006, 11:25 PM
#15
Re: data from txt / excel
If your going to automate Excel then you need to use either Early or Late Binding. Early binbding is when you add the reference so VB knows what the objects are. When you use Late binding you dont adda reference and declare all your excel objects as generic Objects.
What version of Excel are you running as the .OpenText may have different arguments for your version.
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 
-
Aug 21st, 2006, 11:42 PM
#16
Thread Starter
Addicted Member
Re: data from txt / excel
sir , i am using ms excel 2002 and i hav added refrence to library ms exel 10 in my vb as it was the only version available
-
Aug 22nd, 2006, 12:05 AM
#17
Re: data from txt / excel
The function call looks like its the same definition for 2002 and 2003.
http://msdn.microsoft.com/library/de...thOpenText.asp
Since it doesnt look like there is anything wrong, try recording a macro of you importing the text file then stop and view the generated macro code and compare the .OpenText call.
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
|