VBA-运行时错误4605。此方法或属性不可用,因为文档窗口处于不活动状态

时间:2019-01-03 03:52:32

标签: vba outlook ms-word word-vba

我正在尝试使用VBA(在Word中)基于打开包含宏的Word文档时加载的用户表单中提供的数据在Outlook中创建约会。一切似乎都正常,但是当宏执行粘贴功能时,我最终遇到了一些运行时错误。

Dim OutApp As Object
Dim OutMail As Object
Dim OutInsp As Outlook.Inspector
Dim WdApp As Word.Application
Dim OutDoc As Word.Document
Dim WdSel As Word.Selection

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(1)

If request_Form.ComboBox3.Value = "****" Then
With ActiveDocument.Bookmarks("recipient_Name")
    .Range.Text = "Uwe"
End With
With ActiveDocument.Bookmarks("applicant_Name")
    .Range.Text = Replace(request_Form.TextBox1.Value, "_", ", ")
End With
With OutMail
    .MeetingStatus = olMeeting
    .RequiredAttendees = "@live.in"
    .Subject = request_Form.TextBox1.Value & " Work From Home"
    .Location = "Home"
    .BusyStatus = olFree
    .ReminderSet = False
    If request_Form.CheckBox1.Value = False Then
        .Start = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox1.Value)
        .End = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox2.Value)
    Else
        .AllDayEvent = True
    End If
    .Display
End With


Set OutInsp = OutMail.GetInspector
Set OutDoc = OutInsp.WordEditor
Set WdApp = OutDoc.Application
Set WdSel = WdApp.Windows(1).Selection

Documents("Home Office Request Form.docm").Range.Copy

With OutMail
    WdSel.PasteAndFormat Type:=wdFormatOriginalFormatting
End With

save_file
 MsgBox "A copy of this file has been stored in your desktop for your 
 reference.", vbOKOnly + vbInformation, "Status Information"

End Sub

“宏功能-程序根据用户表格中填充的数据创建约会,并将整个内容复制到word文档中并粘贴在约会正文中”。当执行这段代码“ wdsel.pasteandformat”时,它会导致运行时错误,但有趣的是,该错误似乎很少发生,因此我很难理解这里出了什么问题让我们说有70%的时间可以正常工作很好,可能还有30%的时间没有。

如果有人可以帮助我了解上述代码的错误之处,我将非常高兴。

感谢您的帮助。

关于, 拉胡尔

1 个答案:

答案 0 :(得分:0)

因此,我们对您的问题有一个答案。在这里。

可能是,当您打开Outlook时,将激活Outlook窗口。这时可能无法访问非活动Word窗口中的选择对象。若要解决此问题,请在打开Outlook之前从Word中获取所需的一切。其中包括将文本复制到内存中,然后将其粘贴到Outlook中。