【问题标题】:call excel macro with powershell用powershell调用excel宏
【发布时间】:2019-03-08 13:47:48
【问题描述】:

我正在尝试打开一个 Excel 工作表,运行宏并定期关闭它,以便在单元格中的日期更改为“xxx”时收到电子邮件通知。

不幸的是,powershell 在运行下面的宏时显示错误:


Sub tata(ByVal Target As Range)
  On Error Resume Next
    If Target.Cells.Count < 1 Then Exit Sub
  Set xRg = Intersect (Range("AT28:AT673"), Target
   If xRg Is Nothing Then Exit Sub
   If IsNumeric(Target.Value) And Target.Value = 1 Then
     Call Mail_small_Text_outlook
  End If
End Sub
Sub Mail_small_Text_Outlook()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody as String
    Set xOutApp = Create.Object("Outlook.Application")
    Set xOutMail = xOut.App.CreateItem(0)
    xMailBody = "Hi" & vbNewLine & _
                ""
    On error Resume Next
    With xOutMail
       .To = "Name@domainname.com"
       .CC = ""
       .BCC = ""
       .Subject = "order"
       .Body = "xMailbody
       .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Ausnahme beim Aufrufen von "Run" mit 1 Argument(en): "Parameter nicht optional. (Ausnahme von HRESULT: 0x8002000F (DISP_E_PARAMNOTOPTIONAL))" 在 Zeile:14 Zeichen:2 + $App.Run("tata") + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : COMException


 $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbookMacroEnabled
$app = New-Object -comobject Excel.Application
 $app.Visible = $True
 $app.DisplayAlerts = $False
 $wb = $App.Workbooks.Open("c:\my.xlsm")
 $App.Run("tata")
 $app.ActiveWorkbook.Saves
 $app.Quit()

【问题讨论】:

    标签: excel vba powershell


    【解决方案1】:

    调用宏时需要传入一个参数(Target):

    $App.Run("tata", "value for target")
    

    【讨论】:

    • 嗨,我不明白 :-)
    • 运行宏时需要在宏名后指定一个值,在第一部分代码中使用。你也可以通过调用“Mail_small_Text_Outlook”而不是“tata”来避免第一个块,那么你不需要传递任何参数$App.Run("Mail_small_Text_Outlook")
    猜你喜欢
    • 2013-11-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多