插入的单词 Table 不显示边框

Inserted Word Table not showing Borders

我可以插入 table 但 table 的边框不可见。您可以看到创建的文档。为了允许其他人 运行 这个脚本,我必须使用延迟绑定,我怀疑这可能是原因。

我的代码在这里:

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

不显示 table 边框的问题是在使用后期绑定时,您必须使用数值进行设置。

我还做了一些其他调整,它们对您遇到的问题没有影响,但它们是更好的做法。所有对象都已声明,我添加了一个测试以查看 Word 应用程序是否已经 运行ning。在 Office 应用程序的某些版本中,当您执行 CreateObject 并且应用程序已经存在时,应用程序的多个实例可能会加载到内存中。