'打開后焦點發生了轉移,activedocument已經指向剛打開的文件了
'改為
Sub Macro1()
Dim myDoc
'On Error Resume Next
With ActiveDocument.Tables(1).Cell(Row:=1, Column:=3).Range
Set myDoc = Word.Application.Documents.Open("E:\1.docx")
.Delete
.InsertAfter Text:=myDoc.Tables(2).Cell(Row:=1, Column:=2)
End With
End Sub
'(
InsertAfter Text:=myDoc.Tables(2).Cell(Row:=1, Column:=2)這行代碼中,你確定表2存在嗎?我測試時只建一個表,所以改為Tables(1).
)
'也可以這樣控制焦點
Sub Macro1()
Dim myDoc
'On Error Resume Next
Set myDoc = Word.Application.Documents.Open("E:\1.docx", , , , , , , , , , , vbHide)
With ActiveDocument.Tables(1).Cell(Row:=1, Column:=3).Range
.Delete
.InsertAfter Text:=myDoc.Tables(2).Cell(Row:=1, Column:=2)
End With
End Sub