Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VBA在word上创建表格并添加边框_Vba_Ms Word - Fatal编程技术网

如何使用VBA在word上创建表格并添加边框

如何使用VBA在word上创建表格并添加边框,vba,ms-word,Vba,Ms Word,我只想在单击命令按钮时创建一个表。该表应该有2行2列 每当我添加另一个表时,它应该在前一个下添加一个新表 这是我目前的代码: Private Sub CommandButton1_Click() Set MyRange = ActiveDocument.Content MyRange.Collapse Direction:=wdCollapseEnd ActiveDocument.Tables.Add Range:=MyRange, NumRows:=2, NumColumns:=2 End

我只想在单击命令按钮时创建一个表。该表应该有2行2列

每当我添加另一个表时,它应该在前一个下添加一个新表

这是我目前的代码:

Private Sub CommandButton1_Click()

Set MyRange = ActiveDocument.Content
MyRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=MyRange, NumRows:=2, NumColumns:=2

End Sub
这段代码的问题是,当我添加它时,我看不到任何边框和线条

此外,当我多次单击命令按钮时,它只是向现有表中添加更多行,而不是在以前的表下创建其他表

我试图在表之间添加一个换行符来修复这种情况,但没有成功。我用过:

ActiveDocument.Range.Text = "Foo" & Chr(11) & "Bar"
感谢您的帮助

试试:

Sub Demo()
With ActiveDocument.Range
  .InsertAfter vbCr
  .Tables.Add Range:=.Characters.Last, NumRows:=2, NumColumns:=2
  .Tables(.Tables.Count).Borders.Enable = True
End With
End Sub

你想要边界吗?还是要网格线(不打印)?