首先,这正是我想要做的。
{}
或大括号中包含的表单中的字段,我听说它们都被称为。现在,到目前为止,我已经提出了代码。
Public Function GetIDsRegExp()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olApp = New Outlook.Application
Dim objRegExp As RegExp
Dim sString As String
Dim sBody As String
Dim objMatch As Match
Dim colMatches As MatchCollection
Set olMail = olApp.ActiveExplorer.Selection.Item(1)
'Debug.Print olMail.Body
sString = olMail.Body
Set objRegExp = New RegExp
With objRegExp
.Pattern = "(?:message|instrument)ID\s+:(\w+)"
.IgnoreCase = True
.Global = True
End With
If (objRegExp.Test(sString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(sString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
Debug.Print objMatch.SubMatches(0)
' sBody = Replace(Template, "{MessageID}", objMatch.SubMatches(0))
' sBody = Replace(Template, "{InstrumentID}", objMatch.SubMatches(0))
' Debug.Print sBody
Next
End If
Template = FindTemplate()
Debug.Print Replace(Template, "{Date}", Now())
End Function
Function FindTemplate()
Set objOutlook = GetObject("", "Outlook.Application")
Set objDrafts = objOutlook.GetNamespace("MAPI").GetDefaultFolder(16)
Set objItems = objDrafts.Items
For Each objDraft In objItems
If objDraft.Subject = "Environment : Prod International Trade Error" Then
FindTemplate = objDraft.Body
Exit Function
End If
Next
End Function
当我调试代码时,我得到了我想要的东西。现在,我需要用正确的子匹配提取替换模板中{}所包含的字段。我被困在5和6上。
答案 0 :(得分:1)
是否可以从{}更改为{地址},以便您可以替换字符串? https://msdn.microsoft.com/en-us/library/bt3szac5%28v=vs.90%29.aspx
String = Replace(title, "something", "{adres}")
还应该可以从vba编译整个电子邮件,这样你就可以绕过草稿文件夹了(这是我不完全得到的一步)?
https://msdn.microsoft.com/en-us/library/office/ff861332.aspx
Sub CreateMail()
Dim myItem As Object
Set myItem = Application.CreateItem(olMailItem)
myItem.Subject = "Mail to myself"
myItem.Display
End Sub