Results 1 to 6 of 6

Thread: [RESOLVED] problem with Workbooks Open event

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Resolved [RESOLVED] problem with Workbooks Open event

    I have the following code :

    VB Code:
    1. Function Vals(File As String, Prob As Integer) As Variant
    2. Set oXLApp = New Excel.Application
    3.   oXLApp.Visible = True
    4.  
    5.  oXLApp.Workbooks.OpenText FileName:=File, _
    6.         Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    7.         xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
    8.         Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
    9.         Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
    10.         TrailingMinusNumbers:=True
    11.  
    12.  Set oXLBook = oXLApp.Workbooks(File)
    13.  Set oXLSheet = oXLBook.Worksheets(1)
    14. .....
    15. Vals = ...
    16.  
    17. End Function


    I get a subscript out of range error at the
    Set oXLBook = oXLApp.Workbooks(File)
    statement - why ?

    I've checked that File does contain the correct string.

    If I change the statement to :
    Set oXLBook = oXLApp.Workbooks.Open(File)
    everything works fine, but now I don't parse the text file correctly (It seems like it assumes default settings that are different from those that I specify in the OpenText statement).

    Thanks,

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: problem with Workbooks Open event

    try
    VB Code:
    1. set oxlbook = oXLApp.Workbooks.OpenText (FileName:=File, _
    2.         Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    3.         xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
    4.         Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
    5.         Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
    6.         TrailingMinusNumbers:=True)

    pete

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

    Re: problem with Workbooks Open event

    Set oXLBook = oXLApp.Workbooks(File)
    File is the full path and filename of the workbook but for a workbooks collection name all you need is the filename or just provide the index number.

    VB Code:
    1. Set oXLBook = oXLApp.Workbooks(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 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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: problem with Workbooks Open event

    westconn1 :
    when I try that code I get 'expected function or variable at the OpenText statement. I don't understand why.

    RobDog888 :
    the code compiles but the Excel file is still not parsed right (default settings that are different from those that I specify in the OpenText statement).

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: problem with Workbooks Open event

    I was mistaken - the code :

    Set oXLBook = oXLApp.Workbooks(1)

    works (parses) as per the OpenText statement.

    Thanks for the help !

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

    Re: problem with Workbooks Open event

    No prob.

    Dont forget to Resolve your thread so other members know its solved.
    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