Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word VBA - close doc and continue running macro

Posted on 2013-01-22
8
Medium Priority
?
1,596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

670 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