Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/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 Set Word=GetObject(,“Word.Application”在同一文档的一个子系统中工作,但在另一个子系统中不工作_Excel_Vba_Ms Word - Fatal编程技术网

Excel Set Word=GetObject(,“Word.Application”在同一文档的一个子系统中工作,但在另一个子系统中不工作

Excel Set Word=GetObject(,“Word.Application”在同一文档的一个子系统中工作,但在另一个子系统中不工作,excel,vba,ms-word,Excel,Vba,Ms Word,我有两个从Excel创建和/或打开Word文档的代码,它们都包含类似的代码,包括语法 Set Word=GetObject(,“Word.Application”) 在一个子模块中,文档可以很好地打开,而在另一个子模块中,我在上面的语法中遇到了运行时错误429,但只有在Word未打开时才会出现。当Word打开时,该函数工作正常 工作子系统代码的一部分 Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column)

我有两个从Excel创建和/或打开Word文档的代码,它们都包含类似的代码,包括语法

Set Word=GetObject(,“Word.Application”)

在一个子模块中,文档可以很好地打开,而在另一个子模块中,我在上面的语法中遇到了运行时错误429,但只有在Word未打开时才会出现。当Word打开时,该函数工作正常

工作子系统代码的一部分

Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate

Range("zz_preventloop").Value = "x"
Application.ScreenUpdating = False

Dim DocType As String
If Range("zz_officeversion").Value = "previous to 2007" Then
DocType = ".doc"
Else
DocType = ".docx"
End If

Dim filename As String
filename = Range("zz_envelope_documents").Value + "/" + Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_doc").Column).Value + "/"
filename = filename + Cells(ActiveCell.Row, ActiveSheet.Range("zz_eDMSname").Column).Value + DocType
      
If Len(filename) < 256 Then
'check the document type
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_doc").Column).Value = ".url" Then ''Opening the .url shortcut
        On Error Resume Next
        ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
        + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
    Else
        If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_doc").Column).Value = ".docx" Then
            Application.Calculate
            On Error Resume Next
            Set Word = GetObject(, "Word.Application")
            If Word Is Nothing Then
                Set Word = CreateObject("Word.Application")
            End If

 Rest of sub
Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate

Range("zz_preventloop").Value = "x"
Application.ScreenUpdating = False
      
Dim DocType As String
If Range("zz_officeversion").Value = "previous to 2007" Then
DocType = ".doc"
Else
DocType = ".docx"
End If
    
'check the document type
If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".url" Then 
''Opening the .url shortcut
    On Error Resume Next
    ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
    + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
Else
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".docx" Then
        Set Word = GetObject(, "Word.Application")
        If Word Is Nothing Then
            Set Word = CreateObject("Word.Application")
        End If

Rest of sub

在第二个函数中,当Word未打开时,它不工作,我忽略了什么?

问题函数与工作函数不一致:

“下一步继续执行错误时,您的代码丢失”

应该是:

On Error Resume Next
Set Word = GetObject(, "Word.Application")
If Word Is Nothing Then
     Err.Clear: On Error GoTo 0 'good to clear the error and let the code raising an error if the case
     Set Word = CreateObject("Word.Application")
End If
On Error GoTo 0
上述代码的逻辑是:

  • 它尝试查找单词opensession并创建单词对象(如果存在这样的会话)
  • 如果这样一个会话不存在,它将引发一个错误,但是
    On error Resume Next
    会跳过该错误
  • 如果无法从现有会话创建Word对象,则会创建一个新会话

  • @乔伊斯:很高兴我能帮忙!但我们在这里,当有人回答我们的问题时,勾选“代码左侧”复选框。为了让人们接受这个答案。这样,其他正在搜索类似问题的人将在代码/建议生效后。。。