生成表(行和单元格)

时间:2013-10-18 17:30:50

标签: vbscript ms-word

我有VB.net代码,但我需要将其转换为VBScript。我需要打开一个word文档计数表,如果有一个Word(例如Tablein),我需要用表替换它。但是必须在提供数据时构建表格。

VB.net代码(完整代码):

dim oRow As Word.Row,oTable As Word.Table
oRow = oTable.Rows(1)
oRow.Cells(1).Range.Text = ""
If PullRow.Item("TYPE").ToString = "Traffic" Then
oRow.Cells(2).Range.Text = "T"
ElseIf PullRow.Item("TYPE").ToString = "Data" Then
oRow.Cells(2).Range.Text = "D"
End If
oRow.Cells(3).Range.Text = "C"

VBScript(直到我可以写):

Dim intNoOfRows

Dim intNoOfColumns

Dim objWord

Dim objDoc

Dim objRange

Dim objTable
dim strword

intNoOfRows = 5

intNoOfColumns = 3

Set objWord = CreateObject("Word.Application")

objWord.Visible = True    

Set objDoc = objWord.Documents.open("new.docx")

g= objWord.ActiveDocument.Tables.count

Set objRange = objDoc.Range

Set objTable = objDoc.Tables(1)
objTable.Borders.Enable = True   

With objWord.Selection
  .Find.Text = "pen"
  .Find.Forward = True
  .Find.MatchWholeWord = True
  Do
    found = .Find.Execute 

    If found Then objDoc.Tables.Add objRange, 2,2
  Loop While found

End With

1 个答案:

答案 0 :(得分:0)

将您创建的表格分配给变量:

If found Then Set tbl = objDoc.Tables.Add(objRange, 2, 2)

然后使用该变量为表格单元格指定值:

tbl.Cell(1, 1).Range.Text = "foo"
tbl.Cell(1, 2).Range.Text = 23
tbl.Cell(2, 1).Range.Text = "bar"
tbl.Cell(2, 2).Range.Text = 42