Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add VBA code to Excel worksheet using PowerShell?

I need to include a Private Sub Worksheet_BeforeDoubleClick (ByVal Target As Range, Cancel As Boolean) in my Sheet(1).

I'm able to open and write in cells.

$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.WorkSheets.item(1)
$worksheet.range("c1","g6").value = "str"
...
$workbook.SaveAs($xlFlie, 50)
$Excel.Application.Quit()

How do I put the VBA code in the sheet (rather than in a VBA module)?

I tried this:

$xlmodule = $workbook.VBProject.VBComponents.Add()
$xlmodule.CodeModule.AddFromString($code)

I got this error:

Can not call a method in an expression Null.
Au caractère .\Build-ADGrpsMembers2Excel.ps1:273 : 5
+ $xlmodule = $workbook.VBProject.VBComponents.Add(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

like image 362
Alban Avatar asked Jan 22 '14 09:01

Alban


People also ask

How do I import VBA code into Excel?

Insert VBA code to Excel WorkbookOpen your workbook in Excel. Press Alt + F11 to open Visual Basic Editor (VBE). Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu. Copy the VBA code (from a web-page etc.)

Can you use PowerShell with VBA?

This is easy to do in VBA. Checkout the below example, using a PowerShell command from VBA to get the user's free disk space. That's it! It's actually very easy to interface with Powershell from VBA.

Can PowerShell write to Excel?

In this article we'll show how to read and write data from Excel worksheets directly from PowerShell scripts. You can use Excel along with PowerShell to inventory and generate various reports on computers, servers, infrastructure, Active Directory, etc.


1 Answers

I need to change VBA option to

$excel = New-Object -ComObject Excel.Application
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$($excel.Version)\excel\Security" -Name AccessVBOM -Value 1 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$($excel.Version)\excel\Security" -Name VBAWarnings -Value 1 -Force | Out-Null

enter image description here

my working code is :

$excel = New-Object -ComObject Excel.Application
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$($excel.Version)\excel\Security" -Name AccessVBOM -Value 1 -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$($excel.Version)\excel\Security" -Name VBAWarnings -Value 1 -Force | Out-Null

$workbook = $excel.Workbooks.Add(1)
$worksheet=$workbook.WorkSheets.item(1)

$excel.Visible=$true
$excel.DisplayAlerts = $true
$excel.ScreenUpdating = $true

#$worksheet.range("c1","f6").ColumnWidth = 4
#$worksheet.range("c1","f6").Orientation = 90

$xlmodule = $workbook.VBProject.VBComponents.item('feuil1')
$code = @"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
End Sub
"@

$xlmodule.CodeModule.AddFromString($code)

$saveName = "$([Environment]::GetFolderPath('desktop'))\Export-Excel ($( ((Get-Date -Format u ) -replace ":", ".") -replace "Z", '' ) ).xlsb"

# savegarde du fichier
$workbook.SaveAs($saveName, 50)

Write-Verbose "Closing $($WorkSheetName)"
$Excel.Workbooks.Close()
Write-Verbose "Exit Excel"
$Excel.Application.Quit
like image 85
Alban Avatar answered Oct 15 '22 23:10

Alban