Home > Back-end >  Getting error(Runtime Error "5097" and untime Error "4198") while copying data f
Getting error(Runtime Error "5097" and untime Error "4198") while copying data f

Time:07-18

i am trying to copy data from excel to word using macro but when i tried i am getting the below errors. i am using Microsoft office standard 2016 version. how can i solve this issue?

error: Runtime Error "5097": word has encountered a problem

Runtime Error "4198": Command Failed

and below is the code

Sub ExcelToWord()

Dim wordApp As Word.Application
Dim mydoc As Word.Document
Set wordApp = New Word.Application

wordApp.Visible = True
Set mydoc = wordApp.Documents.Add()
ThisWorkbook.Worksheets("sheet1").Range("A1:g20").Copy

mydoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False,_ 
WordFormatting:=False, RTF:=False    << getting error as runtime 
                                  error"5097" word encountered a 
                                  problem>>
mydoc.SaveAs "MyDoc.docx"
mydoc.Close
CutCopyMode = False
End Sub

CodePudding user response:

bro, try mydoc.Paragraphs(1).Range.PasteExcelTable False, True, False if doesn't help - maybe it's something with the system. Excel sometimes creates unexplainable errors, cured by system reboot.

CodePudding user response:

Sub ExcelToWord()

Dim wordApp As New Word.Application
Dim mydoc As Word.Document
  
Set mydoc = wordApp.Documents.Add
wordApp.Visible = True
    
ThisWorkbook.Worksheets("Sheet1").Range("A1:G20").Copy

mydoc.Paragraphs(1).Range.PasteExcelTable False, True, False
mydoc.SaveAs "MyDoc.docx"
mydoc.Close
CutCopyMode = False
End Sub

This works - seems to be that with .Add() you added a blank array of documents.

  • Related