Consulting

Results 1 to 8 of 8

Thread: Solved: Workbook_Open entry

  1. #1
    VBAX Regular
    Joined
    Apr 2006
    Posts
    32
    Location

    Solved: Workbook_Open entry

    Is there a way for a Workbook_Open procedure to be entered into an Excel file with a VBA macro? The currently open workbook does not have a module installed. I need to set a "ThisWorkbook.Path" so the open file will always look within it's directory for another workbook. I have a common name workbook that is called when executing a macro on the currently open workbook. The Subs below works well when entered manually but I would like to automate this while performing other tasks, with a macro, on the open workbook.

    Private Sub Workbook_Open()
    Application.Caption = ThisWorkbook.Path
    End Sub

    Private Sub Workbook_Deactivate()
    Application.Caption = " "
    End Sub

    Thanks

    R1C1

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    '----------------------------------------------------------------
    Sub AddWorkbookEventProc()
    '----------------------------------------------------------------
    Dim StartLine As Long

    With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    StartLine = .CreateEventProc("Open", "Workbook") + 1
    .InsertLines StartLine, _
    "Application.Caption = ThisWorkbook.Path"
    StartLine = .CreateEventProc("Deactivate", "Workbook") + 1
    .InsertLines StartLine, _
    "Application.Caption = """""
    End With

    End Sub
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Apr 2006
    Posts
    32
    Location
    Thank you very much for the quick reply.

    I loaded the macro and ran it. I received the following error message on the "With ActiveWorkbook. ..." line:

    Run-time error '1004':

    Method 'VBProject' of object'_Workbook' failed

    Any suggestions?

    Thanks

    R1C1

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You may need to set permission for the code to change your modules.
    Try Tools/Macros/Security/Trusted Publishers Tick "Trust Access to VB Projects"
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Apr 2006
    Posts
    32
    Location
    Thanks to a Master and a Guru for helping out this ol' newbie. The Master had the right VBA and the Guru made sure I was ticked off. LOL.

    Thanks to both of you,

    R1C1
    Last edited by R1C1; 04-03-2006 at 11:51 AM. Reason: misspelled word

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Happy to help out. I'll mark this solved.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Apr 2006
    Posts
    32
    Location
    Sorry, the newbie is so new I couldn't figure out where the Solved was. I click Thread Tools and do not have that option.

    Thanks again,

    R1C1

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    No Problem. That's where it should be, but it's temporarily out of action.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •