Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
创建用于动态处理word的对象(VBA)_Vba_Ms Word - Fatal编程技术网

创建用于动态处理word的对象(VBA)

创建用于动态处理word的对象(VBA),vba,ms-word,Vba,Ms Word,我正在使用VBA通过添加对库的引用来创建word的实例,但这会导致一个问题,因为存在一些没有word的计算机 这会在这些机器上启动时立即导致运行时错误。不可能捕捉到这个错误 但是,我尝试通过如下方式在VBA中动态创建一个对象 Dim oWshShell As WshShell Set oWshShell = New WshShell ' *** TEST REGESTRY Dim tmp As String tmp = oWshShell.RegRead("HKEY_CURRENT_USER\

我正在使用VBA通过添加对库的引用来创建word的实例,但这会导致一个问题,因为存在一些没有word的计算机

这会在这些机器上启动时立即导致运行时错误。不可能捕捉到这个错误

但是,我尝试通过如下方式在VBA中动态创建一个对象

Dim oWshShell As WshShell
Set oWshShell = New WshShell
' *** TEST  REGESTRY
Dim tmp As String
tmp = oWshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Word\Options\PROGRAMDIR")
Debug.Print tmp

tmp = tmp + "winword.exe"

Dim oWord As Object
Set oWord = Shell(tmp)
但我的问题是oWord不是Word.Application的对象。那么如何处理呢


很高兴能获得Word.Application等所有功能。

您不必使用shell,直接使用COM factory:

Function openWordApp() As Object
    On Error Resume Next
    Set openWordApp = CreateObject("Word.Application")
    If openWordApp Is Nothing Then
        msgBox "Word not installed on this machine"
    Else
        openWordApp.Visible = True
    End If
End Function
在调用者中,检查返回值是否为Nothing。

请参阅或