Merge Mail Merge 4198 Runtime Error

In stackoverflow there was a previous question:
β€œIs it possible to run MAIL MERGE from an excel macro (by clicking the button on the sheet) I have data sheets and want to export them to the new word doc.” The answer was dendarii 772.

We also had this need because we wanted to make life easier for volunteers with limited computer skills.

Unfortunately, the modified dendarii code exits with a 4198 runtime error in the .OpenDataSource statement.

Cindy Meister writes to Microsoft Office for the developer form> ..> Word 2010 VBA suggested that 4198 errors might be caused by synchronization issues. I gave it without success.

Mr Poulson, writing in Mr Excel, recommends that Snecz contributor compare his merging of VBA letters. OpenDataSource operation against writing a Word macro. What do the participants think? My .OpenDataSource line seems to be standard. My Excel datasource file has a title bar followed by two rows of data items.

We have Office 2010.

I would really appreciate any suggestions on how to fix 4198, and if anyone knows about general diagnostic procedures. We work for a two-person charity organization. Any help is appreciated!

Sub RunMerge()   Dim wd As Object Dim wdocSource As Object    Dim strWorkbookName As String    On Error Resume Next  Set wd = GetObject(, "Word.Application")  If wd Is Nothing Then  Set wd = CreateObject("Word.Application")  End If  On Error GoTo 0    Set wdocSource = wd.Documents.Open("C:\Users\george\Desktop\VBA Project\Mergeletter.docx")    strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name    wdocSource.MailMerge.MainDocumentType = wdFormLetters    wdocSource.MailMerge.OpenDataSource _   Name:=strWorkbookName, _   AddToRecentFiles:=False, _   Revert:=False, _   Format:=wdOpenFormatAuto, _    Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _   SQLStatement:="SELECT * FROM `Sheet1$`"  With wdocSource.MailMerge .Destination=wdSendToNewDocument   .SuppressBlankLines = True   With .DataSource   .FirstRecord = wdDefaultFirstRecord   .LastRecord = wdDefaultLastRecord   End With   .Execute Pause:=False   End With   wd.Visible = True  wdocSource.Close SaveChanges:=False     Set wdocSource = Nothing   Set wd = Nothing  End Sub 
+4
source share
2 answers

Do you have a link to the Word object library? If not, the macro will fail because you are using certain elements found in this library (for example, wdOpenFormatAuto ).

To add a link to the library, in excel VBA, click Tools> Links and scroll down until you find the Microsoft Micrsoft Word Object Library, click on the box on the left to check, and click " OK, "Next to it will be the version number ... probably 14.0 since you are using Office 2010.

Add this link and this should fix your problem.

If you already have this kit, let us know.

+3
source

How about setting an artificial delay before calling OpenDataSource to give the Open method an opportunity to catch up? This is pretty ugly and might need to be cleaned up a bit to fit the VBA syntax, but here goes:

 For nI As Integer = 0 To 2500 DoEvents() Next 

You can also test the dates and skip a given period of time (for example, 2 seconds) (this is more VB.Net code than anything):

 Dim dtDate As Date = Date.Now Do While Date.Now.Subtract(dtDate).TotalSeconds < 2 DoEvents() Loop 
+1
source

Source: https://habr.com/ru/post/1379799/


All Articles