使用Access 2007 VBA创建和打开Word文档

时间:2016-10-24 12:22:16

标签: vba ms-word access

任何人都可以指出我做错了什么。我试图将Access 2007报告输出到Word(使用.rtf!),但我似乎无法将我的代码指向该文件。我一直收到Type Mismatch错误 这是我的代码:

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

我一直未能通过“设置文档...”#39;线。

1 个答案:

答案 0 :(得分:0)

删除对象/方法周围的双引号...

Set Doc = AppWord.Documents.Open(FileName)