Search code examples
vbams-worddialogmsgbox

Word VBA: Static Status dialog window


I've created a form that works well with macros running in the background to validate data and then print the document to a specific printer on the network.

The key element of this process is a production number value which I would like to keep a running log of and display in a static status dialog window. In other words, a popup window similar to a MsgBox that would not interfere with other actions on the form, but float on top of the document.

Visual concept of this would be...

enter image description here

User could shift the window away from their work if needed. Close the window if they desired, but pragmatically I want to re-pop/refresh the data in the window each time the background macro completes.

I can't use MsgBox, because it forces a closure of the window before the user can continue working on the document. I just want this visible to the user so they know what was last worked on and the few prior to that.

Any idea what control I might be able to use, or switch to MsgBox that would allow the user to continue working?

Ken...

PS: I found this and am trying to find a way to make this work for me. So far I have managed to get to function in the manner I want, but the lingering issue is how to call this PS script and include the information I need to display. Alternatives to MsgBox in VBScript - StackOverflow

PPS: I opted to go a slightly different route and release the form with a MsgBox that is displayed at the end of the macro. I describe this in the solution noted below.


Solution

  • After doing much research, I've come back to revising my macro to incorporate static variables and a MsgBox at the end to report the last 5 production numbers that have been printed.

    To provide a means of bringing up this MsgBox for reference, between printing runs, I created an OnlyNum variable as string and replaced the MsgBox I had for letting the users know they were only to use numbers in this field with that message. The end of that trap diverted the flow to the bottom of the macro (where the MsgBox that displayed the last five print jobs has been placed).

    So, when the status MsgBox is displayed as a result of printing it only shows the last five events. If the trap captures it, it shows the message letting the user know to only use numerals and then displays the last five events.

    Code reference:

    Private Sub CommandButton1_Click()
    
    Dim Prod As String
    Dim Temp As String
    Dim OnlyNum As String
    
    Static ProdNum1 As String
    Static ProdNum2 As String
    Static ProdNum3 As String
    Static ProdNum4 As String
    Static ProdNum5 As String
    
    'Check for only numeric value of TextBox1.Text
        If Not IsNumeric(TextBox1.Value) Then
        OnlyNum = "only numbers allowed" & vbCrLf & vbCrLf
        Cancel = True
        GoTo NotToday
    End If
    
    'Remove any spaces from TextBox1.Text 
    Prod = Replace(TextBox1.Text, " ", "")
    
    'If the resulting lenght is equal to 7 Print it.
        If Len(Prod) = 7 Then
            ActiveDocument.PrintOut
    
    'Update recent production numbers (5 in total)      
        ProdNum5 = ProdNum4
        ProdNum4 = ProdNum3
        ProdNum3 = ProdNum2
        ProdNum2 = ProdNum1
    
        ProdNum1 = Prod & " - " & Now()  ' Insert a new production number with timestamp
    
        TextBox1.Text = ""    'Clear the value of TextBox1.Text to prepare for the next Production number
    
      Else
        MsgBox ("Production Numbers must be 7 digits and contain only numerials.")
    End If
    
    NotToday:
        Application.ActivePrinter = Temp
        MsgBox (OnlyNum & ProdNum1 & vbCrLf & ProdNum2 & vbCrLf & ProdNum3 & vbCrLf & ProdNum4 & vbCrLf & ProdNum5)
        OnlyNum = ""  'Reset value of OnlyNum 
    
    End Sub