如何在powershell中的excel.Worksheet中添加VBA代码?

时间:2014-01-22 09:21:02

标签: excel vba powershell excel-vba

我需要在我的工作表(1)中加入Private Sub **Worksheet_BeforeDoubleClick** (ByVal Target As Range, Cancel As Boolean)

我可以正确地打开和写入单元格,但我不知道如何将VBA代码放在工作表中(而不是在VBA模块中)。

$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()

我试过这个:

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

但我收到了这个错误:

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

2 个答案:

答案 0 :(得分:6)

我需要将VBA选项更改为

$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

我的工作代码是:

$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

答案 1 :(得分:0)

这个答案对我有很大帮助,但是我不得不添加另一个模块,所以我做了类似的事情

$workbook.VBProject.VBComponents.Add(1) 
$workbook.VBProject.VBComponents.Add(2) 
$workbook.VBProject.VBComponents.Add(3) 

1用于模块,2用于用户表单,3用于类

要获取刚添加的模块,它将位于组件数组的最后一位

$arrayOfComponents = @($workbook.VBProject.VBComponents)
$justAddedModule = $arrayOfComponents[$arrayOfComponents.Length-1]

所以看起来像这样

$excel = New-Object -ComObject Excel.Application
$vbaProject = new-object -ComObject VB
$FilePath = "c:\temp\excel.xlsm"
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($FilePath)
# Add(1) za dodavanje modula
#$workbook.VBProject.VBComponents.Add(1)

$xlmodule = $workbook.VBProject.VBComponents.item('Module1')


$code = @"
Sub test()
'
End Sub
"@
$xlmodule.CodeModule.AddFromString($code)
$workbook.SaveAs($FilePath,52)
$excel.Workbooks.Close() 
$excel.Application.Quit()