添加表格时“无法删除范围”

时间:2017-09-06 13:16:52

标签: vba ms-word

我有一个在Word文档中创建表的功能。

在WordManager中首先运行Main会分配objWordobjDoc。在WordFormating中运行FnAddTableToWordDocument在第一次运行时起作用,但在第二次运行时每次都失败。

我得到的错误如下:

  

运行时错误6028
  范围无法删除

在线:

objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns

两个模块:

模块#1 - WordManager:

Module WordManager    
Public objWord As Word.Application
Public objDoc As Word.Document

Sub Main() ' This is to be replaced by a call from the actual CA tool.
    Call initWordManager("[string path]", "test2.doc")
End Sub

Sub initWordManager(Path, Name)
    sFilePath = Path
    sFileName = Name

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set objDoc = objWord.Documents.Add
End Sub

模块#2 - WordFormating:

Function FnAddTableToWordDocument()

   Dim intNoOfRows

   Dim intNoOfColumns

   Dim objRange

   Dim objTable

 intNoOfRows = 5

 intNoOfColumns = 3


  objWord.Visible = True

  Set objRange = objDoc.Range

  objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns

  Set objTable = objDoc.Tables(1)

    objTable.Borders.Enable = True

  For i = 1 To intNoOfRows

     For j = 1 To intNoOfColumns

 objTable.Cell(i, j).Range.Text = "Sumit_" & i & j

     Next

  Next

End Function

1 个答案:

答案 0 :(得分:0)

设定更具体的范围解决了这个问题。

设置objRange = objDoc.Range(开始:= 0,结束:= 0)

不提供多个表,但至少不会导致错误。