2016-10-24 77 views
-1

任何人都可以指出我做錯了什麼。我試圖向Word輸出一個Access 2007報告(使用.rtf!),但我似乎無法將我的代碼指向該文件。我不斷收到類型不匹配錯誤。
這裏是我的代碼:使用Access 2007 VBA創建和打開Word文檔

Private Sub CatalogTitle_Click() 
Dim AppWord As Object 
Dim Doc As Object 
Dim FileName As String 
Dim DateTime As Date 
Dim DTString As String 
Dim x As Integer 


Set AppWord = CreateObject(Class:="Word.Application") ' create an instance  of Word 

DateTime = Now()    ' build a date string which is compatible with the Windows File structure 
DTString = CStr(DateTime) 

For x = 1 To Len(DTString) ' changes dd/mm/yy hh:mm:ss into dd_mm_yy hh_mm_ss 
    If (Mid(DTString, x, 1) = "/" Or Mid(DTString, x, 1) = ":") Then 
     Mid(DTString, x, 1) = "_" 
    End If 
Next x 

' build the full .rtf filename 
FileName = "Titles " & DTString & ".rtf" 

' and output the rtf file to it 
DoCmd.OutputTo acReport, "ItemsReportByTitle", acFormatRTF, FileName 

' then open the Word instance with the file just created 
Set Doc = "AppWord.Documents.Open(FileName)" 

' and make the instance visible 
AppWord.Visible = True 

End Sub 

我一直沒有在「設置文檔...」線。

回答

0

刪除圍繞對象/方法的雙引號...

Set Doc = AppWord.Documents.Open(FileName) 
+0

哇!這產生了巨大的差異。謝謝! – Solong

+0

是的 - 你正在分配一個字符串,這不適用於'Set ='。從編譯器的角度來看,Set Doc =「xyz」'完全一樣。 - 如果答案解決了你的問題,你可以[接受](http://stackoverflow.com/help/someone-answers)它,這也標誌着問題已解決。 @太長 – Andre