使用 VBA 将单词列表转换为单词 table 中的单元格

Turn a list of words into cells in a Word table with VBA

我制作了一个脚本,将我所有拼错的单词粘贴到一个新文档中,所以现在我有一个文档文件,每个单词都是拼错的单词。该文档中目前有 167 个单词。我还制作了一个可以制作 tables 的脚本。我现在想将这 167 个单词中的每一个放入其自己的单元格中,结果是 table 有 167 个单元格,每个单元格包含一个单词。 目标是自动执行此操作,以便可以将给定文档中的任意数量的单词转换为 table.

我该怎么做呢?我知道答案与循环有关,这就是为什么这是我得到的最远的答案。

 Sub TurnInToTable()
 Dim actdoc As Document
 Dim newtbl As Table
 Dim aword As Range 
 Dim x As Integer 
 Set actdoc = ActiveDocument
    x = actdoc.Words.Count
 Set myrange = actdoc.Range(0, 0)
 Set newtbl = actdoc.Tables.Add(myrange, x, 2, wdWord9TableBehavior)
 For Each aword in actdoc.Words
 ?????
 Next aword
 End Sub

计划最终有一个包含两列的 table:一列包含拼写错误的单词,另一列包含正确的拼写,这样我就可以轻松地将所有对添加到自动拼写检查词典中同时.

假设您已将每个单词粘贴到一个新段落中,则无需循环遍历任何内容。您只需要将段落转换为 table:

Sub MispeltWordsTable()
   With ActiveDocument.Content
      With .ConvertToTable(Separator:=wdSeparateByParagraphs, NumColumns:=1, _
         NumRows:=.Paragraphs.Count, AutoFitBehavior:=wdAutoFitFixed)
         .style = "Table Grid"
         .ApplyStyleHeadingRows = True
         .ApplyStyleLastRow = False
         .ApplyStyleFirstColumn = True
         .ApplyStyleLastColumn = False
         .Columns.add
         .PreferredWidthType = wdPreferredWidthPercent
         .PreferredWidth = 100
      End With
   End With
End Sub