繁体   English   中英

VBA到Lotus Notes-具有格式的可变主体(颜色)

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

我目前正在工作中的流程自动化中,该流程过去通常需要大量的手工工作并从多个来源收集数据,最终以以下方式发送电子邮件:

  • 标头(固定) 常规

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

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

  • 附件

好吧,我们有一个文具可以帮助您发送电子邮件,但是由于我不能保证每个人都可以正确设置文具,因此我正在寻找一种更为优雅的方法(基本上,目标是使其变得万无一失。 ),因此我开始研究将VBA +公式混合到单元格中的方法。

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

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

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

notesmagicproperty.boldthisrange( “B3”)

转化为

“ 03-Lorem ipsum dolor坐在amet”

在此先感谢您,Stack Overflow已为我节省了一千次!

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

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.

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

1. NotesDocument.RenderToRTItem方法

另一种方法是使用NotesDocument.RenderToRTItem方法。 对于此方法,您需要创建表单并根据需要设置样式。 例如,创建一个表单“消息”,并将四个字段添加到该表单中:
“留言”表格
并在您的代码中使用以下形式:

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方法的富文本

请注意,这段代码不是我在一个excel先生帖子中从用户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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM