2016-08-23 61 views
0

我使用了400多页的编码手册,不幸的是关闭了手册中所有注释的绿色。我无法撤销它,因为直到为时已晚,我才注意到它。它毁了多年的工作。如何在Word 2016中将VBA写入以//开头的格式?

我该如何编写VBA来解析文档查找以//开头的句子,并以段落标记结尾并更改它们的颜色?或者给他们分配一个风格?

这里是我所拼凑起来,我很佩服谁可以写代码,而无需intellisence,它就像试图找到自己的方式的人开始蒙住

Dim oPara As Word.Paragraph 
    Dim rng As Range 
    Dim text As String 

    For Each oPara In ActiveDocument.Paragraphs 
    If Len(oPara.Range.text) > 1 Then 
      Set rng = ActiveDocument.Range(oPara.Range.Start,oPara.Range.End) 

    With rng.Font 
      .Font.Color = wdColorBlue 
    End With 
    End If 
    Next 
    End Sub 

回答

0

以下似乎工作:

Dim oPara As Word.Paragraph 
Dim text As String 

For Each oPara In ActiveDocument.Paragraphs 
    text = oPara.Range.text 
    'Check the left 2 characters for // 
    If Left(oPara.Range.text, 2) = "//" Then 
    oPara.Range.text = "'" & text 
    End If 
Next 

我假设你正在使用VBA,所以通过在//前面放置一个“//将它变成绿色”。如果需要,您可以修改代码以替换//。 opera.range.text应该抓住整个段落。

相关问题