我有一些代码可以导出查询并将文档保存为富文本文件:
DoCmd.OutputTo acReport, "rptEvents", "RichTextFormat(*.rtf)", "C:\SARPCCO_Database\SARPCCO_Report.rtf"
如何使用相同的导出按钮打开保存的文档?
此按钮背后的完整代码是:
Private Sub CommandExport_Click()
On Error GoTo Err_CommandExport_Click
Dim db As Database
Dim qdf As QueryDef
Dim strSql As String
'Dim rsCriteria As Recordset
Dim strWhere As Recordset
Set db = CurrentDb
Set strWhere = db.OpenRecordset("qryEvents", dbOpenDynaset)
'*** the first record in the Criteria table ***
strWhere.MoveFirst
'*** loop to move through the records in Criteria table
'Do Until strWhere.EOF
'*** create the Select query based on
' the first record in the Criteria table
strSql = "SELECT * FROM qryEvents WHERE "
strSql = strSql & "[Event_ID] = " & strWhere![Event_ID]
'*** delete the previous query
'db.QueryDefs.Delete "qryEvents2"
'Set qdf = db.CreateQueryDef("qryEvents2", strSql)
DoCmd.OutputTo acReport, "rptEvents", "RichTextFormat(*.rtf)", "C:\SARPCCO_Database\SARPCCO_Report.rtf"
Documents.Open Filename:="C:\SARPCCO_Database\SARPCCO_Report.rtf", ReadOnly:=False
strWhere.MoveNext
'Loop
Exit_CommandExport_Click:
Exit Sub
Err_CommandExport_Click:
MsgBox "The file SARPCCO_Report you want to export to doest not exists." & vbCrLf & vbLf & _
"Please, ensure the file is in the right place before attempting to export your report", vbExclamation + vbOKOnly, "Report exporting error"
Resume Exit_CommandExport_Click
End Sub
答案 0 :(得分:0)
谢谢GOOGLE,我找到了一个正常的代码:
将WordApp作为对象调暗
Dim WordDoc As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\MyFiles\Test.rtf", ReadOnly:=False)
WordApp.Visible = True
'do your stuff
Set WordDoc = Nothing
Set WordApp = Nothing
答案 1 :(得分:0)
为 AutoOpen 附加 True :
DoCmd.OutputTo acReport, "rptEvents", "RichTextFormat(*.rtf)", "C:\SARPCCO_Database\SARPCCO_Report.rtf", True