【问题标题】:Export formatted bullets to an email from Excel TextBox将格式化的项目符号从 Excel 文本框导出到电子邮件
【发布时间】:2018-03-02 17:43:11
【问题描述】:

我一直在制作电子表格,以便我的团队能够更有效地管理我们的工作量,同时公司正在开发一种新工具。无论如何,工作表的作用是注入信息,然后单击一个按钮,它会填充一个 OFT 电子邮件模板,以便将信息发送出去。

问题是,我们的电子邮件严重依赖项目符号列表,我真的很难找到一种从 ActiveX 文本框有效添加项目符号的方法。

目前,我有一个按钮,可以将以下内容添加到文本框中:

[子弹] * 项目符号 1 * 子弹 2 * 子弹 3 [/bullets]

然后我有查找字符串的 Replace 语句,并用适当的 HTML 标记替换它们。代码如下:

' Add HTML formatting to text updates so it displays correctly in the email.
LatestUpdate.Text = Replace(LatestUpdate, "[bullets]", "<ul>")
LatestUpdate.Text = Replace(LatestUpdate, "[/bullets]", "</ul>")
LatestUpdate.Text = Replace(LatestUpdate, "* ", "<li>")
LatestUpdate.Text = Replace(LatestUpdate, vbCrLf, "<br>")

我遇到的问题是,非技术人员正在使用此文档,所以我真的很想以这样的方式拥有它,他们不必查看标记,但可以简单地添加项目符号直接从文本框。

