Results 1 to 17 of 17

Thread: data from txt / excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Option explicit
    2. 'add a reference to ms excel xx.0 object library
    3. private sub command1_click()
    4.     dim oapp as excel.application
    5.     dim owb as excel.workbook
    6.     set oapp = new excel.application
    7.     set owb = oapp.workbooks.open("C:\MyFile.xls")
    8.     owb.cells(21, 3).value = "=SUM(C1:C20)"
    9.     Msgbox owb.cells(21, 3).value
    10. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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 .

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    Re: data from txt / excel

    sir also tellme how to insert a new row in between two rows

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: data from txt / excel

    VB Code:
    1. set owb = oapp.Workbooks.OpenText Filename:="C:\Test.txt", StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    2. ConsecutiveDelimiter:=False, Tab:=False, [b]Semicolon:=True[/b], Comma:=False, Space:=False,Other:=False
    3. '...
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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:
    1. set owb = oapp.Workbooks.OpenText Filename:="C:\Test.txt", StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    2. ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False,Other:=False

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. set owb = oapp.Workbooks.OpenText(Filename:="C:\Test.txt", Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    2. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    Re: data from txt / excel

    now it is giving error expected function or variable and highlights .opentext

    i am really stuck

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: data from txt / excel

    Your not just using it by itself are you?
    VB Code:
    1. Option explicit
    2. 'add a reference to ms excel xx.0 object library
    3. private sub command1_click()
    4.     dim oapp as excel.application
    5.     dim owb as excel.workbook
    6.     set oapp = new excel.application
    7.     set owb = oapp.Workbooks.OpenText(Filename:="C:\Test.txt", Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    8.     ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False,Other:=False)
    9.     'owb.cells(21, 3).value = "=SUM(C1:C20)"
    10.     'Msgbox owb.cells(21, 3).value
    11. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    Re: data from txt / excel

    only god knows why this is still not working

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    146

    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

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width