windows - Ms Word 2016 宏将文本转换为表格,每个单元格一个字,无论字数如何
问题描述
我已经录制了一个宏来将一个句子转换成一个单行表——每个单元格一个单词,然后做一些其他的事情,比如添加行等。
宏的步骤之一是选择句子,然后将文本转换为表格 > 在 [空格] 处分隔文本。这适用于我记录宏的句子,但是,宏仅存储该句子的单词数,然后我不能轻易地将其应用于较短或较长的句子。
输入:
Colorless green ideas sleep furiously at night
这是宏的代码,它只适用于具有相同字数的其他句子。短句 > 空单元格;更长的句子:堆积的行。
Sub Glossing()
'
' Glossing Macro
'
'
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
NumColumns:=8, NumRows:=1, AutoFitBehavior:=wdAutoFitContent
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InsertRowsBelow 1
Selection.InsertRowsBelow 1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=3
Selection.InsertColumns
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
Selection.Cells.Merge
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
End Sub
解决方案
下面的 VBA 应该完成您所描述的基本工作,但请注意 Word 表只能包含 63 列,因此如果您选择的单词多于该列,则此操作将失败。
Sub Glossing2()
'
' Glossing Macro
'
'
Dim r As Word.Range
Dim t As Word.Table
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
' Word creates the necessary number of columns if you omit
' the column count
Set t = Selection.ConvertToTable(Separator:=" ")
With t
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
' NB you have to specify a Column *object*
' not just a column *number*
' when you want to specify the "insert before" option
.Columns.Add .Columns(1)
.Rows.Add
.Rows.Add
Set r = .Range
' (Problem fixed by changing the second .Range.Start to .Range.End)
r.SetRange .Cell(3, 2).Range.Start, .Cell(3, .Columns.Count).Range.End
r.Cells.Merge
Set r = Nothing
.AutoFitBehavior wdAutoFitContent
End With
Set t = Nothing
End Sub
您可能需要多注意一开始是如何选择文本的,但在不知道确切要求的情况下,我决定不更改您已经拥有的内容。
推荐阅读
- java - Java drawRect() 方法有奇怪/错误的大小
- python - 枕头 - OSError:无法识别图像文件
- regex - 如何在 Google 表格中找到出现的德语变音符号?
- git - 合并拉取请求时,提交出现在本地不存在的 GitHub 上
- java - Java 泛型继承
- python - 单击带有 Selenium 的 WhatsApp 弹出窗口
- javascript - 使用 webpack 动态更改 Ejs 文件中的脚本标签
- javascript - 由于 useState 挂钩,我无法在 Safari 上正确处理状态
- graphics - 如何在 Windows 10 上更新 CUDA 驱动程序 nvidia?
- java - 我保存了一个待办事项列表,如何将其加载到我的待办事项列表应用程序中?