Specify ThisWorkbook in the VBComponent object.
$workbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString($Autofit)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using PowerShell to create open a .xlsx, then save it as a macro enabled workbook (.xlsm) and it adds a vb script to vba projects > modules with
$AutofitModule = $workbook.VBProject.VBComponents.Add(1)
$Autofit = @"
Sub workbook_open()
Worksheets("Sheet1").UsedRange.EntireColumn.autofit
End Sub
"@
$AutoFitModule.codemodule.AddFromString($Autofit)
Instead of saving this to vba projects > modules as the first image shows, I would like to save it to vbaprojects > Microsoft Excel Opbjects > Thisworkbook as the 2nd image shows.
Any help is greatly appreciated
Specify ThisWorkbook in the VBComponent object.
$workbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString($Autofit)