要使用vba控制,比如生成一个表:
Dim app As New MSWord.Application
try
Dim doc = app.Documents.add
Dim dt As DataTable = DataTables("表A")
doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt.DataCols.Count)
With app.Selection.Tables(1)
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
For Each dc As DataCol In dt.DataCols
app.Selection.TypeText(Text:=dc.Name)
app.Selection.MoveRight(Unit:=12)
Next
For Each dr As DataRow In dt.DataRows
For Each dc As DataCol In dt.DataCols
app.Selection.TypeText(Text:=dr(dc.Name))
app.Selection.MoveRight(Unit:=12)
Next
Next
app.Visible = True
catch ex As exception
msgbox(ex.message)
app.Quit
finally
End try
-------------------------
控制插入WORD文档中的位置
1、先find然后选中;
2、然后建表
Dim app As New MSWord.Application
try
Dim doc = app.Documents.Open("d:\test.doc")
If app.ActiveWindow.Selection.Find.Execute("test") = False Then
'插入表格
End If
app.Visible = True
catch ex As exception
msgbox(ex.message)
app.Quit
finally
End try