2017-10-10 158 views
0

我正在開發一個腳本來格式化Outlook模板並通過單擊按鈕發送它。 主要目標是在間距之後/之前使用單行距離和0pt,並將模板上的所有內容控件(例如ComboBoxes)轉換爲文本/字符串。使用VBA將內容控制值轉換爲字符串

Sub FormatAndSend() 
    Dim CurrentMessage As Outlook.MailItem 

    If TypeName(Application.ActiveWindow) = "Inspector" Then 
     Set CurrentMessage = Application.ActiveWindow.CurrentItem 

     If CurrentMessage.To = "" Then 
      If CurrentMessage.CC = "" Then 
       If CurrentMessage.BCC = "" Then Exit Sub 
      End If 
     End If 

     CurrentMessage.HTMLBody = Replace(CurrentMessage.HTMLBody, vbCr, vbCrLf) 
     'Workaround to Remove Line Spacing (not working) 
     '& 
     'Convert Content Control selected values to String ... 

     CurrentMessage.Send 

     Set CurrentMessage = Nothing 
    End If 
End Sub 

是否有更簡單的方法來格式化現有模板?我已經嘗試了幾件事情,如用ASCII字符,HTML元素,關鍵字替換...

此外,是否可以將內容控制選定值轉換爲普通文本?

+0

好吧,那麼你打算如何將組合框「轉換」爲文本?具體到底是什麼意思,以及你寫的代碼是什麼,特別是在哪裏以及它是如何工作的? –

+0

我的意思是,轉換組合框選定的值,所以當有人轉發/回覆郵件時,其內容無法更改 – mafap

+0

我不熟悉Outlook對象模型,但快速瀏覽一下MailItem類顯示它沒有看起來沒有任何'Controls'集合,所以解析'HtmlBody'似乎是你最好的選擇。 –

回答

0

我設法讓我的問題通過使用正則表達式來移除OOXML元素並通過更改CSS屬性來解決。

Dim regX As Object 
Set regX = CreateObject("VBScript.RegExp") 

regX.Global = True 
regX.MultiLine = True 
regX.IgnoreCase = False 
regX.Pattern = "<\/?w:[A-z].*?>" 

CurrentMessage.HTMLBody = regX.Replace(CurrentMessage.HTMLBody, "") 
CurrentMessage.HTMLBody = Replace(CurrentMessage.HTMLBody, "<p class=MsoNormal>", _ 
          & "<p class=MsoNormal style='margin:0;line-height:0;'>") 
Set regX = Nothing