We help IT Professionals succeed at work.

Can I execute file operations after Word closes?

I've got a global template (.dotm in the user's Word\STARTUP folder) that creates a custom tab in the Word ribbon menu, with buttons for all kinds of document automation macros written in VBA.

As part of my initialization checks, I'd like to determine whether the user has the latest version of the Add-In installed, by comparing a flag file in the registry vs. one on a network resource library.  If the user does not have the latest version installed, I want to give them an opportunity to download it to their machine.  

I know how to copy the .dotm file from the network and place it in the correct folder on the user machine, but the obvious problem is that the (old version of the) global template is open as long as Word is open, so I can't immediately replace the file.  I can download the .dotm file to STARTUP with a slightly different filename, but then there are two copies.  Is there a clever workaround for this scenario?  Some kind of script that can be set to execute after Word closes, to delete the old file and rename the just-downloaded file?   That's getting outside my area of expertise, but any pointers would be appreciated.

Thanks!
Comment
Watch Question

kevinhsiehNetwork Engineer

Commented:
How about a login script, so the check and file copy happens as the person logs in?
Top Expert 2014

Commented:
You could create a batch script that would accept the name of the open document as parameter and do the dotm update, followed by the open of the file.  I'd suggest Powershell, but you may be restricted in your choices of scripting engines.

A few clients use an intermediary program/script to open the application.  The intermediary program, which can be VBA code, can check for the need for dotm version update, and fix that condition, before launching Word.

Commented:
I would suggest having a script which runs on word opening then checks for your updated version and copies it if needed and then opens the doc for you.

Personally though I would do as suggested by others and have the central file copied to pc on login either by login script or more logically using group policy preferences.

Steve
GrahamSkanRetired
Top Expert 2012

Commented:
Change the attached template (Developer tab, Document Template group, Attach... button)  to, say, the Normal template, Update your template, then change the attached template back.
The three steps could be automated with a simple VBA macro, something like this (untested)
Sub SwitchTemplates()
    Dim strMyTemplate As String
    
    strMyTemplate = ActiveDocument.AttachedTemplate.Name
    ActiveDocument.AttachedTemplate = Application.NormalTemplate
    FileCopy "WorkTemplatesFolder\strMyTemplate", Application.NormalTemplate.Path
    ActiveDocument.AttachedTemplate = strMyTemplate 
End Sub

Open in new window

Bryce BassettFreelance VBA programmer

Author

Commented:
Graham:  I think you're on to something!

The catch is that this is a global template (Add-in), that lives in the STARTUP folder so it is not attached to a document per se.  I can manually go to Developer\Document template to un-load the add-in (my custom tab disappears), then update the file to a new version, then re-load the add-in, all without closing Word.

But when I try to do that using VBA I run into this challenge:  The macro lives in the add-in, so when I got AddIns (full path).Installed = False, it unloads the add-in but of course the macros halts immediately.

I've been experimenting with this approach:  I created a separate little .docm containing macro code in Document_New which un-installs the global add-in, copies the new file into place, then re-installs the add-in.  The (old) add-in opens the .docm which launches the above macro and updates the add-in, then calls a second macro in the (new) add-in which closes the .docm.  Parts of it are working, but the whole sequence not quite yet.

In principle, shouldn't this approach work?   LMK if you have other suggestions.

Thanks.

Commented:
I think that is roughly what I suggested, i.e. have a 'boot' macro which then checks for updates and loads the main one.

If the users dint change the macros themselves I would say your best bet is just to replace the local copy of file during login if the server one had been updated - simple xcopy /d or RoboCopy from login script or single entry in group policy preferences.
Bryce BassettFreelance VBA programmer

Author

Commented:
Thanks, Steve

My client is an enterprise organization and I've already been told they are not open to including this in their group policy login scripts, so I'm looking for a way to do this on my own.  I'll keep working on this idea of an outside macro.

Commented:
In which case do that yourself, have a batch script running copy or RoboCopy that you add to the users startup through hkcu run key etc. assuming a handful of users?  Could even give them an icon on desktop to check for need versions etc.

I'll test out on pc later a Vba method
Bryce BassettFreelance VBA programmer

Author

Commented:
It's an Enterprise organization, so they won't let us change login scripts.  We want to make this as hands off as possible for users.

Why won't the following work?  It is designed to work like this:

1.  MyCustomAddIn1.dotm is saved in the STARTUP folder
2.  On launch, the add in loads.  See My Custom Tab 1 (to the right of Home)
2.  On that tab, run first updater macro
3.  First macro copies a newer version from c:\test\MyCustomAddIn2.dotm, into the STARTUP folder
4.  First macro opens c:\test\Updater.dotm a calls second macro
5.  Second macro uninstalls MyCustomAddIn1
6.  Second macro installs MyCustomAddIn2
7.  Second macro closes Updater.dotm
8.  New Add-in is now active.  See My Custom Tab 2.

