如何为工作宏输出消息?

问题描述 投票:0回答:1

如何在Word宏中显示一条消息,说要等很短的时间。该字段应该简单地有 "请稍等 "的内容,脚本应该在后台加载。(我的脚本使之与序列信每个文档单独保存为PDF)。消息应该在开始保存前出现,并在完成后自动关闭。

我以前使用过MsgBox,但我读到它不会在后台加载脚本(只有在关闭MsgBox后才会出现)。

Sub SerienbriefOneDoc()
'
' SerienbriefOneDoc Makro
'
'
 Dim Dateiname As String
 Dim LetzterRec As Long
 Application.ScreenUpdating = False
 Application.Visible = False

    'Variable declaration
    Dim sFolderName As String
    Dim sDesktopPath As String, sFolderPath As String

    'Find Desktop path location
    sDesktopPath = Environ("USERPROFILE") & "\Desktop\"

    'Define folder name to create on the desktop
    sFolderName = "Serienbrief"

    'Folder Path
    sFolderPath = sDesktopPath & sFolderName

    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFolderPath) Then
        'If folder is available on the desktop
        MsgBox "Der angegebene Ordner existiert bereits auf dem Desktop!", vbInformation, "VBAF1"
        GoTo PDFsave
    Else
        'Create Folder
        MkDir sFolderPath

        'Diplay messafe on the screen
        MsgBox "Ordner erstellt : " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1"
    End If



PDFsave:

 'Const path As String = sFolderPath                                 'Speicherpfad des Resultates     N:\Lehre\Basislehrjahr\Auftraege\Projektarbeit\WordMakro\Serienbrief\save\
 ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
 LetzterRec = Word.ActiveDocument.MailMerge.DataSource.ActiveRecord
 ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord

MsgBox "please wait a moment"

With ActiveDocument.MailMerge                                                                                               'Waehlt das aktuelle Dokument des Serienbriefs
         .DataSource.ActiveRecord = wdFirstRecord
         Do
             If .DataSource.ActiveRecord > 0 Then                                                                           'Prueft ob es mehrere Seiten fuer den Serienbrief gibt
                If .DataSource.DataFields("Name").Value <> "0" Then                                                         'zaehlt die Anzahl Datensaetz in der Spalte "Name"
                     .Destination = wdSendToNewDocument
                     .SuppressBlankLines = True
                        If Dir(sFolderPath, vbDirectory) <> "" Then                                                                'prueft ob es das Verzeichnis gibt.
                        Else
                            MsgBox "Verzeichnis existiert nicht"                                                            'Fehlermeldung falls das Verzeichniss nicht existiert
                        End If
                     With .DataSource
                         .FirstRecord = .ActiveRecord
                         .LastRecord = .ActiveRecord
                          dname = sFolderPath & "\" & .DataFields("Name").Value & "_" & .DataFields("Vorname").Value & ".pdf"            'erstellt eine Variable mit dem Pfad und dem Namen
                     End With
                        .Execute Pause:=False
                        ActiveDocument.SaveAs2 FileName:=dname, FileFormat:=wdFormatPDF                                     'benennt die Datei und aendert das Dateiformat auf PDF
                        ActiveDocument.Close False                                                                          'schliesst das Fenster
                 End If
               End If
             If .DataSource.ActiveRecord < LetzterRec Then                                                                  'prueft ob es noch eine Seite gibt im Serienbrief
                 .DataSource.ActiveRecord = wdNextRecord                                                                    'nimmt die naechste Seite des Serienbriefes
             Else
                 Exit Do                                                                                                    'wenn es keine Seite im Serienbrief mehr gibt wird die Schleife beendet
             End If
         Loop
     End With
     Application.Visible = True
     Application.ScreenUpdating = True[enter image description here][1]
End Sub

https:/i.stack.imgur.comwlznU.png。图中显示了它应该是怎样的(只是没有MsgBox

vba ms-word
1个回答
1
投票

在标准VBA库中的每个函数都是同步的:你调用它,它做它的事情,返回,恢复执行下一条语句。

没有办法让一个 MsgBox 显示出来,并且在显示的时候有任何代码在运行,因为执行是在等待用户关闭消息框:在知道返回什么之前,函数不会返回,因此你的程序不能恢复,直到该返回值的 MsgBox 调用是已知的--你最终丢弃该返回值并不重要。

我想请你阅读 可重复使用的进度指示器 (中级-高级)的想法,涉及到用户表单(忘记了) MsgBox 为此)--最简单的方法,就是让一个非模态的用户表单来运行节目(文章演示了如何制作一个可取消的进度指示器,该指示器是模态的,并且不运行节目)。


0
投票

你可以在状态栏上给出一个进度报告。例如,在你的代码中

在你代码的顶部,插入:

Dim i As Long, j As Long, sBar As Boolean
SBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True

After:

With ActiveDocument.MailMerge

插入:

j = .DataSource.RecordCount

After: insert: After:

Do

insert:

i = i + 1
Application.StatusBar = "Processing " & Int(i / j * 100) & "%"

后面:

Application.Visible = True

插入:

Application.StatusBar = False
Application.DisplayStatusBar = SBar
© www.soinside.com 2019 - 2024. All rights reserved.