Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Vba运行时错误:91_Excel_Vba_Outlook_Ms Word_Runtime Error - Fatal编程技术网

Excel Vba运行时错误:91

Excel Vba运行时错误:91,excel,vba,outlook,ms-word,runtime-error,Excel,Vba,Outlook,Ms Word,Runtime Error,我正在尝试使用Outlook向Excel工作表中列:A中的每个电子邮件地址发送电子邮件,并在正文中插入Word文档。我已经编写了以下代码,但它给出了运行时错误91。我正在使用Office 2013 Public Sub Create_Outlook_Email() Dim OutApp As Object, OutMail As Object, OutWordEditor As Object Dim WordDoc As Object Dim wordfile As String Dim rn

我正在尝试使用Outlook向Excel工作表中
列:A
中的每个电子邮件地址发送电子邮件,并在正文中插入Word文档。我已经编写了以下代码,但它给出了运行时错误91。我正在使用Office 2013

Public Sub Create_Outlook_Email()

Dim OutApp As Object, OutMail As Object, OutWordEditor As Object
Dim WordDoc As Object
Dim wordfile As String
Dim rng As Range
Dim row As Range
Dim cell As Range

 'Create new Outlook email
Set rng = Range("a2:a50")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set OutWordEditor = OutMail.GetInspector.WordEditor

For Each row In rng.Rows
    For Each cell In row.Cells

        With OutMail
            .To = cell.Value
            .Subject = "Your emails are moving on " & Range("e2").Value & "- don't get left behind     "

            wordfile = Application.GetOpenFilename(Title:="Select MS Word file", MultiSelect:=False)
            Set WordDoc = GetObject(wordfile)
            WordDoc.Content.Copy
            WordDoc.Close
            OutWordEditor.Content.Paste

            'See if Outlook is using Word to edit messages

            .display
            .send
        End With

        Set OutApp = Nothing
        Set OutMail = Nothing

        Set OutWordEditor = Nothing
        Set WordDoc = Nothing

    Next cell
Next row  

End Sub

如果您提供了有关发生错误的行的信息,则会更容易提供帮助。例如,您有以下错误,很可能是问题的根源:

您在循环中执行此操作,但在下一次迭代中不设置
outordeditor
变量。因此,在第二次迭代中,在
outordeditor.Content.Paste
行,您会得到“objectnotset”

我建议的解决方案是将这些语句移动到循环内的

Set OutMail = OutApp.CreateItem(0)
Set OutWordEditor = OutMail.GetInspector.WordEditor
此外,您不需要两个嵌套循环,只需要一个:

通过以上所有操作,您的(单循环)将如下所示:

For Each cell In rng.Cells
    If Len(Trim(cell.Value)) = 0 Then Exit For ' <-- to stop at emty cell
    Set OutMail = OutApp.CreateItem(0)
    Set OutWordEditor = OutMail.GetInspector.WordEditor
    ... 'Rest of the loop
Next
对于rng.Cells中的每个单元格,


如果Len(Trim(cell.Value))=0,则退出“,如果提供有关发生错误的行的信息,将更容易提供帮助。例如,您有以下错误,很可能是问题的根源:

您在循环中执行此操作,但在下一次迭代中不设置
outordeditor
变量。因此,在第二次迭代中,在
outordeditor.Content.Paste
行,您会得到“objectnotset”

我建议的解决方案是将这些语句移动到循环内的

Set OutMail = OutApp.CreateItem(0)
Set OutWordEditor = OutMail.GetInspector.WordEditor
此外,您不需要两个嵌套循环,只需要一个:

通过以上所有操作,您的(单循环)将如下所示:

For Each cell In rng.Cells
    If Len(Trim(cell.Value)) = 0 Then Exit For ' <-- to stop at emty cell
    Set OutMail = OutApp.CreateItem(0)
    Set OutWordEditor = OutMail.GetInspector.WordEditor
    ... 'Rest of the loop
Next
对于rng.Cells中的每个单元格,


如果Len(Trim(cell.Value))=0,则退出“@YowE3K IIRC”,我们过去曾讨论过:)这种打开word或excel文件的方式也是正确的;在注册表中可以找到合适的应用程序。不-没有与我讨论过这个问题。(反正我都不记得了。)但我会相信你。@YowE3K那可能不是和你在一起的,我记不清了,但我很确定那是在我们“相遇”的早期:呸-看看(我刚刚试过)-我又学到了新东西!!!谢谢我记不起我昨天讨论过的事情,所以肯定记不起“早期”的事情:D@YowE3KIIRC我们过去曾讨论过:)这种打开word或excel文件的方式也是正确的;在注册表中可以找到合适的应用程序。不-没有与我讨论过这个问题。(反正我都不记得了。)但我会相信你。@YowE3K那可能不是和你在一起的,我记不清了,但我很确定那是在我们“相遇”的早期:呸-看看(我刚刚试过)-我又学到了新东西!!!谢谢我记不起我昨天讨论过的事情,所以肯定记不起“早期”的事情:D