Solved

Word VBA - close doc and continue running macro

Posted on 2013-01-22
8
1,396 Views
Last Modified: 2013-01-23
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
0
Comment
Question by:Staudte
  • 5
  • 3
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38807818
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38807834
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
 

Author Comment

by:Staudte
ID: 38808747
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38809394
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Closing Comment

by:Staudte
ID: 38809523
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38810104
Thanks  Staudte,
That's ingenious. I'll recommend it the next time that someone asks.
0
 

Author Comment

by:Staudte
ID: 38810342
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38810433
Thanks. I can see how than could be useful,
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now