vba - Access vba 运行缓慢
问题描述
下面的 sub 大约需要 2 秒才能到达 DisplayMessage("TEST"),然后我必须等待 20 秒才能控制返回到运行它的表单。
Sub OpenWordDocPrivacy()
Dim wdApp As Word.Application, wdDoc As Word.Document
Dim MyDoc As String
On Error Resume Next 'Otherwise the next line causes error "ActiveX component can't creat object."
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
MyDoc = conAddrPth & "Documents\GPSystems\Privacy.docm"
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(MyDoc)
Set wdApp = Nothing
'Set wdDoc = Nothing
DisplayMessage ("TEST")
End Sub
Public Sub DisplayMessage(strmessage As String)
MsgBox strmessage, vbExclamation, conAppName
End Sub
解决方案
感谢评论,我检查/测试了 .docm 文件。问题就在这里。该文件的功能是生成一个pdf。所以解决方案是通过访问报告生成 pdf。这是.docm 中的代码: 无法想象我会让它快速运行。
enter code here
Option Explicit
Private Sub Document_Open()
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:="E:\Peter\Documents\GPSystems\DataSource.txt", LinkToSource:=True, Revert:=True, Format:=wdOpenFormatAuto, SubType:=wdMergeSubTypeOther
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
End With
.Execute Pause:=False
End With
ActiveDocument.SaveAs FileName:="E:\Peter\Documents\GPSystems\Privacy Declaration.rtf", FileFormat:=wdFormatDocument
ActiveDocument.SaveAs FileName:="E:\Peter\Documents\GPSystems\Privacy Declaration.pdf", FileFormat:=wdExportFormatPDF
ActiveDocument.Close (wdSaveChanges)
Word.Application.Quit SaveChanges:=wdDoNotSaveChanges
End Sub
推荐阅读
- swift - 在 UIBezierPath 图上应用渐变层
- javascript - 如何从 JSON 中获取和显示其他选项
- azure-functions - Azure Functions Application Insights 上下文
- android - 在android Nougat Api 24中以编程方式断开来电
- typescript - 打字稿在通用中标记了联合
- ajax - 使用 ASP.NET mvc 在视图中分页数据而不重复 sql 查询
- arrays - 尝试更改使用迭代创建的数组中的单个值会更改多个值。为什么?
- excel - 识别 vba 列表中的文本变量
- unicode - 日文和中文名字/姓氏检测器
- java - 为多个模型 tensorflow java 的作业指定 CPU 或 GPU