将数据从Excel文件复制到打开的Word文件

将数据从Excel文件复制到打开的Word文件,excel,vba,ms-word,Excel,Vba,Ms Word,我希望我没有监督类似的问题 我使用下面的代码将数据从一个excel文件复制到一个特定的word文件。 这项工作没有任何大问题 我唯一想改进的是,word文件必须关闭 有没有办法使用此方法将过去的数据保存到打开的word文件中 Public Function Export() Dim appWord As Object Dim wdDoc As Object . . . Set appWord = CreateObject("Word.Application") Set wd

我希望我没有监督类似的问题

我使用下面的代码将数据从一个excel文件复制到一个特定的word文件。 这项工作没有任何大问题

我唯一想改进的是,word文件必须关闭

有没有办法使用此方法将过去的数据保存到打开的word文件中

Public Function Export()
Dim appWord As Object
Dim wdDoc As Object
.
.
.
Set appWord = CreateObject("Word.Application") 
Set wdDoc = appWord.Documents.Open(wrdfile)
With wdDoc
.Bookmarks("Mark1").Range.Text = ActiveSheet.Range("X24").Value
.Bookmarks("Mark2").Range.Text = ActiveSheet.Range("H23").Value
.
.
.
End with
End function
尝试添加(…),但这只会打开word文件的新实例

谢谢你的帮助 莫里斯

例如:

Sub Demo()
Application.ScreenUpdating = True
Dim wdApp As Object, wdDoc As Object, StrDocNm As String
Dim bStrt As Boolean, bFound As Boolean
'Check whether the document exists
StrDocNm = "C:\Users\" & Environ("Username") & "\Documents\Document Name.doc"
If Dir(StrDocNm) = "" Then
  MsgBox "Cannot find the designated document: " & StrDocNm, vbExclamation
  Exit Sub
End If
' Test whether Word is already running.
On Error Resume Next
bStrt = False ' Flag to record if we start Word, so we can close it later.
Set wdApp = GetObject(, "Word.Application")
'Start Word if it isn't running
If wdApp Is Nothing Then
  Set wdApp = CreateObject("Word.Application")
  If wdApp Is Nothing Then
    MsgBox "Can't start Word.", vbExclamation
    Exit Sub
  End If
  ' Record that we've started Word, so we can terminate it later.
  bStrt = True
End If
On Error GoTo 0
'Check if the document is open.
bFound = False
With wdApp
  .Visible = True
  For Each wdDoc In .Documents
    If wdDoc.FullName = StrDocNm Then ' We already have it open
      bFound = True
      Exit For
    End If
  Next
  ' If not open by the current user.
  If bFound = False Then
    ' Check if another user has it open.
    If IsFileLocked(StrDocNm) = True Then
      ' Report and exit if true
      MsgBox "The Word document is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
      If bStrt = True Then .Quit
      Exit Sub
    End If
    ' The file is available, so open it.
    Set wdDoc = .Documents.Open(FileName:=StrDocNm)
    If wdDoc Is Nothing Then
      MsgBox "Cannot open:" & vbCr & StrDocNm, vbExclamation
      If bStrt = True Then .Quit
      Exit Sub
    End If
  End If
  With wdDoc
    'Only now can we can process the document!!!
    .Save
    'Close the document if we opened it
    If bFound = False Then .Close
  End With
  If bStrt = True Then .Quit
End With
End Sub

Function IsFileLocked(strFileName As String) As Boolean
  On Error Resume Next
  Open strFileName For Binary Access Read Write Lock Read Write As #1
  Close #1
  IsFileLocked = Err.Number
  Err.Clear
End Function

使用
GetObject
而不是
CreateObject
链接到现有的Word实例?感谢您的回复。我将CreateObject更改为GetObject,但这给了我一个语法error@Morris它应该是
GetObject(,“Word.Application”)
。请看这里的文件:谢谢你的回复。我让它工作,但仍然有一些问题。但下面的答案满足了我的需要;)哇,这太棒了。我不得不改变一点(或者只是删除一些东西),但现在它正是我所需要的!非常感谢你!