Word 宏:将文本插入到表格的每个选定单元格中

Word 宏:将文本插入到表格的每个选定单元格中

我正在尝试格式化单元格并在表格中的每个选定单元格中插入文本(在本例中为复选框)。这用作我工厂紧急程序中的占位辅助工具。单元格格式正确,但文本只会插入到第一个选定的单元格中。我在编写宏方面完全是业余爱好者,不知道如何创建循环以转到每个选定的单元格。我一直找不到合适的答案。

由于程序的性质,需要复选框的区域可能分散在各处,因此我需要能够手动选择不同位置的多个单元格。我不能只指定整行或整列。

示例 VBA:

Sub CkBox2()
'
' CkBox2 Macro
' Inserts check box in each SELECTED cell.
'
Selection.Style = ActiveDocument.Styles("Normal")
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.ParagraphFormat.SpaceBefore = 6
Selection.ParagraphFormat.SpaceAfter = 6

Selection.Font.Size = 14
Selection.TypeText Text:=""
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=168, Unicode:=True

End Sub

使用这个,我最终在第一个选定的单元格中只得到一个复选框:(见下面的图 1)

图1

答案1

实际上,使用 VBA 直接访问非连续 Select 的各个部分是不可能的。你也不能做类似“迭代单元格,检测选定的单元格,并将它们添加到列表中以供以后处理,并查看其中哪个被选中”的事情。从广义上讲,当你尝试这样做时,你最终会处理最后的选择的一部分,就是这样。

有效的方法是使用 Selection.Paste 将剪贴板的内容粘贴到多个单元格中。但要做到这一点,您首先必须在文档中的某个位置插入所需的任何内容并将其复制到剪贴板中,然后将其粘贴到 Selection 中。这使过程变得复杂。

从编码的角度来看,也许更容易尝试通过在每个选定的单元格中插入“魔术文本”来“标记”每个单元格,找到那些单元格,然后插入您真正想要的内容。

例如,

Sub MultipleSelection1()
'
'
' insert an unlikely piece of text  ("magic text") somewhere in the document and copy it into the clipboard

Const MAGIC As String = "zmagic"
Dim c As Word.Cell
Dim r As Word.Range
Dim t As Integer
Set r = ActiveDocument.Characters.Last
r.Text = MAGIC
' exclude the paragraph/section marker
r.End = r.End - 1
' copy the text
r.Copy
' delete the inserted text
r.Delete
Set r = Nothing

' paste into all the selected cells
Selection.Paste

' iterate the cells containing the magic text

' If the selection is only ever in a single table, you *could* then use

For Each c In Selection.Tables(1).Range.Cells

' or if you don't mind iterating all the cells in the document you could loop through all the cells in each table.
' Unfortunately you can't just use "ActiveDocument.Range.Cells"

' NB sometimes better to count backwords, i.e. .count to 1.
'For t = 1 To ActiveDocument.Tables.Count
'For Each c In ActiveDocument.Tables(t).Range.Cells

  If Left(c.Range.Text, Len(MAGIC)) = MAGIC Then
    Set r = c.Range
    r.End = r.End - 1
    ' do whatever you want to this cell, e.g.
    With r
      .Style = ActiveDocument.Styles("Normal")
      .ParagraphFormat.Alignment = wdAlignParagraphRight
      .ParagraphFormat.SpaceBefore = 6
      .ParagraphFormat.SpaceAfter = 6

      .Font.Size = 14
      .Text = ""
      .InsertSymbol Font:="Wingdings", CharacterNumber:=168, Unicode:=True
    End With
    Set r = Nothing
  End If
Next

' Uncomment this if you are iterating all the tables
'Next
    
End Sub

或者您可以使用 Find 来遍历每个“魔法文本”,如下所示:

Sub MultipleSelection2()
'
'
' insert an unlikely piece of text  ("magic text") somewhere in the document and copy it into the clipboard

Const MAGIC As String = "zmagic"

Dim bFound As Boolean
Dim c As Word.Cell
Dim r As Word.Range
bFound = True
Set r = ActiveDocument.Characters.Last
r.Text = MAGIC
' exclude the paragraph/section marker
r.End = r.End - 1
' copy the text
r.Copy
' delete the inserted text
r.Delete
Set r = Nothing

' paste into all the selected cells
Selection.Paste

' iterate all instances of the magic text

Set r = ActiveDocument.Content
With r.Find
   .ClearFormatting
   Do While bFound
     .Text = MAGIC
     .Forward = True
     .Wrap = wdFindStop
     bFound = .Execute()
     If bFound Then
       ' do whatever you want to the found text
       With r
         .Style = ActiveDocument.Styles("Normal")
         .ParagraphFormat.Alignment = wdAlignParagraphRight
         .ParagraphFormat.SpaceBefore = 6
         .ParagraphFormat.SpaceAfter = 6

         .Font.Size = 14
         .Text = ""
         .InsertSymbol Font:="Wingdings", CharacterNumber:=168, Unicode:=True
       End With
    End If
  Loop
End With
Set r = Nothing
End Sub

相关内容