我有一个在 Outlook 中使用的 vbscript 宏。它将邮件项移动到某个文件夹,比如 X。在我运行宏并尝试使用 Control-v 从 Outlook 手动移动邮件项后,它默认为文件夹 X。我希望 Control-v 默认为它所在的文件夹在我运行宏之前会使用。
VBScript 中是否有某种方法可以找出最后一个邮件项移动到哪个文件夹,并在我运行脚本后将其返回为默认文件夹?或者有没有办法在我运行脚本后移动我的脚本中的邮件项而不用 Outlook Control-v 记住目标文件夹?
感谢您的任何提示。
好的,这是我正在使用的代码。这是一个将邮件项保存为 HTML 并在浏览器中打开的宏。我将所有附件保存在一个单独的目录中,并将 URL 列表添加到附件中。我通过修改邮件项来做到这一点,但我不想更改原始邮件 - 我希望它保持原样保留在我的收件箱中。所以我创建了一个副本,当我完成后,我想摆脱这个副本。出于某种原因,.Delete 方法什么也不做。因此,对我来说,一种解决方案是找出 .Delete 不起作用的原因。我通过将复制的消息移动到我的已删除项目文件夹中创建了一个解决方法。我遇到的问题是我经常使用 control-v 将项目从我的收件箱移动到存档文件夹。但是,一旦我运行宏,control-v 的默认文件夹就是已删除的项目文件夹。我一直错误地在那里存档项目。所以最好的解决方案是让 .Delete 工作,但即便如此,这可能会在运行宏后改变 control-v 的默认行为。
这是代码。我只做了几天vba,所以任何关于我错过的东西的提示都会受到赞赏。
Option Explicit
Sub CreateHTML()
Select Case TypeName(Outlook.Application.ActiveWindow)
Case "Inspector"
CreateHTMLfromObject Outlook.Application.ActiveInspector.CurrentItem
Case "Explorer"
Dim objItem As Object
For Each objItem In Outlook.Application.ActiveExplorer.Selection
CreateHTMLfromObject objItem
Next
End Select
End Sub
Sub CreateHTMLfromObject(objItem As Object)
' For now, assume all items are mail items
'Select Case objItem.Class
'Case olMail
Dim objMailOrig As MailItem
Dim objMailCopy As MailItem ' Work on a copy of the message
Set objMailOrig = objItem
Set objMailCopy = objMailOrig.copy
' Where all HTML versions of messages will be stored
Dim fileDir As String
fileDir = "C:\Lib\olHTML\"
' A unique message id from the original message
Dim MsgId As String
MsgId = objMailOrig.EntryID
' The file the HTML version of the message will be stored in
Dim fileName As String
fileName = MsgId & ".html"
' The full file system path where the HTML verison of the message will be stored
Dim filePath As String
filePath = fileDir & fileName
' ---------------------------------------------------------------
' Save Attachments
' ---------------------------------------------------------------
' Subdirectory for attachments on this message
' A unique subdirectory for each message
Dim atmtDir As String
atmtDir = MsgId & "_atmt\"
' Full file system path to the attachment directory
Dim atmtDirPath As String
atmtDirPath = fileDir & atmtDir
' File system object for creating the attachment folder
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If (objMailCopy.Attachments.Count > 0) And (Not oFSO.FolderExists(atmtDirPath)) Then
oFSO.CreateFolder (atmtDirPath)
End If
' To hold the full file system path to each attachment file
Dim atmtFilePath As String
' String to accumulate HTML code for displaying links to attachments
' in the body of the HTML message
Dim atmtLinks As String
atmtLinks = " "
Dim atmt As Attachment
For Each atmt In objMailCopy.Attachments
atmtFilePath = atmtDirPath & atmt.fileName
atmt.SaveAsFile atmtFilePath
' create a relative URL
atmtLinks = atmtLinks & _
"<br><a href='" & atmtDir & atmt.fileName & "'>" & atmt.fileName & "</a>"
Next atmt
' ---------------------------------------------------------------
' Add links to attachments
' ---------------------------------------------------------------
' This changes the original message in Outlook - so we work on a copy
' Convert body to HTML if RTF, Text or other format
If (objMailCopy.BodyFormat = olFormatPlain Or olFormatRichText Or olFormatUnspecified) Then
objMailCopy.BodyFormat = olFormatHTML
End If
' Add attachments links at the beginning
If objMailCopy.Attachments.Count > 0 Then
objMailCopy.HTMLBody = _
"<p>" & "Attachments: " & atmtLinks & "</p>" & objMailCopy.HTMLBody
End If
' ---------------------------------------------------------------
' Save the HTML message file
' ---------------------------------------------------------------
objMailCopy.SaveAs filePath, olHTML
' ---------------------------------------------------------------
' Delete the copy from Outlook
' ---------------------------------------------------------------
'! This seems to have no effect
' objMailCopy.Delete
' Move copied message to deleted items folder
objMailCopy.Move Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems)
' ---------------------------------------------------------------
' Open the HTML file with default browser
' ---------------------------------------------------------------
Dim url As String
url = "file:///" & filePath
CreateObject("WScript.Shell").Run (url)
End Sub