我可以插入表格,但表格边框不可见。您可以看到创建的文档。为了允许其他人运行此脚本,我必须使用后期绑定(bind),我怀疑这可能是原因。
我的代码在这里:
Sub Button1_Click()
Dim objWord As Object
Dim objDoc
Dim objSelection
Dim i As Long
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.TypeText ("Insert table after this text")
Set myRange = objDoc.Content
myRange.Collapse Direction:=wdCollapseEnd
objDoc.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4
Set myTable = objDoc.Tables(1)
With myTable.Borders
.Enable = True
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
.InsideColor = wdColorBlack
.OutsideColor = wdColorBlack
End With
End Sub
最佳答案
这是您修改过的代码,在我的测试中,当我从 Excel 运行它时它可以工作。
Sub Button1_Click()
Dim objWord As Object
Dim objDoc As Object
Dim objSelection As Object
Dim i As Long
Dim myRange As Object
Dim myTable As Object
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
On Error GoTo 0
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection
objSelection.TypeText ("Insert table after this text")
Set myRange = objDoc.Content
myRange.Collapse Direction:=wdCollapseEnd
Set myTable = objDoc.Tables.Add(Range:=myRange, NumRows:=3, NumColumns:=4)
With myTable.Borders
.Enable = True
.InsideLineStyle = 1
.OutsideLineStyle = 7
.InsideColor = 0
.OutsideColor = 0
End With
End Sub
表格边框不显示的问题是,在使用后期绑定(bind)时,您必须使用数值进行设置。我还进行了一些其他调整,它们对您遇到的问题没有影响,但它们是更好的做法。所有对象都已声明,我添加了一个测试以查看 Word 应用程序是否已在运行。在 Office 应用程序的某些版本中,当您执行 CreateObject 并且应用程序已经存在时,应用程序的多个实例可能会加载到内存中。
关于excel - 插入的字表不显示边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65362354/