Autorun Word Macro

J

John Calder

Hi

I have a normal word document that has a macro in it that provides a
sequential number each time I open the document:

Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub


This works perfectly ok when I open the document and the select Tools,
Macro, Run.

What I would like to do is place this document on a server (so that it can
be accessed by an number of users) but have it so that when anyone opens it,
the macro will run automatically. (Without having to go to Tools, Macro, Run)

Can anyone help me?

Thanks

John
 
J

John Calder

Thank you for your prompt response Jay.

I understand how to construct the macro that creates the sequential number
but I do not understand how to make this macro run automatically when the
document is opened? can you help? The link you supplied does not seem to
explain this.

Thanks

John
 
C

Charles Kenyon

Name the macro AutoNew. It will fire when a new document is created based on
your template. (You want it to run when the document is created, not each
time it is opened.)
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jay Freedman

John, Charles's advice is exactly correct. I just want to add that the
article does tell you to use an AutoNew macro, and that word (in
orange) is a hyperlink to another page on the site that explains all
the Auto macros.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

John Calder

Jay & Charles

Thanks for your help.

I apoligise for the lack of understanding but I dont really know much about
macro's at this level and alas I have failed to come to grips with your
advice.

This is what I have:

A document called AutoNumber.doc on my local drive

In this document I have a macro called AutoNew (with the code previously
metioned)

When I open the document AutoNumber.doc I get a blank work area.

If I then run the macro (AutoNew) the document then re-saves to path001.doc
and the number 001 is displayed on the document.

If i then close it all down and re-open the AutoNumber.doc and do the same
thing then the document re-saves to path002.doc with the number 002 displayed
on the document.

Which is exacty what I want it to do. However I would like to place the
document called AutoNumber.doc on a server drive and have a link to it from
my web page so that others in the department can open and use this document.
This I can do.

I do not want the people that use the document to then have to run the macro
(AutoNew) by having to go to "Tools", "Macro", "Run" to recreate the
sequential number.

I would like them just to open the document and the new number be there on
the work area. So, I am assuming that there is something that can be done
that when someone opens the document AutoNumber.doc that there is a way that
the AutoNew macro runs automatically.

I would really appreciate some sort of step by step instruction if this is
possible.

Thanks again and I hope this helps

John
 
J

Jay Freedman

Hi John,

What you're missing is that the macro must be in a _template_ (a file
with a .dot extension, saved that way by using File > Save As and
setting the file type to "Word Template"), not in a document.

The significance of the name AutoNew is that Word automatically runs
that macro when you create a new document based on the template
("auto" + "new" -- get it?). The macro does _not_ run automatically
when you open an existing document (there's a different special name,
AutoOpen, for a macro that does that -- but it won't work for this
application).

There are some other points you need to know for the server situation:

- If you put a hyperlink on a web page that points directly to a
template, it will open the template itself instead of creating a new
document based on the template. The workaround is, I believe, to make
a shortcut that points to the template file's location, and make the
hyperlink point to the shortcut. It sounds odd, but it should work.

An alternative is to tell your coworkers to modify the Workgroup
Templates location in Word's Tools > Options > File Locations dialog
to point to the server folder that contains your template. Then the
template will appear in the File > New dialog along with all their
local templates. Unfortunately, each person would have to do this on
their own PC.

- In order for the macro to work for multiple users, the file that
stores the current number must be the same file for everyone. That
means it can't be on the local C: drive for anyone -- it must be on
the server in a location everyone can read. (Just change
"C:\Settings.txt" in two places in the macro to something in the
format "\\officeserver\sharename\somefolder\Settings.txt".) Each user
also must have write permission to that folder so the macro can update
to the next number.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

John Calder

Outstanding !!!!

Thanks a lot Jay, not only did I get it to work but I now understand why it
works.

Many many thanks

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top