5

仅在未配置邮件帐户时才会出现问题 - 我仍然希望有解决方案

我需要一些帮助。我发现这个小脚本有一个非常奇怪的习惯。我完全不知道为什么会发生这种情况。如果我运行下面发布的代码,Microsoft Outlook 就会启动。只要我不终止Outlook进程,脚本就会卡住!为什么这段代码会启动 Outlook?我搞不清楚了!

$Path     = "C:\test.xls"
#Excelvar:
$Row                 = [int] 2
$Excel               = New-Object -ComObject Excel.Application
$Excel.Visible       = $true
$Excel.DisplayAlerts = $false
        #Sheets:
        $ADUsers     = "Active Directory Users"
        $Groups      = "Create Groups"
        $UsertoGroup = "User to groups"
        $DNS         = "DNS"
#$Worksheet = $Workbook.Sheets.Add()
$checkxls = test-path -pathtype Any $Path
if ($checkxls -eq $false) {  
    $wb = $Excel.Workbooks.Add()

         $wb.Worksheets.add()

$wb.SaveAs($Path)
    $wb.Close()
    $Excel.Quit()

提前谢谢!

Outlook 终止后的 Powershell 输出:

Application                       : Microsoft.Office.Interop.Excel.ApplicationClass
Creator                           : 1480803660
Parent                            : System.__ComObject
CodeName                          : 
_CodeName                         : 
Index                             : 1
Name                              : Tabelle4
Next                              : System.__ComObject
OnDoubleClick                     : 
OnSheetActivate                   : 
OnSheetDeactivate                 : 
PageSetup                         : System.__ComObject
Previous                          : 
ProtectContents                   : False
ProtectDrawingObjects             : False
ProtectionMode                    : False
ProtectScenarios                  : False
Visible                           : -1
Shapes                            : System.__ComObject
TransitionExpEval                 : False
AutoFilterMode                    : False
EnableCalculation                 : True
Cells                             : System.__ComObject
CircularReference                 : 
Columns                           : System.__ComObject
ConsolidationFunction             : -4157
ConsolidationOptions              : {False, False, False}
ConsolidationSources              : 
DisplayAutomaticPageBreaks        : False
EnableAutoFilter                  : False
EnableSelection                   : 0
EnableOutlining                   : False
EnablePivotTable                  : False
FilterMode                        : False
Names                             : System.__ComObject
OnCalculate                       : 
OnData                            : 
OnEntry                           : 
Outline                           : System.__ComObject
Rows                              : System.__ComObject
ScrollArea                        : 
StandardHeight                    : 15
StandardWidth                     : 10,71
TransitionFormEntry               : False
Type                              : -4167
UsedRange                         : System.__ComObject
HPageBreaks                       : System.__ComObject
VPageBreaks                       : System.__ComObject
QueryTables                       : System.__ComObject
DisplayPageBreaks                 : False
Comments                          : System.__ComObject
Hyperlinks                        : System.__ComObject
_DisplayRightToLeft               : False
AutoFilter                        : 
DisplayRightToLeft                : False
Scripts                           : System.__ComObject
Tab                               : System.__ComObject
MailEnvelope                      : 
CustomProperties                  : System.__ComObject
SmartTags                         : System.__ComObject
Protection                        : System.__ComObject
ListObjects                       : System.__ComObject
EnableFormatConditionsCalculation : True
Sort                              : System.__ComObject
PrintedCommentPages               : 0
4

2 回答 2

6

The issue here is that when you run $wb.Worksheets.add() it returns the new worksheet to the pipeline (this is why the Sheet properties are displayed when you kill Outlook).

I believe the MailEnvelope property of the Worksheet is what causes Outlook to open (if you store the Sheet and return the MailEnvelope property, the same behaviour occurs).

To get around this you can store the returned sheet, use the Out-Null cmdlet or cast the worksheet to void: $ws = $wb.Worksheets.add() or $wb.Worksheets.add() | Out-Null or [void] $wb.Worksheets.add()

于 2015-04-23T15:54:33.347 回答
0

您可以尝试在“安全模式”下运行 Excel:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/79a8d280-3b80-4371-95e1-e7827472d36f/how-to-start-excel-in-safe-mode-programmatically?forum=vsto

于 2013-10-31T11:30:48.897 回答