PowerShell: Remove VBA module from excel file PowerShell: Remove VBA module from excel file powershell powershell

PowerShell: Remove VBA module from excel file


had to do something similar recently, this how i did it.

$Code = @'your code hereMake sure this guy is public@'$Excel = new-object -com Excel.Application#Need to change security settingsNew-ItemProperty -Path `"HKCU:\Software\Microsoft\Office\$($Excel.Version)\excel\Security" -Name ` AccessVBOM -Value 1 -Force | Out-NullNew-ItemProperty -Path `"HKCU:\Software\Microsoft\Office\$($Excel.Version)\excel\Security" -Name `VBAWarnings -Value 1 -Force | Out-Null$Workbook = $Excel.Workbooks.open("Insert Path to file here",$true)$xlModule = $Workbook.VBProject.VBComponents.Add(1)$Module = $xlmodule.CodeModule.AddFromString($Code)$Excel.Run("Name of Module here, make sure that the sub is public")$Workbook.VBProject.VBComponents.Remove($xlmodule)$Workbook.Close($True)


is there a reason why you can't simply reference the appropriate module?excel does not require modules to be attached. modules in add-ins & personal workbooks can run on any sheet.

if you create an add-in (save as _ add-in, then under developer tools click the box to turn it on) you csn update it on the fly with whatever module you want.

if you dont have access to the developer tools tab, you can add the module to your personal workbook (this is a hidden workbook that can be auto generated when you first use the record macro button)

by doing any of the above you can open an excel file, run code on it, and then close the file without the hassle af attempting to programmatically add a module to a fike that does not support modules.

if you absolutely have to attach and remove the module, try this: open the file, convert to xlsb format, close file, rename file as .zip, add module and update xml path inside .zip, rename as .xlsb, open in excel, run module, save as file that does not support modules.