2013-10-31 66 views
2

問題只出現在沒有郵件帳戶被配置 - 我還是會喜歡一個解決方案Powershell | COM對象Excel |打開前景?

我需要一些幫助。我發現了這個小腳本的一個非常奇怪的習慣。我完全不知道爲什麼會發生這種情況。 如果我通過下面的代碼運行,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() 

Thx提前!

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 
+1

我已經經歷了同樣的行爲。我[報告它是一個bug](https://connect.microsoft.com/PowerShell/feedback/details/841516/working-with-excel-in-powershell-triggers-outlook-to-open)。 –

+0

你有沒有得到任何反饋? – XXInvidiaXX

回答

1

這裏的問題是,當你運行$wb.Worksheets.add()它返回將新工作表添加到管道中(這是爲什麼在停用Outlook時顯示圖紙屬性的原因)。

我相信工作表的MailEnvelope屬性是導致Outlook打開的原因(如果存儲表單並返回MailEnvelope屬性,則會發生相同的行爲)。

要解決這個問題,你可以存儲在返回的紙張,使用外空cmdlet或投工作表作廢:$ws = $wb.Worksheets.add()或 ​​或[void] $wb.Worksheets.add()

0
+0

thx這個想法,但我怎麼能把它轉換成PowerShell? – XXInvidiaXX

+0

無法弄清楚如何在PowerShell中做到這一點。你能給我一個在PowerShell中以「安全模式」運行Excel的代碼示例嗎? – XXInvidiaXX

+0

沒有。如果沒有幫助,我會刪除答案。 – mjolinor