查找并替换子弹列表到html样式

时间:2012-05-29 03:28:10

标签: replace vbscript ms-word word-vba word-vba-mac

我想查找项目符号列表的实例,以替换为html标记的列表。请参阅下面的示例:

my_doc.docx ...

text,text,text
My bullet list:
   • List point one
   • List point two
Some more text here.

...

查找和替换导致

...

text,text,text
My bullet list:
<ul>
<li>List point one</li>
<li>List point two</li>
</ul>
Some more text here.

...

我已尝试find and replace寻找子弹角色;不起作用,因为它的格式。还尝试find and replace用于样式为“List bullet”的行以及我能找到的任何其他列表样式;不起作用,(也许是因为我使用Word for Mac似乎有些错误)

编辑: 我有以下VBScript,在我的文档中找到具有子弹样式的行。我现在需要这个脚本来重写它找到的行&lt;李&GT;最后的标签。

Sub FindBullet()
Dim oPara As Word.Paragraph
Dim count As Integer

count = 0
Selection.WholeStory
With Selection
    For Each oPara In .Paragraphs
    If oPara.Range.ListFormat.ListType = _
    WdListType.wdListBullet Then

          count = count + 1

            # from here down it gets shaky!!!

            With ActiveDocument.Range.Find
              .Text = #How do i convert the oPara to a string here?!?
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = True
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False

              .ClearFormatting
              With .replacement
                .ClearFormatting
                .Text = # how do i specify this is where i want the "<li>" & oPara & "</li>"
              End With
              .Execute Replace:=wdReplaceAll

        End If
    Next
End With
'Gives you the count of bullets in a document
MsgBox count & " replacements"
End Sub

1 个答案:

答案 0 :(得分:3)

您可以使用( InsertBefore InsertAfter )在段落中插入文字。 这适用于Word Mac。

Sub FindBullet()
Dim count As Integer
count = 0
Set myStyle = ActiveDocument.Styles("Body text") ' replacement style
bulletList = WdListType.wdListBullet

 ' each list instead of each paragraph of the document
For Each thisList In ActiveDocument.Lists
      For Each p In thisList.ListParagraphs
           If p.Range.ListFormat.ListType = bulletList Then
                p.Style = myStyle ' change the style to "Body text" 
                p.Range.InsertBefore ("<li>")
                Set aRange = p.Range
                aRange.End = aRange.End - 1
                aRange.InsertAfter ("</li>")
                count = count + 1
            End If
      Next
Next
MsgBox count & " replacements"
End Sub