+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    02-25-2010
    Location
    Osielsko
    MS-Off Ver
    Excel 2003
    Posts
    21

    Inserting module to ActiveWorkbook

    Hello,

    How can I insert code in ThisWorkbook module in Workbook_Open event? I would like something like code in code so I can write in my macro line by line what should be putted in Workbook_Open in ActiveWorkbook.

  2. #2
    Forum Moderator dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003 & 2007
    Posts
    3,661

    Smile Re: Inserting module to ActiveWorkbook

    Good evening owiec

    Try using this code :

    Code:
    Sub test()
    Set wbcodemod = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule
    Set codemod = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule
    codemod.insertLines 1, "Public Sub Workbook_Open()"
    codemod.insertLines 2, "'** Put your lines of code in here **"
    codemod.insertLines 3, "End Sub"
    End Sub
    Before you run it set a reference within the VBE to Microsoft Forms 2.0.
    Go to Tools > References, find and tck "Microsoft Forms 2.0 Object Library" and click OK.

    HTH

    DominicB
    Now available : Ultimate Add-In 2007
    Integrates directly into the Office Excel Ribbon


    Download Ultimate Add-In v1.52 from www.dom-and-lis.co.uk
    90+ Utilities, 200+ Sub utilities last updated 25th April 2008
    Free!!

  3. #3
    Registered User
    Join Date
    02-25-2010
    Location
    Osielsko
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Inserting module to ActiveWorkbook

    Hi,

    Thanks for reply.

    I get error Subscript out of range and it points to
    Set wbcodemod = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule
    When I put something like MsgBox ActiveWorkbook.CodeName its returne blank window. Should it be like that?

    I know that ActiveWorkbook is working cuz when I put MsgBox ActiveWorkbook.Name it returns aproperiate value.
    Last edited by owiec; 03-12-2010 at 03:53 AM.

  4. #4
    Forum Moderator dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003 & 2007
    Posts
    3,661

    Smile Re: Inserting module to ActiveWorkbook

    Hi owiec

    Your profile states you are running XL2003. Is this still the case?

    I tested the code last night on an XL2003 and it worked fine. I've tested it again this morning on a different machine and it still works fine.

    Try running the macro in the attached file and let me know how it goes.

    HTH

    DominicB
    Attached Files Attached Files
    Now available : Ultimate Add-In 2007
    Integrates directly into the Office Excel Ribbon


    Download Ultimate Add-In v1.52 from www.dom-and-lis.co.uk
    90+ Utilities, 200+ Sub utilities last updated 25th April 2008
    Free!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0