我对VBA世界来说几乎是全新的,到目前为止,这是一场艰苦的战斗。我现在拥有的是excel中的宏,它将获取命名范围并将它们复制到自己的工作表中(如果存在这些范围),并从那里提示用户从模板创建word文档并将其保存在名称输入下通过messagebox。一切正常。我似乎无法弄清楚的问题是将其中一个范围移动到Word中的表中并使其保持在同一页面上。我一直在陪审团操纵一些代码以使其与复制和粘贴一起使用,但我似乎无法使用Selecton将其与现有表合并。 PasteAppendTable因为它给我一个错误,说该操作不可用。我可以验证单元格是否在剪贴板中,如果我停止宏并手动粘贴它,它就可以工作。模板中有一个小的2 x 1表格,我用它来合并,然后展开。我将附上以下代码。
Sub Dailytable()
Selection.Tables(1).Cell(1, 1).Select
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.PasteAppendTable
Selection.Tables(1).Columns(2).Select
Selection.InsertColumnsRight
Selection.Tables(1).Cell(1, 3).Select
Selection.Text = "Comments"
Selection.Tables(1).Rows(1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = -603937025
Selection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
Dim RowNum As Variant
RowNum = Selection.Tables(1).Rows.Count
Selection.Tables(1).Cell(RowNum, 1).Select
Selection.InsertRowsBelow 1
Selection.Cells.Merge
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = -603937025
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Text = _
"Note to perform a visual inspection only. Report any oil or lubricant refills to your supervisor."
Selection.Tables(1).Rows.Select
Selection.Range.ParagraphFormat.KeepTogether = True
Selection.Range.ParagraphFormat.KeepWithNext = True
End Sub