首页 文章

从Excel单元格中的格式化文本中删除标记

提问于
浏览
0

和我一起走了一会儿 .

我已经构建了一个Access应用程序来管理我公司内部项目的数据 . 此应用程序的一个功能是查询数据库,然后将查询输出到Excel电子表格,然后将电子表格格式化为规范 .

输出的一个单元格是数据库中Rich Text Memo字段中的大量文本 . 当富文本发送到Excel时,它带有指示 bold 或斜体的HTML标记,因此对于输出,我必须添加格式并删除标记 .

以下是我需要格式化的文本示例(此文本位于单个单元格中):

For each participant, record 1 effort per lesson delivered

   • Time Spent = # minutes spent on lesson

<strong>OR</strong>
For each participant, record 1 effort per month

   • Time Spent = total # minutes spent on lessons that month

    <strong>Note:</strong> Recording 1 effort per lesson is recommended but not     required
<strong>Note:</strong> Use groups function in ABC when appropriate (see <u>Working With     Groups</u> in ABC document library on the ABC portal)

我有三个简洁的小递归函数来格式化文本,这里是粗体函数:

Function BoldCharacters(rng As Range, Optional ByVal chrStart As Long)
'This will find all the "<strong></strong>" tags and bold the text in between.
Dim tagL As Integer
tagL = 8
rng.Select

If chrStart = 0 Then chrStart = 1

b1 = InStr(chrStart, ActiveCell.Value, "<strong>") + tagL
    If b1 = tagL Then Exit Function
b2 = InStr(b1, ActiveCell.Value, "</strong>")

ActiveCell.Characters(Start:=b1, Length:=b2 - b1).Font.Bold = True

'Remove the tags
'ActiveCell.Characters(Start:=1, Length:=1).Delete
'ActiveCell.Characters(Start:=b2 - tagL, Length:=tagL + 1).Delete

'Recursion to get all the bolding done in the cell
Call BoldCharacters(ActiveCell, b2 + tagL + 1)

End Function

现在这是问题所在 . 这很好地格式化了文本 . 但是当我尝试使用它删除标记时,"ActiveCell.Characters.Delete"方法失败,因为该单元格包含超过255个字符 . 所以我不能使用delete方法 .

当我这样做时:

With xlApp.Selection
    .Replace what:="<strong>", replacement:=""

标签全部被删除,但格式化全部被销毁!那有什么意义呢??

I'm looking for a way of formatting my text and removing the tags. 我'm considering taking the large bit of text and '将它分解成许多单元格,处理格式化和重新组装,但这听起来很难,容易出错,甚至可能无法正常工作 .

有任何想法吗!?

谢谢!

3 回答

  • 0

    您可能希望在将数据导出到Excel之前删除格式 . 在删除格式的同时,将格式信息(位置,长度,样式)存储到数据结构中 . 导出“纯文本”数据后,您可以迭代结构并在Excel中应用格式 . 这可能是一个耗时的过程,具体取决于您在给定时间计划导出的记录数量,但这将取消Excel强加的限制 .

  • 1

    如果它是格式良好的html(即它总是有结束标记),那么你可以使用正则表达式 .

    Dim data As String
    data = "For each participant, record 1 effort per lesson delivered     • Time Spent = # minutes spent on lesson  <strong>OR</strong> For each participant, record 1 effort per month     • Time Spent = total # minutes spent on lessons that month      <strong>Note:</strong> Recording 1 effort per lesson is recommended but not     required <strong>Note:</strong> Use groups function in ABC when appropriate (see <u>Working With     Groups</u> in ABC document library on the ABC portal)"
    Dim r As New RegExp
    r.Pattern = "<(.|\n)*?>"
    r.Global = True
    Debug.Print r.Replace(data, "")
    

    要使用RegExp对象,请将引用设置为 Microsoft VBScript Regular Expressions 5.5 .

    心连心

  • 0

    这些内容可能有用:

    Sub DoFormat(rng As Range)
        Dim DataObj As New MSForms.DataObject
        Dim s As String, c As Range
    
        For Each c In rng.Cells
            s = "<html>" & Replace(c.Value, " ", "&nbsp;") & "</html>"
            DataObj.SetText s
            DataObj.PutInClipboard
            c.Parent.Paste Destination:=c
        Next c
    
    End Sub
    

    您需要引用“Microsoft Forms 2.0对象库”

相关问题