Excel 邮件合并运行时错误 4198

发布于 2024-12-14 06:42:11 字数 2357 浏览 1 评论 0原文

stackoverflow 上之前有一个问题:
“是否可以从 Excel 宏启动邮件合并(通过单击工作表上的按钮) 我有包含数据的 Excel 工作表,想要将其导出到新的 Word 文档。” dendarii 772 于 2009 年 9 月 29 日 12:39 回答了这一问题。

我们也有这种需求,因为我们希望简化计算机技能有限的志愿者的生活。

不幸的是,使用合适的路径修改后的 dendarii 代码在 .OpenDataSource 语句处退出并出现运行时错误 4198。

Cindy Meister 在 Microsoft Office for Developer's Form 上撰写文章> ..>Word 2010 VBA 建议 4198 错误可能是由于同步问题造成的。我放弃了,但没有成功。

Andrew Poulson 在撰写有关 Excel 先生的文章时鼓励贡献者 Snecz 将他的 VBA 邮件合并 .OpenDataSource 语句与 Word 宏记录进行比较。贡献者有何看法?我的 .OpenDataSource 行似乎是标准的。我的Excel 数据源文件有一行标题,后跟两行数据元素。

我们有 Office 2010。

如果有人知道一般诊断程序,我将非常感谢任何有关如何修复 4198 的建议。我们是两个人为之工作 一个慈善组织。欢迎任何帮助!

 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  

There was a previous question on stackoverflow:
"Is it possible to start MAIL MERGE from excel macro (by clicking a button on sheet)
I have excel sheet with data and want to export it to new word doc."
This was answered Sep 29 '09 at 12:39 by dendarii 772.

We had this need too because we wanted to simplify life for volunteers with limited computer skills.

Unfortunately dendarii's code, modified with a suitable path, exits with run time error 4198 at the .OpenDataSource statement.

Cindy Meister writing on Microsoft Office for Developer's Form> ..>Word 2010 VBA suggested 4198 errors could be due to synchronicity problems. I gave it go with no success.

Andrew Poulson writing on Mr Excel encourages a contributor Snecz to compare his VBA mail merge .OpenDataSource statement against a Word macro recording. What do contributors think? My .OpenDataSource line seems standard. My Excel
datasource file has a line of headers followed by two lines of data elements.

We have Office 2010.

I would be very grateful for any suggestions as to how to fix 4198 and if anyone knows of general diagnostic procedures. We are two people working for
a charitable organisation. Any help welcome!

 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

There was a previous question on stackoverflow:
"Is it possible to start MAIL MERGE from excel macro (by clicking a button on sheet)
I have excel sheet with data and want to export it to new word doc."
This was answered Sep 29 '09 at 12:39 by dendarii 772.

We had this need too because we wanted to simplify life for volunteers with limited computer skills.

Unfortunately dendarii's code, modified with a suitable path, exits with run time error 4198 at the .OpenDataSource statement.

Cindy Meister writing on Microsoft Office for Developer's Form> ..>Word 2010 VBA suggested 4198 errors could be due to synchronicity problems. I gave it go with no success.

Andrew Poulson writing on Mr Excel encourages a contributor Snecz to compare his VBA mail merge .OpenDataSource statement against a Word macro recording. What do contributors think? My .OpenDataSource line seems standard. My Excel
datasource file has a line of headers followed by two lines of data elements.

We have Office 2010.

I would be very grateful for any suggestions as to how to fix 4198 and if anyone knows of general diagnostic procedures. We are two people working for
a charitable organisation. Any help welcome!

"   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-12-21 06:42:11

您有 Word 对象库的参考集吗?如果不是,那么宏将会失败,因为您正在使用该库中找到的特定项目(例如 wdOpenFormatAuto)。

要添加对库的引用,请在 Excel VBA 编辑器中单击“工具”>“引用”,然后向下滚动直至找到“Microsoft Word 对象库”,单击左侧的框“选中它”,然后单击“确定”。将有一个与之关联的版本号...可能是 14.0,因为您运行的是 Office 2010。

添加该引用,这应该可以解决您的问题。

如果您已经拥有该套件,请告诉我们。

Do you have a reference set to the Word object library? If not, then the macro will fail because you are using specific items found in that library ( like wdOpenFormatAuto).

To add a reference to the library, in the excel VBA editor click on Tools>References and scroll down until you find "Micrsoft Word Object Library", click the box to the left to 'check it' and click 'OK'. There will be a version number associated with it... probably 14.0 since you are running Office 2010.

Add that reference and that should fix your problem.

If you already have that set, let us know.

酸甜透明夹心 2024-12-21 06:42:11

在调用 OpenDataSource 之前人为延迟以使 Open 方法有机会赶上如何?这非常丑陋,可能需要清理一下以匹配 VBA 语法,但这里是:

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

您还可以测试日期并在继续之前让给定的时间过去(即 2 秒)(这更像是 VB.Net)代码比什么都重要):

Dim dtDate As Date = Date.Now

Do While Date.Now.Subtract(dtDate).TotalSeconds < 2
  DoEvents()
Loop

How about putting an artificial delay in just before the call to OpenDataSource to give the Open method a chance to catch up? This is pretty ugly and may need to be cleaned up a bit to match VBA syntax, but here goes:

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

You could also test dates and let a given amount of time pass (i.e. 2 seconds) before carrying on (this is more VB.Net code than anything):

Dim dtDate As Date = Date.Now

Do While Date.Now.Subtract(dtDate).TotalSeconds < 2
  DoEvents()
Loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文