gpt4 book ai didi

vba - 运行时错误 91 : Object variable or With block variable not set

转载 作者:行者123 更新时间:2023-12-03 09:04:19 25 4
gpt4 key购买 nike

我有 2 个带有邮件合并列表的单独 word 文档。我有一个带有 2 张纸的 excel 工作簿。根据工作表名称,如果工作表不为空,我需要将邮件合并发送到相应的 Word 文档。

当我尝试执行此代码时,它会运行到第一个文档并在第二个文档处停止并出现错误 Run-time Error 91 : Object variable or With block variable not set
我不确定是什么导致了这个错误(如果是 Dim 变量或 With block )。如果有人能帮助我纠正这个错误,将不胜感激。

Sub Generate_Certificate() 

Dim wd As Object
Dim wdoc_reg As Object
Dim wdoc_occ As Object
Dim strWbName_reg As String
Dim strWbName_occ As String


Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdFormLetters1 = 0, wdOpenFormatAuto1 = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16
Const wdSendToNewDocument1 = 0, wdDefaultFirstRecord1 = 1, wdDefaultLastRecord1 = -16


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


For Each Sheet In ActiveWorkbook.Sheets

'Generate report using "Mailmerge" if any data available for Mailmerge1
If Sheet.Name Like "Sheet1" And IsEmpty(ThisWorkbook.Sheets("Sheet1").Range("A2").Value) = False Then
Set wdoc_reg = wd.Documents.Open("C:\Mailmerge1.docx")


strWbName_reg = ThisWorkbook.Path & "\" & ThisWorkbook.Name


wdoc_reg.MailMerge.MainDocumentType = wdFormLetters


wdoc_reg.MailMerge.OpenDataSource _
Name:=strWbName_reg, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWbName_reg & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Sheet1$`"

With wdoc_reg.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With


wd.Visible = True
wdoc_reg.Close SaveChanges:=False


Set wdoc_reg = Nothing
Set wd = Nothing
End If


'Generate report using "Mailmerge" if any data available for Mailmerge2
If Sheet.Name Like "Sheet2" And IsEmpty(ThisWorkbook.Sheets("Sheet2").Range("A2").Value) = False Then
Set wdoc_occ = wd.Documents.Open("C:\Mailmerge2.docx")


strWbName_occ = ThisWorkbook.Path & "\" & ThisWorkbook.Name


wdoc_occ.MailMerge.MainDocumentType = wdFormLetters1


wdoc_occ.MailMerge.OpenDataSource _
Name:=strWbName_Occ, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto1, _
Connection:="Data Source=" & strWbName_occ & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Sheet2$`"


With wdoc_occ.MailMerge
.Destination = wdSendToNewDocument1
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord1
.LastRecord = wdDefaultLastRecord1
End With
.Execute Pause:=False
End With


wd.Visible = True
wdoc_occ.Close SaveChanges:=False


Set wdoc_Occ = Nothing
Set wd = Nothing
End If


Next


End Sub

最佳答案

正如蒂姆威廉姆斯在问题评论中所述。

You have Set wd = Nothing inside your loop, which will clear your reference to Word after the first sheet. Move that to just before the End Sub

关于vba - 运行时错误 91 : Object variable or With block variable not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com