使用VBA将MS Access报告导出到MS Word [英] Exporting MS access Report to MS Word using VBA

查看:106
本文介绍了使用VBA将MS Access报告导出到MS Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的vba脚本来查找文件.如果存在,则将其删除.然后将其移至DoCmd.OutputTo acOutputReport.下面是我正在使用的实际代码的示例.我遇到的问题是当我将auto open设置为true时.我希望文件在导出报告后打开.但是,我发现由于某种原因该程序停顿了,并且将在那儿坐了至少5到10分钟,然后错误地指出远程过程无法完成.错误后,该文件无论如何都打开,没有明显的错误...当我将自动打开更改为false时.我可以在大约3到5秒后通过手动打开报告来打开它.是否有我不知道或找不到的东西导致此延迟问题?

I am doing a simple vba script that looks for a file. If it exsits it deletes it. then it moves on to the DoCmd.OutputTo acOutputReport. Below is the an example of the actual code i am using. The problem i am running into is when i set the auto open to true. I want the file to open after it exports the report. However i am finding that for some reason the program stalls and will sit there for atleast 5 to 10 minutes then it will error out saying that the remote proceedure can not be completed. After the error the file opens anyway with no apparent errors... When i change the auto open to false. I am able to open the report in about 3 to 5 seconds later by manually opening it. Is there something i do not know or can not find that is causing this latancy issue?

示例代码-有些语法在语法上可能是错误的,因为我没有前面的代码.但是它仍然应该传达我在做什么.

Example code - Some things might be syntacticly wrong as i dont have the code infront of me. But it should still communicate what im doing.

Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim cnn As Access.Application

strFile = "C:Myfile.rtf"
If Len(Dir(strFile)) Then
Kill strFile
End If

'access connection stuff here

with cnn
     'dont display messages code
     'active connection to my database code
      DoCmd.OutputTo acOutputReport, strFile, acFormatRTF, OutputFile, True
      .Quit
End With

因此,我再次遇到的问题是,在访问中使用报告填充文件后,似乎无法自动打开该文件.我只是想提高性能,或者在已知的情况下找到解决方法.

so again the issue i am having is that i can not seem to get the file to automatically open after access populates it with the report. I am simply trying to increase the preformance or find a work around if you know of any.

推荐答案

在从Access 2007导出的应用程序中,我将导出操作保存为导出规范,并使用了:

In an application which exports from Access 2007, I saved the export operation as an export specification and used :

CurrentProject.ImportExportSpecifications(NameOfSpecification).Execute False

然后我用

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

Set Doc = AppWord.Documents.Open(NameOfDocument)

AppWord.Visible = True                      ' instance is invisible by default

如果有VBA的方式,我倾向于避免使用DoCmd:VBA给了我更多的控制权.

I tend to avoid DoCmd if there's a VBA way of doing it: VBA gives me more control.

这篇关于使用VBA将MS Access报告导出到MS Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