创建用于动态处理word的对象(VBA)
我正在使用VBA通过添加对库的引用来创建word的实例,但这会导致一个问题,因为存在一些没有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\
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。请参阅或