Open Word template, paste Excel data in and save

前端 未结 2 1713
慢半拍i
慢半拍i 2021-01-25 20:27

I\'ve got an Excel data range I need to be able to paste into a Word document template and save automatically using a macro from Excel.

Currently when it is run, it tell

2条回答
  •  离开以前
    2021-01-25 20:41

    First, you are getting a lock error because the instance of word is still running in the background and locking the file you are trying to re-open. You can verify that with the task manager. To avoid this error, you can either:

    • kill the WinWord.exe process in the task manager
    • use wdApp.documents.Add instead of .open

    Or (best)

    • Write a function that either gets the word application running in the background or if none, creates a new one. This is the best option IMO, because you will not have the risk to have many invisible Word processes running in the background.

      Private Function GetWordApp() As Word.Application
      
          On Error Resume Next
      
          Set GetWordApp = GetObject(, "Word.Application")
      
          If GetWordApp Is Nothing Then Set GetWordApp = New Word.Application
      
      End Function
      

提交回复
热议问题