使用MS Word VBA读取字符串的第一个字符,然后将其删除

时间:2016-03-01 22:37:30

标签: vba ms-word

我有这段代码来计算段落中的字符数,如果它小于x字下划线:

Sub Underline_Header()

  Dim numOfParagraphs As Integer
  numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
  Selection.HomeKey Unit:=wdStory

  For x1 = 1 To numOfParagraphs
    Selection.Paragraphs(1).Range.Select
    char_count = Len(Selection.Paragraphs(1).Range)

    If char_count < 50 Then
      Selection.Font.Underline = True
      'Selection.TypeText Text:="this text" & Selection
    End If

  Selection.MoveDown Unit:=wdParagraph, Count:=1

  Next x1

End Sub

相反,我试图让宏读取每个段落的第一个字符,如果它是-(减号),那么这是触发下划线的条件。我试过了:

 char1 = Left(Len(Selection.Paragraphs(1).Range), 1)
 If char1 = "-" Then

但没有任何反应。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

对于后人,感谢海报Kris B和OpiesDad:

Sub Underline_Header()

Dim numOfParagraphs As Integer
numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
Selection.HomeKey Unit:=wdStory
For x1 = 1 To numOfParagraphs

Selection.Paragraphs(1).Range.Select


char1 = Left(Selection.Paragraphs(1).Range, 1)
 If char1 = "-" Then

End If

Selection.MoveDown Unit:=wdParagraph, Count:=1

Next x1


End Sub