Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    I feel certain I've seen code in this forum to do this, but I can't find it using a search. I need to create code to copy a module from Workbook_A.xls to Workbook_B.xls. This assumes that the code to copy the module is contained in Workbook_A.xls.

    While Excel is my most immediate need, I would also like to do the same thing in Word. I'm hopeful that the code would be similar, if not identical.

    Thanks!!
    Troy

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Troy,

    The following code should copy a module from the active workbook to another open (in the example called "WBToUpdate.xls") workbook. You could of course code it to open any workbook. It first exports a module to fName (you might want to add a full path) and then activates the second workbook and imports it.<pre> Sub UpDateModule()
    Dim vbComp As Object
    Dim fName As String
    fName = "AddModule.bas"
    Application.VBE.ActiveVBProject.VBComponents("Modu leX").Export fName
    Workbooks("WBToUpdate.xls").Activate
    ActiveWorkbook.VBProject.VBComponents.Import fName
    End Sub</pre>

    There should be no need to set a reference to the VBA Extensibilty library, and with little modification should work with Word, and indeed other VB components like forms etc.

    Andrew C

  4. #3
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Dallas plus 20 miles or so, Texas, USA
    Posts
    876
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Andrew,

    Any idea as to the nature of events if the module you were copying to already existed? I was thinking that this could be a good way to update a module that had already been distributed to a number of users.

  5. #4
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Thanks for the help!! That should work.

    HOWEVER, I forgot to mention when I made my post that I actually needed to copy the module into the personal.xls file (equlivalent of normal.dot for Word). Obviously this will be a different paths on different machines. Thus, I'm assuming I need to set a variable for the path for this file first, then import it into that file.

    QUESTION: How do I programmatically find the path to this file? In Word I was able to find the startup folder using

    Options.DefaultFilePath(Path:-wdStartupPath

    But "Options" does not seem to be an option in Excel.

    Thanks!!
    Troy

  6. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 27 Times in 26 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Troy,

    Try:

    Application.StartupPath

    Gary

  7. #6
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    The code to find the Personal.xls works great.

    HOWEVER, I'm getting errors when I try to run the code as follows:

    Public GlobTempPath As String


    Dim vbComp As Object
    Dim fName As String

    GlobTempPath = Application.StartupPath

    fName = "Test1.bas"

    For BOTH of the next two lines I get a "Subscript out of range" error:
    Application.VBE.ActiveVBProject.VBComponents("Test 1.bas").Export fName
    Workbooks(GlobTempPath & "Personal.xls").Activate
    ActiveWorkbook.VBProject.VBComponents.Import fName

    Thanks again for the help!!
    Troy

  8. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Troy,

    you have to add a

    Workbooks(GlobTempPath & "" & "Personal.xls").Activate

  9. #8
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Good point!! Thanks!!

    HOWEVER, I still get the same "Subscript out of range" errors.

    Troy

  10. #9
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    I think you have to open your Presonal.xls before.
    It is not open by default like in Excel 2000.

    Application.VBE.ActiveVBProject.VBComponents("Test 1.bas").Export fName
    Workbooks.Open GlobTempPath & "" & "Personal.xls"
    Workbooks("Personal.xls").Activate
    ActiveWorkbook.VBProject.VBComponents.Import fName

  11. #10
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    When I stepped through this, it said the Personal.xls was already open. Either way that would not explain the same error on the second line under my note. I've included again below:

    Public GlobTempPath As String


    Dim vbComp As Object
    Dim fName As String

    GlobTempPath = Application.StartupPath
    fName = "Test1.bas"

    NOTE: For BOTH of the next two lines I get a "Subscript out of range" error:
    Application.VBE.ActiveVBProject.VBComponents("Test 1.bas").Export fName
    Workbooks(GlobTempPath & "Personal.xls").Activate
    ActiveWorkbook.VBProject.VBComponents.Import fName

    Thanks!!
    Troy

  12. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Troy,
    I have doing some testing.
    According Test1 is a module, you don't have to add .bas (This give the Subscript out of range error)
    Secondly I think you can't make the personal.xls active

    This is working on my Excel97

    Application.VBE.ActiveVBProject.VBComponents("Test 1").Export fName
    Workbooks("Personal.xls").VBProject.VBComponents.I mport fName

    The Module must not exist in the personal.xls otherwise it will be saved as "Test11"

  13. #12
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    477
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Thanks!! I'm up and running now. I think part of my problem was a real bonehead move. I was developing this code inside my personal.xls file instead of a separate file.

    Working now thanks!!
    Troy

  14. #13
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Michael,

    If the Module (Name) already existed the newly imported module would have a 1 appended to the name to become ModuleX1 in the eexample. To deal with that situation you would need to remove the original module before importing the replacement. <pre> Sub UpDateModule()
    Dim fName As String
    fName = "NewModuleX.bas"
    Application.VBE.ActiveVBProject.VBComponents("Modu leX").Export fName
    Workbooks("WBToUpdate.xls").Activate
    With ActiveWorkbook.VBProject.VBComponents
    .Remove ActiveWorkbook.VBProject.VBComponents("ModuleX")
    .Import fName
    End With
    End Sub</pre>

    The above code could do with some error trapping, just in case.

    Andrew

  15. #14
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Dallas plus 20 miles or so, Texas, USA
    Posts
    876
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    Thanks Andrew,

    I tried adding both to a project with no module (it added a module) and to a project with a module named the same name and received the result you noted. Thanks!

  16. #15
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Dallas plus 20 miles or so, Texas, USA
    Posts
    876
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying modules from one workbook/doc to another (VBA Excel/Word 97)

    One other question Andrew,

    This might be somewhat impossible, but is it possible to write lines of code into one module from code in the other module?

    Something like:

    [pre]
    Sub UpDateModule()
    With Application.VBE.activeVBProject.VBComponents.<font color=red>Sub("Test").Line(2)</font color=red> <font color=448800>'or something</font color=448800>
    With Line(2)
    .Text="Workbooks(1).Sheet(1).Range("A1").Value=1"
    End With
    End With
    End Sub

Page 1 of 2 12 LastLast

Posting Permissions

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