|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
可将表2中的“序号”拆分更妙!(有利于您更好的访问表行)
Excel中的代码
Private Sub CommandButton1_Click()
Dim wdDoc As Object
Dim wdTable As Object
Dim wdRow As Object
Dim strFullname As String
strFullname = ThisWorkbook.Path & "\表格.doc"
If Len(Dir(strFullname, vbDirectory)) = 0 Then
MsgBox "Excel未找到" & strFullname, vbExclamation
Exit Sub
End If
Set wdDoc = GetObject(strFullname)
With wdDoc
''' wdDoc.Application.Visible = True可用于调试
'============================追加表格==================================
Set wdTable = .Tables(1)
wdTable.Cell(1, 2).Range = Date
Set wdTable = .Tables(2)
Set wdRow = wdTable.Rows.Last
wdRow.Select
.ActiveWindow.Selection.InsertRowsBelow 10
strFullname = Replace(strFullname, "\表格.doc", "\新表格.doc")
.SaveAs Filename:=strFullname
.Close
Set wdDoc = Nothing
End With
End Sub
修改后的表格见附件,此代码中应用了Selection对象,也可以使用Rows.Add方法。 |
|