运行时错误“4198” [英] run-time error "4198"

查看:1219
本文介绍了运行时错误“4198”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了来自VBA的错误,该错误之前运行正常并且不知道在哪里修改并触发了错误4198。


调试函数指示错误来自底部第六row:  appWD.ActiveDocument.SaveAs2(TWD_SLoc&" \"& ThisWorkbook.Sheets(" TWD")。Range(" D" i + 1)&" .docx" )。


此宏用于将Excel数据合并到word模板中以创建单个单词报告。所有word文件都是在这个宏下成功创建的,但是在结束之前就已经卡住了。真正的问题似乎是vba在所有数据
运行后继续运行。 


希望获得一些好评!我很绝望...

 Sub Report_TWD()

Dim TWD_MLoc As String,TWD_DLoc As String,TWD_SLoc As String

TWD_MLoc = ThisWorkbook.Sheets("õ|æ")。范围(" B5")。值
TWD_DLoc = ThisWorkbook.Sheets("õ|æ")。范围( "C5"。值。
TWD_SLoc = ThisWorkbook.Sheets("õ|æ")。范围(" D5")。值

Dim appWD As Word.Application
设置appWD = CreateObject(" Word.Application")
appWD.Visible = True
Dim TWDWD As Word.Document
设置TWDWD = appWD.Documents.Open(TWD_MLoc)
TWDWD.Activate
TWDWD.MailMerge.OpenDataSource名称:= TWD_DLoc,SQLStatement:=" SELECT * FROM`TWD $`"

Dim x As Long
Dim i As Long
Dim v As Long,w As Long
Dim stMsg As String

TWDWD。激活
使用TWDWD.MailMerge
。目的= wdSendToNewDocument
.SuppressBlankLines = True
使用.DataSource
.ActiveRecord = wdLastRecord
x = .ActiveRecord
.ActiveRecord = wdFirstRecord


结束对于i = 1到x
.DataSource.FirstRecord = i
.DataSource.LastRecord = i
。执行
appWD.ActiveDocument.SaveAs2(TWD_SLoc&" \"& ThisWorkbook.Sheets(" TWD")。Range(" D" i + 1)&" .docx" ;)
appWD.ActiveDocument.Close wdDoNotSaveChanges
下一个i
结束
TWDWD.Close wdDoNotSaveChanges
End Sub

解决方案

Allison Wong,


错误发生时我的价值是多少?您可以分享一些简单的工作簿和word文档,以便我们可以尝试重现您的问题吗?您可以通过OneDrive共享文件,然后在此处输入链接。感谢您的理解。


最好的问候,


Terry





I'm encountering error from VBA which runs fine before and don't know where was revised and triggered error 4198.

The debug function indicated error from the bottom sixth row: appWD.ActiveDocument.SaveAs2 (TWD_SLoc & "\" & ThisWorkbook.Sheets("TWD").Range("D" & i + 1) & ".docx").

This macro is meant to merge excel data into word template to create individual word report. All word files were successfully created under this macro, however stuck just before ended. The true problem seems to be that vba kept on running after all data were run through. 

Hoping for some great opinions! I'm desperate...

Sub Report_TWD()

Dim TWD_MLoc As String, TWD_DLoc As String, TWD_SLoc As String

TWD_MLoc = ThisWorkbook.Sheets("°õ¦æ").Range("B5").Value
TWD_DLoc = ThisWorkbook.Sheets("°õ¦æ").Range("C5").Value
TWD_SLoc = ThisWorkbook.Sheets("°õ¦æ").Range("D5").Value

Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
Dim TWDWD As Word.Document
Set TWDWD = appWD.Documents.Open(TWD_MLoc)
TWDWD.Activate
TWDWD.MailMerge.OpenDataSource Name:=TWD_DLoc, SQLStatement:="SELECT *  FROM `TWD$`"

Dim x As Long
Dim i As Long
Dim v As Long, w As Long
Dim stMsg As String

TWDWD.Activate
With TWDWD.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.ActiveRecord = wdLastRecord
x = .ActiveRecord
.ActiveRecord = wdFirstRecord
End With

For i = 1 To x
.DataSource.FirstRecord = i
.DataSource.LastRecord = i
.Execute
appWD.ActiveDocument.SaveAs2 (TWD_SLoc & "\" & ThisWorkbook.Sheets("TWD").Range("D" & i + 1) & ".docx")
appWD.ActiveDocument.Close wdDoNotSaveChanges
Next i
End With
TWDWD.Close wdDoNotSaveChanges
End Sub

解决方案

Hi Allison Wong,

What's the value of I while the error occurs? Could you share some simply workbooks and word documents so we could try to reproduce your issue? You could share files via OneDrive and then put link here. Thanks for understanding.

Best Regards,

Terry



这篇关于运行时错误“4198”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