与我一起散步。删除Excel单元格格式文本中的标签
我已经建立了一个Access应用程序在我公司的一个内部项目管理数据。此应用程序的功能之一是查询数据库,然后将查询输出到Excel电子表格,然后将电子表格格式化为规格。
其中一个输出的细胞是从所述数据库中的富文本备注字段大量文本的。当富文本发送到Excel它承载的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个字符删除标签失败。所以我不能使用删除方法。
当我这样做:
With xlApp.Selection
.Replace what:="<strong>", replacement:=""
的标记都删除,但格式化全部被毁!那么有什么意义!?
我在寻找我的格式化文本和删除标记的方式。我正在考虑把大量文本和'分块'到许多单元格中,处理格式化和重新组装,但这听起来很困难,容易出错,甚至可能不起作用。
任何想法!?
谢谢!
我认为这是我现在领导的方向。我将不得不创建一些数据结构来描述在删除标签之前格式必须去的位置。 – Jesse 2011-05-05 15:56:19