2017-02-06 18 views
1

我正在使用VBA創建單詞的實例,目前通過添加對庫的引用,但這會導致問題,因爲存在一些沒有單詞的機器。創建對象以便實時處理字(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) 

到動態創建的VBA的對象,但我的問題是,oWord不是Word.Application的對象。那麼如何處理呢?

很高興能獲得像Word.Application一樣的所有功能。

+0

請參閱http://stackoverflow.com/questions/9491314/preventing-excel-vba-compile-errors-due-to-users-having-an-older-version-of-ms-o –

+0

或http:///stackoverflow.com/a/41855913/4628637 – R3uK

回答

1

您沒有使用外殼,直接使用COM廠:

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

+0

是的,那太棒了。謝謝。 – Kinimod

相關問題