2013-07-31 54 views
1

我正在使用Access 2013,並在表單上創建了一個幫助按鈕,我希望打開帶有說明的Word文檔。這是我試過的代碼:在Access 2013中使用VBA打開Word文檔

Private Sub cmdHelp_Click() 
    Dim wrdApp As Word.Application 
    Dim wrdDoc As Word.Document 
    Dim filepath As String 

    Set wrdApp = CreateObject("Word.Application") 
    wrdApp.Visible = True 

    filepath = "C:\...\Handout.docx" 
    Set wrdDoc = wrdApp.Documents.Open(filepath) 
End Sub 

的問題是,當我嘗試編譯,我得到的第一行,說:「沒有定義的用戶定義類型」

+1

你在VBA/IDE編輯器設置爲'字Library'參考? –

+0

我沒有。我是VBA新手,我該怎麼做? – user2521720

+0

轉到VBA編輯器>>菜單>>工具>>參考>>並在XX.X(可能是16.0)之後查找列表中具有最高可能編號的「Microsoft Word XX.X Object library」>>檢查它>>按OK。然後再次運行宏... –

回答

3

請檢查您是否在VBA環境中設置了對Word Library的適當引用。

這樣做遵循的路徑:

Go to VBA Editor >> Menu >> Tools >> References >> 
    Find on the list Microsoft Word XX.X Object Library where 
    XX.X is the highest available number >> 
Check it >> press OK. 
+1

用早期結合和早期結合來發展幾乎總是更好。 – Fionnuala

4

那豈不是更容易出錯只是要做:

Shell "winword ""c:\...\Handout.docx""" 

...而不是創建一個對象只是爲了打開應用程序?