从Word复制到Outlook时保留格式 [英] Retain formatting when copying from word to outlook
问题描述
我有一个代码将某种格式的文本替换成一个超链接。这个代码在收到的电子邮件中有效。
传入电子邮件 - >将电子邮件复制到文字编辑器(格式化丢失) - >进行必要的更改 - >从文字编辑器复制到Outlook邮件项目(再次被取代的超链接在邮件项目丢失)
我的代码在这里为您的参考..
<$ Sub $ IncomingHyperlink(MyMail As MailItem)
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim strtemp As String
Dim RegExpReplace As String
Dim RegX As Object
Dim myObject As Object
Dim myDoc As Word.Document
Dim mySelection As Word.Selection
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
$ b $ Set objWord = CreateObject(Word.Application)
objWord.Visible = True
'Set myDoc = objWord.Documents.Open(filename)
'Set objDoc = objWord.Documents.Open(C:\test.do c)
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeTextGOOD& objMail.HTMLBody
使用objSelection.Find
.ClearFormatting
.Text =ASA [0-9] [0-9] [0-9] [0-9] [az] [az]
.Forward = True
.Wrap = wdFindAsk
.MatchWildcards = True
End With
objSelection.Find.Execute
objSelection.Hyperlinks.Add锚点:= objSelection.Range,_
地址:=http://www.code.com/& objSelection.Text,_
TextToDisplay:= objSelection.Text
objMail.HTMLBody = objDoc.Range(0,objDoc.Range.End)
objMail.Save
Set objMail = Nothing
End Sub
此代码仅替换首先出现需要的文字,不能取代其他文字。
请帮助解决这些问题。谢谢...
为了替换每一个正则表达式,你可以遍历结果:
与objSelection.Find
.ClearFormatting
.Text =ASA [0-9] [0-9] [0-9] [0-9] [az] [az]
.Forward = True
.Wrap = wdFindAsk
.MatchWildcards = True
虽然objSelection.Find。执行
Hyperlinks.Add锚点:= objSelection.Range,_
地址:=http://www.code.com/& objSelection.Text,_
TextToDisplay:= objSelection.Text
objSelection.Collapse wdCollapseEnd $ b $ Wend
End With
为了保持格式化,您是否尝试(如果可能)在Outlook中只执行vba?
关心,
最大值
I have a code which replaces the text of certain format into a hyperlink. This code works during an incoming email.
Incoming email -> copy the email to word editor(formatting lost) -> make necessary changes -> copy from word editor to outlook mail item(again replaced hyperlinks gets lost in mail item)
My code is here for your refernce..
Sub IncomingHyperlink(MyMail As MailItem)
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim strtemp As String
Dim RegExpReplace As String
Dim RegX As Object
Dim myObject As Object
Dim myDoc As Word.Document
Dim mySelection As Word.Selection
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Set myDoc = objWord.Documents.Open("filename")
'Set objDoc = objWord.Documents.Open("C:\test.doc")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeText "GOOD" & objMail.HTMLBody
With objSelection.Find
.ClearFormatting
.Text = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Forward = True
.Wrap = wdFindAsk
.MatchWildcards = True
End With
objSelection.Find.Execute
objSelection.Hyperlinks.Add Anchor:=objSelection.Range, _
Address:="http://www.code.com/" & objSelection.Text, _
TextToDisplay:=objSelection.Text
objMail.HTMLBody = objDoc.Range(0, objDoc.Range.End)
objMail.Save
Set objMail = Nothing
End Sub
Also, this code replaces only the first occurrence of the needed text and does not replace others. Please help solve these problems. Thank you...
In order to replace every occurrences of the regex, you can loop over the results :
With objSelection.Find
.ClearFormatting
.Text = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Forward = True
.Wrap = wdFindAsk
.MatchWildcards = True
While objSelection.Find.Execute
Hyperlinks.Add Anchor:= objSelection.Range, _
Address:="http://www.code.com/" & objSelection.Text, _
TextToDisplay:=objSelection.Text
objSelection.Collapse wdCollapseEnd
Wend
End With
In order to keep your formatting, did you try (if possible) to execute your vba only in Outlook ?
Regards,
Max
这篇关于从Word复制到Outlook时保留格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!