VBA到Lotus Notes – 带格式的variables主体(颜色)

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

  • 标题(固定) 正常

  • 说明(每个单元格与给定范围内的数据一行) 粗体

  • 页脚(固定) – 文字颜色:红色

  • 附件

那么,我们有一个文具来帮助电子邮件,但因为我不能保证每个人都会正确设置文具,我正在寻找一个更优雅的方式来这样做(基本上,目标是使其防呆),所以我开始工作的方式来混合VBA +公式在单元格中。

到目前为止,我的代码在笔记上创build消息,插入地址列表,标题和附加它生成的文件,但是当涉及到插入身体时,胖的机会! 我可以插入单行消息,但没有任何格式或样式,在主体元素旁边以粗体显示。

  • 我追逐的是一种将给定单元格中的文本从我的电子表格粘贴到笔记上并对其应用格式的方法,因此每个单元格的值将是笔记上具有不同样式的文本行。

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

notesmagicproperty.boldthisrange( “B3”)

这意味着

“03 – Lorem ipsum dolor sit amet”

在此先感谢,堆栈溢出已经救了我一千次!

另外,抱歉没有发布代码,我从家里写这个,现在是凌晨3点,所以我现在还没有访问它。

0. NotesRichTextRange.SetStyle方法

NotesRichTextRange.SetStyle方法是你正在寻找的。 对于这个方法,你需要创buildNotesRichTextStyle对象。 还需要通过使用NotesRichTextNavigator对象SetBegin结束SetEnd范围。
这里是例子:

 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. 

这个例子生成这个富文本:
带有NotesRichTextRange.SetStyle方法的丰富文本

1. NotesDocument.RenderToRTItem方法

另一种方法是使用NotesDocument.RenderToRTItem方法。 对于这种方法,您需要创build一个窗体并根据需要进行设置。 例如,创build一个表单“消息”,并添加到这个表单四个字段:
“消息”形式
在你的代码中使用这个表单:

 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. 

这个例子生成这个富文本:
带有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. 

这个例子生成这个富文本:
带有NotesUIDocument.Import方法的丰富文本

请注意,这段代码并不是我从用户John_W中拿出来的,我只是把它粘贴在这里,因为我想分享一些帮助我的东西,因为它可能帮助别人。 另外,我不会在这里链接页面,因为我不认为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