使用 VBA 将 MS 访问报告导出到 MS Word
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15575092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting MS access Report to MS Word using VBA
提问by divdeamor
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?
我正在做一个简单的 vba 脚本来查找文件。如果它存在,则将其删除。然后它移动到 DoCmd.OutputTo acOutputReport。下面是我正在使用的实际代码的示例。我遇到的问题是当我将自动打开设置为 true 时。我希望文件在导出报告后打开。但是我发现由于某种原因程序停止并会在那里停留至少 5 到 10 分钟,然后它会出错,说无法完成远程程序。错误发生后,文件仍然打开,没有明显错误...当我将自动打开更改为 false 时。我可以在大约 3 到 5 秒后手动打开报告。有什么我不知道或找不到导致此延迟问题的原因吗?
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.
所以我再次遇到的问题是我似乎无法在访问用报告填充文件后自动打开文件。我只是想提高性能或找到解决方法(如果您知道的话)。
回答by grahamj42
In an application which exports from Access 2007, I saved the export operation as an export specification and used :
在从 Access 2007 导出的应用程序中,我将导出操作保存为导出规范并使用:
CurrentProject.ImportExportSpecifications(NameOfSpecification).Execute False
CurrentProject.ImportExportSpecifications(NameOfSpecification).Execute False
I then used
然后我用
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
I tend to avoid DoCmd
if there's a VBA way of doing it: VBA gives me more control.
DoCmd
如果有 VBA 的方式,我倾向于避免:VBA 给了我更多的控制权。