Word VBA - close doc and continue running macro

Hi fellow experts,

I'm creating a Word template that contains some VBA code wich should ultimately pass the document into a document management system. For that, the document template contains a button "Save in DMS". In order to pass the document to the DMS, the file must be in a closed state.

1. I'm unable link the button to code stored in the normal.dot. Is this possible? I can only link the button to code stored in the document template itself.

2. The handover process to the DMS requires the document to be closed. Even if I store the entire handover code in the document template, it will naturally stop as soon as I do a activedocument.close. I have tried the following: save button launches code in document, document calls code in normal.dot, that code in normal.dot closes the document and continues the handover to the DMS. This, too, does not work - as soon as the document is closed (by normal.dot's code), the whole program stops.

Is there a way I can either link a button in a document to code stored in normal.dot? And will this allow me to close the document and still continue running code? Or how else can I  continue a process that was triggered by code in a document that gets closed during the process?

Thanks for your useful feedback.

Thomas
StaudteAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
You can continue after the close by using the OnTime method. This code works for me.
Private Sub btnELOSave_Click()
    'ActiveDocument.SaveAs2 Environ("Temp") & "\RgNr " & ReadProp("RgNr") & ".docm", wdFormatFlatXMLMacroEnabled
    Application.Run "AusgangsrechnungSpeichern", ActiveDocument
End Sub
'----------
Sub AusgangsrechnungSpeichern(doc As Document)
    MsgBox 1
    Application.OnTime DateAdd("s", 1, Now), "MsgBox2"
    doc.Close (Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
Sub MsgBox2()
    MsgBox 2
End Sub

Open in new window

Yes,  ActiveX event code must be in the ThisDocument module of the document, though Macrobutton and FormField Exit & Entry macros can be in a code module of the template.
Personally, I try to avoid using ActiveX control;s on the document. I think they work better on a Userform. Also, we frequently get requests about how to hide ActiveX buttons when printing
0
 
GrahamSkanRetiredCommented:
You can use the Application.Run method. This can be used between different templates (including ones that are currently closed). You can even use it to run code in a different VBA host (e.g. Excel).
0
 
GrahamSkanRetiredCommented:
Or you could put the whole of the code in a Global template, such as the Normal or, better,  one placed in the Word Startup folder.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
StaudteAuthor Commented:
Thank you for the quick reply, however, Application.Run also stops the whole process as soon as the calling Word doc is closed. As far as your second suggestion is concerned, I fully agreee, but how do I attach the code in the global template to a button in the document? Isn't the event handler of a button required to be in the same document?

Here are some code fragments to show what I've done with Application.Run:

----- in the document template:
Private Sub btnELOSave_Click()
    ActiveDocument.SaveAs2 Environ("Temp") & "\RgNr " & ReadProp("RgNr") & ".docm", wdFormatFlatXMLMacroEnabled
    Application.Run "AusgangsrechnungSpeichern", "RgNr " & ReadProp("RgNr") & ".docm"
End Sub

Open in new window


----- in Normal.dot:
Sub AusgangsrechnungSpeichern(doc As String)
    MsgBox 1
    Application.Documents(doc).Close (Word.WdSaveOptions.wdDoNotSaveChanges)
    MsgBox 2
End Sub

Open in new window


Msgbox 1 is executed just fine, but the thread stops after the close and never gets to msgbox 2.
0
 
StaudteAuthor Commented:
Thanks Graham, that works like a charm. For others coming back here: It should be noted that the 1 second delay in the OnTime call is needed. As far as the sideshow hiding ActiveX buttons when printing is concerned: I handle this by putting the buttons in textboxes. The following example of mine has two text boxes, "Stempelfeld" and "Buttons". It prints two copies of the document on two different printers (color for the "original" and black and white in tonersave mode for the internal copy), where "Buttons" are always hidden and "Stempelfeld" is printed on only one  copy (used for internal purposes):

Private Sub btnDruck_Click()
   
  Dim s As Shape

  For Each s In ActiveDocument.Shapes
    If s.AlternativeText = "Buttons" Then s.Visible = msoFalse
    If s.AlternativeText = "Stempelfeld" Then s.Visible = msoFalse
  Next s

  ActivePrinter = "Brother MFC-9840CDW"
  Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=False, Background:=False, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0
 
  For Each s In ActiveDocument.Shapes
    If s.AlternativeText = "Stempelfeld" Then s.Visible = msoTrue
  Next s
 
  ActivePrinter = "Brother MFC-9840CDW (nur schwarz und Tonersparen)"
  Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=False, Background:=False, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0
 
  For Each s In ActiveDocument.Shapes
    If s.AlternativeText = "Buttons" Then s.Visible = msoTrue
    If s.AlternativeText = "Stempelfeld" Then s.Visible = msoTrue
  Next s
     
End Sub
0
 
GrahamSkanRetiredCommented:
Thanks  Staudte,
That's ingenious. I'll recommend it the next time that someone asks.
0
 
StaudteAuthor Commented:
I forgot to mention that my code to hide buttons requires that the AlternativeText property of the textboxes is set accordingly. It should be obvious from the code, but just in case... :-)
0
 
GrahamSkanRetiredCommented:
Thanks. I can see how than could be useful,
0
All Courses

From novice to tech pro — start learning today.