将新邮件中的关键字替换为从 word 文档中复制的内容
Replace Keyword in a new mail with copied content from a word document
我浏览了该网站上的很多主题,但找不到解决我的问题的方法。
这里是上下文:
我正在尝试从 Word 文档生成包含正文的电子邮件 => 这里没问题。
在这个文档中有关键字,我想用我从另一个词复制的内容替换。
我知道如何粘贴在文档末尾,但不会用复制的内容替换关键字。
这是我的代码:
Sub DisplayMail()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim OlApp As Outlook.Application
Dim OlItem As Outlook.MailItem
Set WDObj = ThisWorkbook.Sheets("Modèle").OLEObjects("Objet 1")
WDObj.Activate
WDObj.Object.Application.Visible = False
Set WDApp = GetObject(, "Word.Application")
Set WDDoc = WDApp.ActiveDocument
WDDoc.Content.Copy
Set OlApp = CreateObject("Outlook.application")
OlApp.GetNamespace("MAPI").Logon
Set OlItem = OlApp.CreateItem(olMailItem)
With OlItem
.To = ""
.CC = ""
.BodyFormat = olFormatHTML
.Subject = "test"
Set Editor = .GetInspector.WordEditor
Editor.Content.Select
Editor.Application.Selection.Paste
'this works fine, the email's body now looks like my first word document
------------
Set WDObj = ThisWorkbook.Sheets("Sheet2").OLEObjects("Objet 2")
WDObj.Activate
WDObj.Object.Application.Visible = False
Set WDDoc = WDApp.ActiveDocument
WDDoc.Content.Copy
'Copy of the content of my second word document (that's what I want to replace the keyword with)
'Editor.Characters.Last.Select
'Editor.Application.Selection.Paste
'code I use to copy at the end of the email body
------------
------------
With Editor.Content.Find
.Text = "#Keyword#"
.Replacement.Text = "text"
.Forward = True
.Execute Replace:=wdReplaceAll
End With
'with this code I can replace the keyword with Text but not with the content of my second word document
.Display
End With
End Sub
要使用剪贴板的内容替换文本,请使用搜索代码:^c
例如(来自问题中发布的代码):
With Editor.Content.Find
.Text = "#Keyword#"
.Replacement.Text = "^c"
.Forward = True
.Execute Replace:=wdReplaceAll
End With
您可以通过显示 "Replace" 对话框(按 Ctrl+H)、单击 "More" 然后单击 "Special" 来找到此类特殊搜索代码的列表。有 "Find" 和 "Replace" 的列表 - 单击对话框顶部的相应框以获取该框的列表。
我浏览了该网站上的很多主题,但找不到解决我的问题的方法。
这里是上下文:
我正在尝试从 Word 文档生成包含正文的电子邮件 => 这里没问题。
在这个文档中有关键字,我想用我从另一个词复制的内容替换。
我知道如何粘贴在文档末尾,但不会用复制的内容替换关键字。
这是我的代码:
Sub DisplayMail()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim OlApp As Outlook.Application
Dim OlItem As Outlook.MailItem
Set WDObj = ThisWorkbook.Sheets("Modèle").OLEObjects("Objet 1")
WDObj.Activate
WDObj.Object.Application.Visible = False
Set WDApp = GetObject(, "Word.Application")
Set WDDoc = WDApp.ActiveDocument
WDDoc.Content.Copy
Set OlApp = CreateObject("Outlook.application")
OlApp.GetNamespace("MAPI").Logon
Set OlItem = OlApp.CreateItem(olMailItem)
With OlItem
.To = ""
.CC = ""
.BodyFormat = olFormatHTML
.Subject = "test"
Set Editor = .GetInspector.WordEditor
Editor.Content.Select
Editor.Application.Selection.Paste
'this works fine, the email's body now looks like my first word document
------------
Set WDObj = ThisWorkbook.Sheets("Sheet2").OLEObjects("Objet 2")
WDObj.Activate
WDObj.Object.Application.Visible = False
Set WDDoc = WDApp.ActiveDocument
WDDoc.Content.Copy
'Copy of the content of my second word document (that's what I want to replace the keyword with)
'Editor.Characters.Last.Select
'Editor.Application.Selection.Paste
'code I use to copy at the end of the email body
------------
------------
With Editor.Content.Find
.Text = "#Keyword#"
.Replacement.Text = "text"
.Forward = True
.Execute Replace:=wdReplaceAll
End With
'with this code I can replace the keyword with Text but not with the content of my second word document
.Display
End With
End Sub
要使用剪贴板的内容替换文本,请使用搜索代码:^c
例如(来自问题中发布的代码):
With Editor.Content.Find
.Text = "#Keyword#"
.Replacement.Text = "^c"
.Forward = True
.Execute Replace:=wdReplaceAll
End With
您可以通过显示 "Replace" 对话框(按 Ctrl+H)、单击 "More" 然后单击 "Special" 来找到此类特殊搜索代码的列表。有 "Find" 和 "Replace" 的列表 - 单击对话框顶部的相应框以获取该框的列表。