给定:使用 Powershell 将带有模板文件的文件夹复制到不同的目录 重命名 .msg 文件并编辑 .msg 文件以获取重复出现的常用值并保存。
问题:Outlook 打开文件并将其保存在 Outlook 草稿文件夹中,而不是编辑并将其保存到给定的复制目录中。
在谷歌搜索中,我找到了 Word 文档编辑程序,但没有找到 Outlook。在 word 文档编程中,我们可以搜索特定的单词并在文档中替换它。
我不确定 Outlook 是否可以实现类似的效果。
以下是我成功实现的
以下是我未能实现并需要帮助的内容。
以下是我期望达到的目标
请求帮助。
感谢大家提前给予他们的想法和努力。
代码如下
$LBStempfolder = Template folder location
$newfolder = new directory location
If(test-path $LBStempfolder)
{ if(!(test-path $newfolder))
{ $newfolder = New-Item -ItemType "Directory" -Path "LBStempfolder" -Name "XYZ" -Confirm
Write-host $newfolder
if(test-path $newfolder)
{ Copy-Item -Path $LBStempfolder -Destination $newfolder
Get-ChildItem -Path $newfolder\* -Include xyz\* -Recurse |
Rename-Item -NewName {$_.Name -replace 'xyz', 'abc'} -Force
}}}
Function Get-Outlookfile ($ofile)
{ $outlook = New-Object -ComObject outlook. Application
$namespace = $outlook.GetNameSpace("MAPI")
$email = $outlook.CreateItemFromTemplate($ofile)
$email.HTMLBody = $email.HTMLBody.Replace("XXX-XXX", "myedition")
$email.Subject = $email.Subject("mysubject")
$email.Save()
}
$outlook doc = Get-Outlookfile -ofile $newfolder\\abc.msg
Save
类的 MailItem
方法将 Microsoft Outlook 项目保存到 Outlook 中的当前文件夹(不是磁盘),或者,如果这是一个新项目,则保存到项目类型的 Outlook 默认文件夹,在您的情况下将是草稿文件夹,因为它从未保存在 Outlook 中。
如果您需要将模板创建和编辑的项目保存到磁盘,您需要使用MailItem.SaveAs方法将Microsoft Outlook项目以指定文件的格式保存到磁盘上的指定路径类型。如果未指定文件类型,则使用 MSG 格式 (.msg)。
因此,代码可能如下所示:
$LBStempfolder = Template folder location
$newfolder = new directory location
If(test-path $LBStempfolder)
{ if(!(test-path $newfolder))
{ $newfolder = New-Item -ItemType "Directory" -Path "LBStempfolder" -Name "XYZ" -Confirm
Write-host $newfolder
if(test-path $newfolder)
{ Copy-Item -Path $LBStempfolder -Destination $newfolder
Get-ChildItem -Path $newfolder\* -Include xyz\* -Recurse |
Rename-Item -NewName {$_.Name -replace 'xyz', 'abc'} -Force
}}}
Function Get-Outlookfile ($ofile)
{ $outlook = New-Object -ComObject outlook. Application
$namespace = $outlook.GetNameSpace("MAPI")
$email = $outlook.CreateItemFromTemplate($ofile)
$email.HTMLBody = $email.HTMLBody.Replace("XXX-XXX", "myedition")
$email.Subject = $email.Subject("mysubject")
$email.SaveAs($ofile)
}
$outlook doc = Get-Outlookfile -ofile $newfolder\\abc.msg
在某些情况下,您可能对使用 NameSpace.OpenSharedItem 方法感兴趣,该方法可以从指定路径或 URL 打开共享项目。有关详细信息,请参阅使用 OpenSharedItem 导入保存的项目。
在这两种情况下,您都需要使用
SaveAs
方法将您的更改保存在磁盘上,因此原始项目将被覆盖。
但这两种方法的区别在于,
CreateItemFromTemplate
方法创建了一个组合状态的新项目,将其保存为接收项目需要为此做额外的步骤,而OpenSharedItem
方法没有改变项目状态。如果这是有道理的。否则,两种方式都是完全有效的。
而不是使用
Application.CreateItemFromTemplate
(正如它的名字所暗示的那样),使用Namespace.OpenSharedItem
。
保存项目时,您可能还想使用
MailItem.SaveAs(.., olMsgUnicode)
而不是 MailItem.Save
.