+ Reply to Thread
Results 1 to 9 of 9

Copying VBA Modules Between Workbooks

  1. #1
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Copying VBA Modules Between Workbooks

    This is a cross posting of a post I have made at another site. I suspect I may have challenged that group as there hasn't been much along the line of support. Perhaps there is no solution. I thought I'd post here to tap into the skill of a different group hoping to get some direction.

    In my VBA project, a worksheet is created, copied to a new book and saved. This worksheet is like an internal user order form and has a few macro embedded shapes on it for use by the user using the form which is emailed to them.

    The macros are stored in the workbook that created the initial worksheet. The users receiving the email with the form in it with the macro embedded shapes do not have access to the original creation workbook, so the macros embedded on the attachment they receive are not executable.

    How can I send out that document with the macros available to the user? (I know this is can be an unsafe practice from the recipient standpoint, but no harm is intended with these macros.)

    My email distribution code is below ...

    Please Login or Register  to view this content.
    On further research, I found that what I simply (really? simply??) need to do is import the module holding the macros for the buttons from the master workbook to the target. The code in the macros is not written with any public variables or direct references to specific source worksheets etc. (all references are to activesheet or activeworkbook in the macros).

    Here is the code (thanks to ExcelTips)to copy the module (which doesn't seem to be working)

    Please Login or Register  to view this content.
    Questions ....

    1) Can anyone provide a solution as to why the exporting of the module isn't happening? There are no errors, it just doesn't happen.
    2) The line is giving me an error ("Method 'SaveAs' of object '_Workbook' failed.") . It's trying the save the workbook with the newly imported module as an .xlsm file (which I believe if you have macros has to be saved as). I'm assuming it's failing because there are no macros associated with that workbook since the import failed. If issue #1 is taken care of, will that eliminate this error? Or will I still have this error? What could I do then?

    Thank you in advance for your consideration!

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,012

    Re: Copying VBA Modules Between Workbooks

    .
    One thing that will assist in troubleshooting any macro is to comment out (or delete entirely) the line "On Error Resume Next".

    I did not review the email code for accuracy but I am presuming if you got it from Ron deBruin's site it is good. He does have an
    example of making a copy of the existing workbook and attaching it to the email for sending.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Copying VBA Modules Between Workbooks

    Perhaps an easier way is to put your code in the sheet object. That way the code will travel with the sheet copy.

    You will only then need to update the OnAction reference of the button(s)
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Copying VBA Modules Between Workbooks

    Hi! Thanks so much for stepping in to offer assistance!

    Logit ... I commented out the line you pointed out in the CopyModule code. It stopped on this line with the error "Method 'VBProject' of object '_Workbook' failed. This is a new one for me.
    Please Login or Register  to view this content.
    Andy, your approach seems simple enough so I gave it a go. I moved the simple lines of code for the shapes into that worksheet's object code. BTW, here is the code for the macro emedded buttons on the worksheet being copied to a new book.
    Please Login or Register  to view this content.
    The CopyModule code is eliminated altogether from the emailing code.

    However, I run into a problems even before this associated with saving the copied workbook initially. The process of copying the worksheet to a new book is initially created in this module. (This creation is not the one that is emailed. The one emailed is the same worksheet but has some button functionality disabled for the recipient). Refer to this code ...

    Please Login or Register  to view this content.
    This code breaks at the line in red when it tries to save. Since the new workbook has vb associuated to it (in the worksheet object) it will not allow it to save with that (.xlsx) extension.
    If I change the extension to ".xlsm" in the line in blue ... I get "This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type." as in breaks at the same line as above in red. It doesn't appear as though I can save the newly created workbook with the vba in the sheet.

  5. #5
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Copying VBA Modules Between Workbooks

    It would appear that I have managed to overcomne the file extension related hurdle after a decent Google search.

    I added the argument " FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled " which seems to have done the trick.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,012

    Re: Copying VBA Modules Between Workbooks

    Jenn68

    Great ! You have it working for you. Good feeling .. huh ?

    What was the solution to : with the error "Method 'VBProject' of object '_Workbook' failed.

  7. #7
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Copying VBA Modules Between Workbooks

    Hi again folks ...
    So I thought I had things good to go. My worksheet (with code attached to the sheet object for the two macro enabled shapes on it) was copied to a new workbook and saved successfully as a .xlsm file. It gets sent off to the recipient.
    The recipient opens the file and enables macros as prompted. However, when they click on the macro enabled button, they get the message "Cannot run the macro ''SRF_WP_07Jan19-13Jan19.xlsm'!SRF_Group6_Click'. The macro may not be available in this workbook or all macros may be disabled."

    The workbook name matches, the only worksheet in the workbook is "SRF" There are two macro enabled shapes available to the user upon receipt of the workbook.

    Please Login or Register  to view this content.
    I do not have the same issue with the "Sub Group9_Click" macro.

    Any thoughts?

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Copying VBA Modules Between Workbooks

    Check the settings for the button that is working, does it match the style of Group6 shape?

    I doubt excel will unravel the sheet name in that format.

  9. #9
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Copying VBA Modules Between Workbooks

    Hi Andy. Your suggestion has revealed that I get the same error on the original.
    I should have checked there first as it was easy then to dsicover the problem

    Thanks for the hint!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Help to combine two Modules from seperate workbooks to one workbook preform task
    By tcrjmom in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-30-2014, 06:14 PM
  2. Declaring workbooks for different modules
    By Borg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-22-2012, 09:33 PM
  3. Exporting/Importing modules between two workbooks
    By AnnieLilly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2007, 09:08 PM
  4. [SOLVED] Importing Modules From Other Workbooks
    By Jim Jackson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2006, 03:00 PM
  5. [SOLVED] Copying modules from Word to Excel
    By Pflugs in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2006, 01:35 PM
  6. [SOLVED] Copying UserForms & Modules to another workbook
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2006, 05:25 PM
  7. [SOLVED] Extracting (copying) modules from one workbook to another.
    By Devin Linnington in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-06-2005, 07:05 PM

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.6.0 RC 1