When I test this out,  it halts after step #5.  I can see that is has copied the new Add-in to STARTUP.  It uninstalls the first add-in (Developer, Document Template to verify) then stops.  If I run the UpdateVersion macro manually from Updater.dotm, it complains it can't to find MyCustomAddIn2.  If I wait a couple minutes then run it, it works.  But why won't it work immediately as written?  

To test this out, please save MyCustomAddIn1.dotm in the Word STARTUP folder, create C:\test, and save MyCustomAddIn2 and Updater.dotm into C:\test..

Can someone help me to get this working?  Thanks!
MyTestAddIn1.dotm
MyTestAddIn2.dotm
Updater.dotm

Commented:
That's why i asked how many users as if it is only a handful then the users can trigger a simple copy command automatically as part of their profile.

Didn't get a chance to try the way I was going to before, will have a go this morning if I can.

Commented:
I had a quick go with this and method I thought would work seems to OK.  You'll have your updater macro in startup too.  In that check for your updated file and if it is found do this:

application.AddIns(Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn1.dotm").Delete
'copy the replacement file
application.AddIns.Add(Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn1.dotm")

Seems to work for me with my own test files alongside looking at what you had.
Bryce BassettFreelance VBA programmer

Author

Commented:
Steve:

Thanks for sticking with me on this!   I still can't quite get it to work.  Here's what I'm doing:

MyTestAddIn1.dotm is in the STARTUP folder
Update.dotm is also in the STARTUP folder
MyTestAddIn2.dotm is in C:\test

When I want to update, I call this UpdateVersion macro (which lives in the Updater.dotm add in).  Notice I am using your suggestion of AddIns.Delete and AddIns.Add, instead of AddIns.Installed.

Application.AddIns(Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn1.dotm").Delete

source = "c:\test\MyTestAddIn2.dotm"  'newer version
destination = Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn2.dotm"
Call FSO.copyfile(source, destination, True)

Application.AddIns.Add (Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn2.dotm")

FSO.deletefile (Environ("AppData") & "\Microsoft\Word\STARTUP\MyTestAddIn1.dotm")

Open in new window

This successfully unloads version1, copies version2, and loads version2.  But it gives me a permissions error when it tries to delete version1 from the STARTP folder.  Word still thinks that file is open or loaded or something.  I need to delete the old version otherwise next time I launch Word I get both version1 and version2 tabs, which I don't want.  I tried copying the new version over the old version keeping the same name but run into the same permissions error.

This is also strange, and might be a clue.  When I launch Word, my templates screen looks like this:Before running update.  then after running the updater it looks like this:  after updating
What am I missing?
MyTestAddIn1.dotm
Updater.dotm
MyTestAddIn2.dotm
Bryce BassettFreelance VBA programmer

Author

Commented:
Steve, have you given up on this?  Anybody else please?  Thanks!
Commented:
I haven't.  Have been busy, sorry.  I think your best bet still is to give them an indication maybe that they need to replace it because it is out of date with a nag screen and tell them to click on the icon on their desktop.... you could even drop them the icon, e.g.

f=freefile
open environ$("userprofile") & "\desktop\update macro.cmd" for output as #f

Print #f,"xcopy /y /d ""\\server\share\whatever.dotm"" ""%appdata%\Microsoft\Word\STARTUP"""
Print #f, "del "%~0"""
close #f
Bryce BassettFreelance VBA programmer

Author

Commented:
Thanks, all, for your suggestions.  I guess it's too much to ask to do this all in Word with VBA, and I like the .cmd file solution.  The client seems ok with that too.

Commented:
Good, sorry I did try a few other ways but they seemed to behave differently on different occasions or computers so you would probably have found situations where it didn't work for some then.

You could have it execute the batch file using Shell command from within word - e.g.

msgbox to ask "Update now, it will need to close Word?", quit if they say "no"
Write batch file
batchname = "e:\temp\wordupdate.cmd"
x = Shell("""" & batchname & """", vbNormalFocus)
Application.Quit (True)

Open in new window


Your batch file could include then something like:

@echo off
color 4F
mode 60,20
Echo Your Word addin needs to be updated.
Echo Please press a key to start this, will continue in 10 sec
Echo.
Timeout 10
echo Do your update xcopy
color 2F
cls
echo Update complete.  Press any key to start Word
timeout 10
START winword

Open in new window