I am trying to export a VBA module from one workbook and import it into another:
Dim ProofName As Variant
Dim sDate As String
Dim CashShN As Variant
Dim wbkSource As Workbook
Dim wbkTarget As Workbook
Dim CSVModName As String
sDate = Format(ActiveWorkbook.Sheets(1).Cells(4, 2).Value, "mmddyy")
ProofName = ThisWorkbook.Name
CashShN = ActiveWorkbook.Name
Set wbkSource = Workbooks(ProofName)
Set wbkTarget = Workbooks(CashShN)
CSVModName = ThisWorkbook.Path & "\CSV Module " & sDate & ".bas"
wbkSource.VBProject.VBComponents("Module2").Export (CSVModName)
wbkTarget.VBProject.VBComponents.Import (CSVModName)
This worked when ThisWorkbook and wbkSource were on a local drive. They are now at SharePoint locations and I am getting the following error:
Run-time error 50012: Method "Export" of object '_VBComponent' failed
This occurs on this line:
wbkSource.VBProject.VBComponents("Module2").Export (CSVModName)
How do I export the VBA module from the source to the target?
ThisWorkbook.Pathwith a temp folder location. See stackoverflow.com/a/424332/478884 for finding the temp folder path. FYI you can useSet wbkSource = ThisWorkbookandSet wbkSource = ActiveWorkbook- there's no need to make that round-trip using the workbook names.