【问题标题】:Adding a new word to each subsequent cell in Word VBA向 Word VBA 中的每个后续单元格添加一个新单词
【发布时间】:2020-10-30 07:05:57
【问题描述】:

我一直在研究这段代码,它从文档中提取拼写错误的单词,然后将它们变成一个表格,其中所有拼写错误的单词都放在一列。然后单词被拼写检查,更正出现在另一列。我的代码做了我想做的一切,但是每个单元格上只出现第一个单词。我做错了什么?

Sub SuperSpellCheck()
Dim doc1 As Document
Dim doc2 As Document
Dim tb As Table
Set doc1 = ActiveDocument
Set doc2 = Documents.Add
doc1.Activate
Dim badw As Range
Dim rng As Range
Dim sugg As SpellingSuggestions    
Dim sug As Variant
err = doc1.SpellingErrors.Count
For Each badw In doc1.SpellingErrors
doc2.Range.InsertAfter badw & vbCr
Next
doc2.Activate
Set tb = ActiveDocument.Content.ConvertToTable(Separator:=wdSeparateByParagraphs, NumColumns:=1,                     
NumRows:=ActiveDocument.SpellingErrors.Count, AutoFitBehavior:=wdAutoFitFixed)
With tb
    .Style = "Table Grid"
     .ApplyStyleHeadingRows = True
     .ApplyStyleLastRow = False
     .ApplyStyleFirstColumn = True
     .ApplyStyleLastColumn = False
     .Columns.Add
     .PreferredWidthType = wdPreferredWidthPercent
     .PreferredWidth = 100
  End With
err2 = ActiveDocument.SpellingErrors.Count
i = 1
Set sugg = doc2.Range.GetSpellingSuggestions
For Each rng In doc2.Range.SpellingErrors
With rng
If sugg.Count > 0 Then
Set sug = .GetSpellingSuggestions
tb.Cell(i, 2).Range.InsertAfter sug(1)
End If
End With
Next
End Sub

【问题讨论】:

标签: vba ms-word


【解决方案1】:

未连接到您的问题,但您需要更改这些行

Err = doc1.SpellingErrors.Count
err2 = ActiveDocument.SpellingErrors.Count

收件人:

Dim errors1 as Long, dim errors2 as Long
errors1 = doc1.SpellingErrors.Count
errors2 = doc2.SpellingErrors.Count

Err 是 VBA 中的一个对象,用于保存代码生成的错误。您还没有声明这些变量。在代码模块的最顶部添加Option Explicit,您将收到任何未声明变量的警报。要在以后自动打开此功能,请转到工具 |选项 |编辑器并确保选中需要变量声明。

我会改变

Dim sugg As SpellingSuggestions    
Dim sug As Variant

   Dim docSugg As SpellingSuggestions
   Dim rngSugg As SpellingSuggestions
   Dim sug As SpellingSuggestion

这将使它们中的每一个代表什么更清楚。 SpellingSuggestionsSpellingSuggestion 对象的集合,因此您可以使用 sug 循环访问集合。

   i = 1
   Set sugg = doc2.Range.GetSpellingSuggestions
   For Each rng In doc2.Range.SpellingErrors
      With rng
         If sugg.Count > 0 Then
            Set sug = .GetSpellingSuggestions
            tb.Cell(i, 2).Range.InsertAfter sug(1)
         End If
      End With
   Next

在此代码块中,您首先将未声明的变量i 设置为值1,但随后不会增加该值。这将导致您的所有拼写建议都插入到同一个单元格中。此外,当您插入拼写建议时,您只会插入第一个,因为您没有办法遍历它们。所以我会把它改写为:

   i = 1
   Set docSugg = doc2.Range.GetSpellingSuggestions
   For Each rng In doc2.Range.SpellingErrors
      With rng
         If docSugg.Count > 0 Then
            Set rngSugg = .GetSpellingSuggestions
            For Each sug In rngSugg
               tb.Cell(i, 2).Range.InsertAfter sug
            Next
         End If
      End With
      i = i + 1
   Next

编辑:如果您只想要第一个建议的拼写,请使用:

   i = 1
   Set docSugg = doc2.Range.GetSpellingSuggestions
   For Each rng In doc2.Range.SpellingErrors
      With rng
         If docSugg.Count > 0 Then
            Set rngSugg = .GetSpellingSuggestions
            tb.Cell(i, 2).Range.InsertAfter rngSugg(1)
         End If
      End With
      i = i + 1
   Next

【讨论】:

  • 当我进行您所说的更改时,我在正确的单元格上为每个拼写错误的单词得到了正确的单词(这很棒),但是在许多单元格中,有许多不同的单词都粘在一起.例如,有一个单元格包含文本“Stratgfies”。旁边是一个包含文本“StratifiesStrategies”的单元格,我只想为每个单元格中的每个拼写错误的单词提供第一个 SpellingSuggestion。这就是为什么我把“1”放在“sug”之后。
  • @CountSpergulia - 你的问题是“每个单元格上只出现第一个单词。我做错了什么?” - 因此我的原始答案,我现在已经编辑。
  • 它有效!!!!如果您不介意,现在只问最后一个问题:为什么需要两个 SpellingSuggestion 集合?
  • @CountSpergulia - 除了它们在您的原始代码中之外,没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2014-01-04
相关资源
最近更新 更多