首页 > 解决方案 > 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

标签: vbams-access

解决方案


感谢评论,我检查/测试了 .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

推荐阅读