Results 1 to 9 of 9

Thread: Excel: workbooks.open doesn't work (not a newbie problem)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    3

    Resolved Excel: workbooks.open doesn't work (not a newbie problem)

    This one is small enough to try out on your own machine.


    Book1 has this event procedure in the ThisWorkbook object:
    VB Code:
    1. Private Sub Workbook_BeforePrint(Cancel As Boolean)
    2.     Workbooks.Open "SomeWorkbook"
    3.     MsgBox "foo"
    4. End Sub
    Book2 has this sub in a code module:
    VB Code:
    1. Sub test()
    2.     Dim wb As Workbook
    3.     Set wb = Workbooks.Open("Book1.xls")
    4.     wb.PrintOut
    5. End Sub
    When I run test() in Book2, Book1 opens, msgboxes the 'foo' and prints, but the Workbooks.Open "SomeWorkbook" line doesn't do anything. Is there any way to make it work? Maybe the Workbooks.Open calls can't be nested, but I can't find any indication of this in documentation. Thanks for you help.

    Will
    Last edited by tranner; Jun 9th, 2005 at 01:17 PM.

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    Welcome to the Forums.

    So your saying that the Workbooks.Open "SomeWorkbook" is not executing when opened and printed from book2.
    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
    New Member
    Join Date
    Jun 2005
    Posts
    3

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    Precisely. Now, if I open Book1 maually and print, the Workbooks.Open "SomeWorkbook" does work, and will throw an error is SomeWorkbook doesn't exist.

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    As a test, make sure ALL instances of Excel are closed and no instances are running in the taskmanager running processes list. Execute
    your code and you should have showing 3 instances of Excel.exe.
    It may be that its being shown as invisible if the last time it was saved it was hidden or invisible.
    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

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    as far as i can understand you have to do mare than just write a sub called beforeprint

    Application events occur when a workbook is created or opened or when any sheet in any open workbook changes. To write event procedures for the Application object, you must create a new object using the WithEvents keyword in a class module. For more information, see Using Events with the Application Object.
    Before you can use events with the Application object, you must create a new class module and declare an object of type Application with events. For example, assume that a new class module is created and called EventClassModule. The new class module contains the following code.

    Public WithEvents App As ApplicationAfter the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.)

    Before the procedures will run, however, you must connect the declared object in the class module with the Application object. You can do this with the following code from any module.

    Dim X As New EventClassModule

    Sub InitializeApp()
    Set X.App = Application
    End SubAfter you run the InitializeApp procedure, the App object in the class module points to the Microsoft Excel Application object, and the event procedures in the class module will run when the events occur.
    information above from vba helpfile

    pete

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    He is using the default events behind each workbook so no WithEvents are required. Also, the _BeforePrint event is
    executing, just not the Workbooks.Open(Blah).
    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

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    Looks like there is a limitation of one workbook opening another from another via automation.
    I was searching earlier on MS and I couldnt find anything that states this. I would suggest that instead of daisy-chaining the opening of
    workbooks, you have a central workbook to open all the wbs you need.
    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
    New Member
    Join Date
    Jun 2005
    Posts
    3

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    I guess it's to prevent a recursive loop of opening workbooks. Weird how this isn't documented though. Oh well.

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

    Re: Excel: workbooks.open doesn't work (not a newbie problem)

    Thats what I was thinking and also that it may come from a security standpoint. Dont want to open a workbook and have it
    open "other" malicious workbooks, etc. like some virus' and spam.
    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