Excel to new Outlook Contact: correct data but not reading in

huangapple go评论66阅读模式
英文:

Excel to new Outlook Contact: correct data but not reading in

问题

我已经编写了下面的脚本。其任务是逐行从(格式良好的)Excel文件中创建新的Outlook联系人。

我正在使用最新更新的Windows 11运行,事实上是今天早上更新的。

在PowerShell(以管理员身份运行)中,脚本运行时不会引发任何错误。屏幕上输出的数据表明Excel文件被正确读取。

在Outlook中,已创建新的联系人。

然而,**联系人中根本没有读取到任何数据。对于我可能遗漏了什么,我会非常感激任何解释。**也许新的联系人没有被正确保存以保留数据?

(以下为脚本代码部分,不需要翻译)

*出于更清楚需要帮助的目的进行编辑,以及修正了一个拼写错误。

英文:

I’ve written the below script. Its job is to take a (well-formatted) Excel file and, row by row, create new Contacts in Outlook.

Am running Windows 11 with everything recently updated (this morning, actually).

Within PowerShell (run as Administrator), the script runs without throwing any errors. The data outputted to the screen indicate the Excel file is being read correctly.

In Outlook, new contacts are created.

However, no data at all is being read into the contacts. Would appreciate any explanation as to what I've missed. Maybe the new contacts aren't being saved correctly to retain the data?

# Create an Excel application object
$excel = New-Object -ComObject Excel.Application

# Open the workbook
$workbook = $excel.Workbooks.Open("C:\Users\DW-ECM\Scripts\test_xl.xlsx")

# Select the first worksheet
$worksheet = $workbook.Worksheets.Item(1)

# Define the range of cells that contain the data
$range = $worksheet.UsedRange

# Creates a new instance of the Outlook application object 
# using the COM (Component Object Model) interface in PowerShell
$outlook = New-Object -ComObject Outlook.Application

Write-Host "Selected workbook: $($workbook.Name)"
Write-Host "Selected worksheet: $($worksheet.Name)"

# Loop through each row in the range
for ($i = 2; $i -le $range.Rows.Count; $i++) {
    # Get the current row
    $row = $range.Rows.Item($i)
    
    # Create a new contact object
    $contact = $outlook.CreateItem(2)

    # Set the properties of the contact object
    $contact.Account = $row.Cells.Item(1).Value2
    $contact.Business2TelephoneNumber = $row.Cells.Item(2).Value2
    $contact.BusinessAddress = $row.Cells.Item(3).Value2
    $contact.CompanyName = $row.Cells.Item(4).Value2
    $contact.Department = $row.Cells.Item(5).Value2
    $contact.Email1Address = $row.Cells.Item(6).Value2
    $contact.FirstName = $row.Cells.Item(7).Value2
    $contact.JobTitle = $row.Cells.Item(8).Value2
    $contact.LastName = $row.Cells.Item(9).Value2
    $contact.MobileTelephoneNumber = $row.Cells.Item(10).Value2
    $contact.User1 = $row.Cells.Item(11).Value2
    $contact.User2 = $row.Cells.Item(12).Value2
    
    # Save the contact to Outlook
    $contact.Save()

    Write-Host "Creating contact for $($row.Cells.Item(8).Value2) $($row.Cells.Item(10).Value2)"
}

# Close the workbook and quit Excel
$workbook.Close()
$excel.Quit()

# Save and close the Outlook instance
$outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook)
Remove-Variable outlook

# Stop the Outlook process
Stop-Process -ProcessName OUTLOOK -Force

*Edited for clarity of what I'm asking for help with and a typo.

答案1

得分: 0

您的PowerShell脚本自动化了两个Office应用程序 - Excel和Outlook,看起来很不错,我没有看到任何奇怪的地方。请注意,您不能同时运行两个Outlook进程(与Excel不同),所以如果它已经在运行中,您将得到一个正在运行的实例。这意味着如果Outlook已经由用户(没有管理员特权)打开并运行,那么您以管理员权限从命令行(或任何其他shell)运行代码将无法连接到正在运行的Outlook实例,因此无法添加联系人。

在PowerShell(以管理员身份运行)中,脚本可以在不引发任何错误的情况下运行。

请确保在相同的安全上下文下运行所有应用程序和PowerShell脚本,以便能够在Outlook中添加联系人。这意味着在以管理员身份运行脚本时,Outlook应该是关闭的(不在运行)。

英文:

Your PowerShell script, where two Office applications - Excel and Outlook are automated, looks good and I don't see anything strange there. Be aware, you can't run two instances of Outlook processes at the same time (unlike Excel), so if it was already run you will get a running instance. That means if you have Outlook already opened and running by a user (without administrative privileges) your code run form a command line (or any other shell) with admin privileges will not be able to connect to the running Outlook instance and contacts will not be added.

> Within PowerShell (run as Administrator), the script runs without throwing any errors.

Make sure that you run all applications and the PowerShell script under the same security context to be able to add contacts in Outlook. That means Outlook should be closed (not running) when you run a script as an administrator.

答案2

得分: 0

Running as an admin is a bad idea - Outlook is a singleton, and if it is already running, your script will attach to the existing instance. And COM system will refuse to marshal calls between two apps (PS and Outlook) running in different security contexts.

英文:

Running as an admin is a bad idea - Outlook is a singleton, and if it is already running, your script will attach to the existing instance. And COM system will refuse to marshal calls between two apps (PS and Outlook) running in different security contexts.

huangapple
  • 本文由 发表于 2023年3月4日 04:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定