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

0 format excel vba lotus-notes styling

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

  • 标题(固定)常规

  • 描述(每个单元格占一行,数据在给定范围内)粗体

  • 页脚(固定)-文本颜色:红色

  • 依恋

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

到目前为止,我的代码在注释上创建消息,插入地址列表、标题并附加它生成的文件,但是当涉及到插入正文时,机会很大!我可以插入单行消息,但没有任何格式或样式,上面描述的内容在正文元素旁边以粗体显示。

  • 我正在寻找一种将电子表格中给定单元格中的文本粘贴到注释并对其应用格式的方法,因此每个单元格值将是注释上的一行文本,具有不同的样式。

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

Notesmagicproperty.boldthisrange("B3")

这意味着

“03 - Lorem ipsum dolor sat amet”

预先感谢,Stack Overflow 已经救了我一千次了!

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

nem*_*Bu4 5

0.NotesRichTextRange.SetStyle方法

NotesRichTextRange.SetStyle方法就是您正在寻找的。对于此方法,您需要创建NotesRichTextStyle对象。您还需要使用对象来SetBegin结束SetEnd范围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.
Run Code Online (Sandbox Code Playgroud)

此示例生成以下富文本:
使用 NotesRichTextRange.SetStyle 方法的富文本

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.
Run Code Online (Sandbox Code Playgroud)

此示例生成以下富文本:
使用 NotesDocument.RenderToRTItem 方法的富文本

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.
Run Code Online (Sandbox Code Playgroud)

此示例生成以下富文本:
使用 NotesUIDocument.Import 方法的富文本