我原本想用“

  • ”替换“*”,但是这并没有添加正确的
      标签,所以它实际上不是电子邮件中的项目符号列表。

      谁能帮助为最终用户简化此过程?我真的被困住了。

      圣杯是在文本框上启用富文本格式,但我不相信从我所做的所有研究来看这是可能的?

      TIA。

  • 【问题讨论】:

    标签: html excel outlook vba


    【解决方案1】:

    根据您的上一条评论,您要查找的不仅仅是文本框中的项目符号点,还有缩进。所以这里是一个尝试:

    首先在您的&lt;textbox&gt;_KeyUp 函数中添加以下内容:

    Private Sub txtBulletPoints_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Dim STRING_LENGTH As Long: STRING_LENGTH = 49
        Dim aLine() As String
        Dim aLineSpace() As String
        Dim iC As Integer
        Dim sText As String
        Dim bUpdate As Boolean
    
        ' Only do this if there is a string to work with
        If Len(Me.txtBulletPoints.Text) > 0 Then
    
            ' Set initial values
            aLine = Split(Me.txtBulletPoints.Text, vbCrLf)
            bUpdate = False
    
            ' First lets indent the last line if we need to
            If Left(aLine(UBound(aLine)), 2) = "- " Then
                For iC = LBound(aLine) To UBound(aLine)
                    If iC = UBound(aLine) Then
                        sText = sText & vbTab & aLine(iC)
                    Else
                        sText = sText & aLine(iC) & vbCrLf
                    End If
                Next
    
                Me.txtBulletPoints.Text = sText
    
            End If
    
            ' Now the tricky bit. Check if we have reached the end of the
            ' line so that we can indent the text into the next line
            If (Len(aLine(UBound(aLine))) >= STRING_LENGTH) And (InStr(1, aLine(UBound(aLine)), vbTab) = 1) Then
                For iC = LBound(aLine) To UBound(aLine)
                    If iC = UBound(aLine) Then
                        aLineSpace = Split(aLine(iC), " ")
    
                        ' As we have to indent the last bullet point line, call the finction to do that
                        sText = sText & SetIndentsInString(aLine(iC), STRING_LENGTH)
    
                    Else
                        sText = sText & aLine(iC) & vbCrLf
                    End If
                Next
    
                Me.txtBulletPoints.Text = sText
    
            End If
    
        End If
    
    End Sub
    

    现在在您的表单代码所在的位置添加以下 UDF(基本上与您的 &lt;textbox&gt;_KeyUp 函数所在的位置相同):

    Function SetIndentsInString(ByVal sString As String, ByVal iIndentLen As Long) As String
        Dim iC As Long
        Dim iLastTab As Long: iLastTab = 0
        Dim aSpace() As String
        Dim aTab() As String
        Dim sCurString As String
    
        ' Check if the string is the same as what it was last
        ' time (sLastString is a private module variable initialised
        ' to "" when the form is activated)
        If Replace(sString, vbTab, "") = Replace(sLastString, vbTab, "") Then
            ' Its the same string so lets return it as is
            SetIndentsInString = sString
        Else
            ' Its not the same string so set initial values
            sLastString = sString
            SetIndentsInString = ""
    
            ' Loop to see how many lines we have based on number of TABs in the string
            Do While InStr(iLastTab + 1, sString, vbTab) > 0
                iLastTab = iLastTab + InStr(iLastTab + 1, sString, vbTab)
            Loop
    
            ' If there is only 1 TAB, simply indent the line
            If iLastTab = 1 Then
                aSpace = Split(sString, " ")
                SetIndentsInString = Mid(sString, 1, Len(sString) - Len(aSpace(UBound(aSpace)))) & vbTab & "  " & aSpace(UBound(aSpace))
            Else
    
                ' More then 1 TAB.. damn!. Ok well lets work it
                aTab = Split(sString, vbTab)
                sCurString = aTab(UBound(aTab))
    
                ' Check if the last line of our bullet point has more characters then allowed in a line
                If Len(sCurString) >= iIndentLen Then
    
                    ' It does. Now loop through all the lines in our bullet point and set the last character in a new line with indent
                    aSpace = Split(sCurString, " ")
                    For iC = LBound(aTab) To UBound(aTab)
                        If iC = UBound(aTab) Then
                            SetIndentsInString = SetIndentsInString & Mid(sCurString, 1, Len(sCurString) - Len(aSpace(UBound(aSpace)))) & vbTab & "  " & aSpace(UBound(aSpace))
                        Else
                            SetIndentsInString = SetIndentsInString & aTab(iC) & vbTab
                        End If
                    Next
    
                Else
    
                    ' It doesnt. Loop through and send the string back
                    SetIndentsInString = sString
    
                End If
    
            End If
    
        End If
    
    End Function
    

    现在在同一个模块中,在顶部进行如下声明:

    Private sLastString As String
    

    基本上,上述内容将作为 like 一个要点,就像它在富文本框中一样。需要记住的是,您必须将STRING_LENGTH 设置为您的文本框将在给定的项目符号行中占用的字符数(您将不得不使用它)。下面是它如何为我工作的屏幕打印

    【讨论】:

    • 感谢 Zac,不幸的是,这并没有按预期工作。输出不是作为项目符号出现的,而是在其间带有项目符号标记的单行文本。例如:“这是一个带有项目符号的更新: • 项目符号 1 • 项目符号 2 • 项目符号 3”
    • 这不是作为要点出来的。但是用户只需键入“-”来表示一个要点,然后您应该能够在其上运行您的Replace 以将其转换为电子邮件中的要点。这样他们就不需要在文本框中添加"[bullets]" 标签
    • 啊,我明白了。尽管如此,它仍然没有真正实现目标,因为这只会给出一个以项目符号开头的行。我需要的是实际的项目符号,以便越过多行的较长项目符号保持缩进,人们可以添加嵌套项目符号等。嵌套项目符号可以很容易地通过在项目符号前用两个“标签”替换“- -”来修复。但长字符串不会缩进,因此它们保持内联。如果我能解决这个问题,我就赢了。
    • 有趣的小项目,用缩进重新发明项目符号。可能仍然不是您需要的,但我喜欢这样做:)
    • 对延迟回复表示歉意。我不是想重新发明项目符号,但我确实想要一个 HTML 格式的项目符号列表,而不是一些在行首带有项目符号点的文本。这将允许用户在电子邮件中正确格式化文本,并在需要时添加到项目符号列表中。我希望这很清楚。所以要从我现在的位置开始,我需要代码用适当的项目符号字符串(例如“-”)查找第一行并将其第一个实例替换为
    • ,然后替换所有其他只有
    • 的实例,然后是
    • 的最终实例
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签