Exporting module from workbook to workbook - not working (user specific)

marc81

New Member
Joined
Aug 3, 2017
Messages
17
Hi all,

I have some code that takes a module from the workbook that I am running a macro, exports it to a temporary bas file then imports it to a newly created workbook. This works absolutely fine for me, just just had another member of my team advise different. We've stepped through it, and it doesn't error, it just doesn't create the bas file. Anybody any idea why? Here's my code, sorry if it's a bit messy I'm no expert... there is a bit in there that changes some of the text in the bas file as when I was importing it to the new workbook with the same module name it kept linking it back to the original workbook (if that makes sense). Like I say though, works perfect for me. The code in red is where I think it may be failing.

Rich (BB code):
    'Copy macro from main file to extract
    Dim strModuleName As String
    Dim strFolder As String
    Dim strTempFile As String
    ThisWorkbook.Activate              '<= changed
    strFolder = ThisWorkbook.Path      '<= changed
    If Len(strFolder) = 0 Then strFolder = CurDir
    strFolder = strFolder & "\"
    strTempFile = strFolder & "~tmpexport.bas"
    On Error Resume Next
    ThisWorkbook.VBProject.VBComponents("Alloc_Date_Button").Export strTempFile '<= changed
  
            'AMEND THE EXPORTED MACRO TO NEW NAME
            Dim objFSO
            Const ForReading = 1
            Const ForWriting = 2
            Dim objTS 'define a TextStream object
            Dim strContents As String
            Dim fileSpec As String
          
            fileSpec = ThisWorkbook.Path & "\~tmpexport.bas"
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objTS = objFSO.OpenTextFile(fileSpec, ForReading)
            strContents = objTS.ReadAll
            strContents = Replace(strContents, "Alloc_Date_Button", "Alloc_Date_Button_Export")
            strContents = Replace(strContents, "ALLOC_DATE_UPDATE", "ALLOC_DATE_UPDATE_EXPORT")
          
            objTS.Close
          
            Set objTS = objFSO.OpenTextFile(fileSpec, ForWriting)
            objTS.Write strContents
            objTS.Close

  
    Output.VBProject.VBComponents.Import strTempFile '<= changed
    Kill strTempFile
    On Error GoTo 0

Thanks,

Marc
 
Last edited by a moderator:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You won't see any errors due to your On Error Resume Next statement. Your code will fail if the user's trust centre settings do not have the 'trust access to VBA project option' set.
 
Upvote 0
You won't see any errors due to your On Error Resume Next statement. Your code will fail if the user's trust centre settings do not have the 'trust access to VBA project option' set.
Of course I won't see the error sorry completely forgot about that! He's just been allocated a new laptop so that would make sense, thanks so much for your help.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top