需要能够输入更多的单词

时间:2019-07-30 21:20:33

标签: vba ms-word

我正试图使我的宏弹出一个搜索框,使我可以输入所需的任意单词,并用逗号分隔,然后在文档列表中找到每个单词并将其变为粗体和蓝色。我现在拥有的实际上确实可以完成我想要的操作。它将找到我输入到搜索框中的每个单词(用逗号分隔),找到该单词,然后将其更改为粗体蓝色字体。

问题是这样的:我需要能够输入页面或更多单词。 InputBox一次只能提供255个字符。是否对我的代码进行了修改,使我可以粘贴的单词数量不受限制?

我是新来的人,这无济于事。我花了一天半的时间才能到达现在的位置。

Sub BlueWords()
  Dim blueword As String
  Dim numberofwords As Long

  Application.ScreenUpdating = False

  ' Enter words that need to become bold blue words.
  blueword = InputBox("Enter items to be found here,seperated by comma: ", "Items to be found")
  numberofwords = UBound(Split(blueword, ","))

 ' Find each item and replace it with new one respectively.
  For numberofwords = 0 To numberofwords
    With Selection
      .HomeKey Unit:=wdStory
      With Selection.Find
        .Text = Split(blueword, ",")(numberofwords)
        .Replacement.Text = ""
        .Replacement.Font.Bold = True
        .Replacement.Font.ColorIndex = wdBlue
        .Format = True
        .MatchWholeWord = True
      End With
    Selection.Find.Execute Replace:=wdReplaceAll
  End With
Next numberofwords

  Application.ScreenUpdating = True

End Sub

有效。我只需要能够一次输入超过255个字符的单词。我需要“没有限制”。

2 个答案:

答案 0 :(得分:1)

相反,添加用户表单,然后添加一个按钮和一个文本框(表单中的文本框没有限制),然后将按钮设置为运行宏,以从表单文本框中获取输入

答案 1 :(得分:1)

Sub BlueWords()
  Dim blueword As String
  Dim numberofwords As Long

  Application.ScreenUpdating = False

  ' Enter words that need to become bold blue words.
  UserForm1.Show

  'blueword = The super long input string in the userform
  blueword = UserForm1.TextBox1.Text
  numberofwords = UBound(Split(blueword, ","))

 ' Find each item and replace it with new one respectively.
  For numberofwords = 0 To numberofwords
    With Selection
      .HomeKey Unit:=wdStory
      With Selection.Find
        .Text = Split(blueword, ",")(numberofwords)
        .Replacement.Text = ""
        .Replacement.Font.Bold = True
        .Replacement.Font.ColorIndex = wdBlue
        .Format = True
        .MatchWholeWord = True
      End With
    Selection.Find.Execute Replace:=wdReplaceAll
  End With
Next numberofwords

  Application.ScreenUpdating = True

  Unload UserForm1

End Sub
相关问题