VBA 到 Lotus Notes - 带格式(颜色)的变量 body

VBA to Lotus Notes - Variable body with formatting ( Colors )

我目前从事工作流程的自动化,过去需要大量的手工工作并从多个来源收集数据,最后发送了一封电子邮件:

  • Header ( fixed ) Regular

  • Description ( One line for each cell with data in a given range ) Bold

  • Footer ( fixed ) - Text Color: Red

  • Attachment

好吧,我们有一个信纸来帮助处理电子邮件,但由于我不能保证每个body 都会正确设置信纸,我正在寻找一种更优雅的方式来做到这一点(基本上目标是做到 fool-proof ),所以我开始研究一种方法,在单元格中混合 VBA+公式。

到目前为止,我的代码在便笺上创建了消息,插入了地址列表、标题并附加了它生成的文件,但是在插入 body 时,机会难得!我可以插入一条 single-lined 消息,但没有任何格式或样式,即上面 body.

元素旁边以粗体显示的那些

我已经阅读了大约 3 天的问题和文章,但没有任何成功,我决定自己问一下,因为这是我的项目向前迈出的一大步,有没有办法做到这一点?我相信我正在寻找类似

的东西

notesmagicproperty.boldthisrange("B3")

that translates to

"03 - Lorem ipsum dolor sit amet"

提前致谢,Stack Overflow 已经救了我一千次了!

此外,很抱歉没有发布代码,我是在家里写这篇文章的,现在是凌晨 3 点,所以我现在无法访问它。

请注意此代码不是我的 我从 John_W 的 excel post 先生那里获取了它,我只是将它粘贴在这里,因为我想分享一些对我有帮助的东西因为它可能会帮助别人。另外,我不会 link 此处的页面,因为我认为这对 Stack Overflow 不公平,但我非常感谢 John_W 在线分享此内容。

Sub Notes_Email_Excel_Cells()

    Dim NSession As Object
    Dim NDatabase As Object
    Dim NUIWorkSpace As Object
    Dim NDoc As Object
    Dim NUIdoc As Object

    Set NSession = CreateObject("Notes.NotesSession")
    Set NUIWorkSpace = CreateObject("Notes.NotesUIWorkspace")
    Set NDatabase = NSession.GetDatabase("", "")

    If Not NDatabase.IsOpen Then
        NDatabase.OPENMAIL
    End If

    'Create a new document

    Set NDoc = NDatabase.CreateDocument

    With NDoc
        .SendTo = "email.address@email.com"       'CHANGE THIS
        .CopyTo = ""
        .subject = "Pasted Excel cells " & Now

        'Email body text, including marker text which will be replaced by the Excel cells

        .body = "Text in email body" & vbNewLine & vbNewLine & _
            "**PASTE EXCEL CELLS HERE**" & vbNewLine & vbNewLine & _
            "Excel cells are shown above"

        .Save True, False
    End With

    'Edit the just-created document to copy and paste the Excel cells into it

    Set NUIdoc = NUIWorkSpace.EDITDocument(True, NDoc)

    With NUIdoc

        'Find the marker text in the Body item

        .GotoField ("Body")
        .FINDSTRING "**PASTE EXCEL CELLS HERE**"
        '.DESELECTALL            'Uncomment to leave the marker text in place (cells are inserted immediately before)

        'Replace it with the Excel cells

        Sheets("Sheet1").Range("A1:E6").Copy       'CHANGE THIS
        .Paste
        Application.CutCopyMode = False

        .Send
        .Close
    End With

    Set NSession = Nothing

End Sub

0。 NotesRichTextRange.SetStyle 方法

NotesRichTextRange.SetStyle method is what you are looking for. For this method you need to create NotesRichTextStyle object. Also you need to SetBegin end SetEnd of range by using NotesRichTextNavigator 对象。
这是示例:

Dim ses As New NotesSession 
Dim doc As NotesDocument
Dim richText As NotesRichTextItem
Dim navigator As NotesRichTextNavigator
Dim range As NotesRichTextRange
Dim headerStyle As NotesRichTextStyle
Dim descriptionStyle As NotesRichTextStyle
Dim footerStyle As NotesRichTextStyle

'Create your doc.

'Generate rich text content:    
Set richText = doc.CreateRichTextItem("Body")
Set navigator = richText.CreateNavigator
Set range = richText.CreateRange

richText.AppendText("Header")
richText.AddNewline(1)

Set headerStyle = ses.CreateRichTextStyle
headerStyle.Underline = True

Set descriptionStyle = ses.CreateRichTextStyle
descriptionStyle.Bold = True

Set footerStyle = ses.CreateRichTextStyle
footerStyle.NotesColor = COLOR_RED

navigator.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH)

range.SetBegin(navigator)
range.SetEnd(navigator)

Call range.SetStyle(headerStyle)

For index% = 0 To 7
    richText.AppendText("Description" & index%)
    richText.AddNewline(1)

    navigator.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)

    range.SetBegin(navigator)
    range.SetEnd(navigator)

    Call range.SetStyle(descriptionStyle)
Next

richText.AppendText("Footer")
richText.AddNewline(1)

navigator.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)

range.SetBegin(navigator)
range.SetEnd(navigator)

Call range.SetStyle(footerStyle)

Call richText.EmbedObject(EMBED_ATTACHMENT, "", "SomeFile")

richText.Update

'Process your doc.

此示例生成此富文本:

1。 NotesDocument.RenderToRTItem 方法

另一种方法是使用NotesDocument.RenderToRTItem方法。对于此方法,您需要创建一个表单并根据需要设置样式。例如,创建一个表单 "Message" 并向该表单添加四个字段:

并在您的代码中使用此表单:

Dim ses As New NotesSession
Dim db As NotesDatabase
Dim messageDoc As NotesDocument
Dim attachment As NotesRichTextItem
Dim description(7) As String
Dim doc As NotesDocument
Dim richText As NotesRichTextItem

Set db = ses.CurrentDatabase
Set messageDoc = db.CreateDocument
messageDoc.Form = "Message"
messageDoc.Header = "Header"

For index% = 0 To Ubound(description)
    description(index%) = "Description" & index%
Next

messageDoc.Description = description
messageDoc.Footer = "Footer"

Set attachment = messageDoc.CreateRichTextItem("Attachment")
Call attachment.EmbedObject(EMBED_ATTACHMENT, "", "SomeFile")

'Create your doc.

'Generate rich text content:    
Set richText = doc.CreateRichTextItem("Body")
Call messageDoc.RenderToRTItem(richText)
richText.Update

'Process your doc.

此示例生成此富文本:

2。 NotesUIDocument.Import 方法

您可以在其他地方生成富文本内容,然后使用 NotesUIDocument.Import 方法将其导入到您的文档中。
这是导入 html 内容的示例:

Dim ses As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim richText As NotesRichTextItem
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument

'Generate html file
tempdir$ = Environ("Temp")

file = Freefile
filename$ = tempdir$ & "\temp.html"
Open filename$ For Output As file

Print #file, "<u>Header</u><br>"

For index% = 0 To 7
    Print #file, "<b>Description" & index% & "</b><br>"
Next

Print #file, "<font color='red'>Footer</font><br><br>"

Close file

Set db = ses.CurrentDatabase
Set doc = db.CreateDocument

'Create your doc.

'Add attachment to rich text:
Set richText = doc.CreateRichTextItem("Body")
Call richText.EmbedObject(EMBED_ATTACHMENT, "", "SomeFile")

Set uidoc = ws.EditDocument(True, doc)

uidoc.GotoField("Body")
uidoc.Import "html", filename$

'Process your doc.

此示例生成此富文本: