Gary regularly receives an XLS file that is generated by someone outside his company. He then needs to import some macros into the workbook (or, bother!, copy them from a TXT file) and then save the workbook out in the XLSM format. Gary wonders if there is a way to automate this, such that he can run a macro that will import the macros he would normally add manually and then save the workbook in the proper format.
There is a way to automate it by using the VBProject object. If your external text file contains, essentially, Visual Basic modules, you can use the Import method to actually import those modules. It behaves exactly the same as if you were to use the Import capabilities of the Visual Basic Editor. Here's an example of some code you could use:
Sub AutomateImport() Const ModulePath As String = "C:\temp\code.txt" Dim thisTarget As Workbook Dim thisName As String Set thisTarget = ActiveWorkbook thisName = thisTarget.Name ' Save as XLSM file (neceassry before importing module) ActiveWorkbook.SaveAs thisName & ".xlsm", _ FileFormat:=xlOpenXMLWorkbookMacroEnabled ' Import the VBA code required thisTarget.VBProject.VBComponents.Import ModulePath ' Save the workbook ActiveWorkbook.Save End Sub
Note that the code saves the active workbook as an XLSM file before actually doing the import. This is necessary because the Import function may balk if you try to import a VBA module into a workbook that doesn't support macros. In addition, if you get an error when running the macro, you may want to check in the Visual Basic Editor that you've established a reference to the VBA Extensibility object library.
In order to use the macro, make sure you update the ModulePath constant so that it points right to the desired text file. The macro should also be stored in your Personal workbook so that it will be available whenever you are using Excel.
You might also profit by examining some code on Ron DeBruin's site which shows how to both import and export VBA code within a macro:
https://www.rondebruin.nl/win/s9/win002.htm
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11855) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
If you need to know whether a particular value is odd or even, you can use this simple formula. Designed to be used in a ...
Discover MoreA common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...
Discover MoreNormally a macro is only calculated when you specifically tell Excel to calculate it. Some macros need to be calculated ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-01-04 15:50:57
Olivier
I can't find how to do that for Microsoft Word... :(
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments