#1
|
|||
|
|||
Looking for a macro to increase a number by 1 in a Word document
Hello,
I have a Word document that other users can access and change (on the same computer). At the top of that document is a line that says "Version # XXX". What I would like is to be able to run a macro that would increase XXX by 1 and save the document. Is that possible? How? Thanks for any reply, Olopocram (I run Word 2010) |
#2
|
||||
|
||||
It's not apparent from your post why a macro would be needed; users could simply overtype the number or, if appears multiple times, use Find/Replace to update it.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
The idea of a macro is to save the hassle of going through the Find/Replace procedure...
|
#4
|
||||
|
||||
A macro that requires one to input the old & new values won't be any faster than pressing Ctrl-H and inputting them directly into the Find/Replace dialogue.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
You may find - http://www.gmayor.com/automatic_numbering_documents.htm
and/or http://www.gmayor.com/SaveVersionsAdd-in.htm useful. The first linked item stores the number in the template so all users would need access to the same template if they were to share the numbering system.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
(please delete this reply, code not suitable for the given question)
|
#7
|
||||
|
||||
And what happens if the user opens to document to make some edits, but doesn't want to change the numbering?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
The code will not actually work if there is a document change without change the version number.
Thanks |
#9
|
||||
|
||||
Your code also won't work unless a Document Variable named 'VNUM' has been created - for which you'd need additional code.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Yes, it's required to insert once the variable and set it's initial value.
Thanks |
#11
|
|||
|
|||
Hello again,
I'm afraid I might not have been very clear. The idea is this (assuming the macro shortcut is Ctrl+M): – someone opens doc just to read => Ctrl+W to close (no incrementation) – someone opens doc and makes a change => Ctrl+M to increment and close – same person reopens doc to make a slight change => Ctrl+W to close (no incrementation) The way I see it – but I am not a specialist (this is why I post here) – the version number would be stored in some kind of field within the doc that the macro would only have to read and add 1 to before closing the doc. Thus no user imput needed. Question is : is it at all possible ? Thanks to all |
#12
|
|||
|
|||
Try this:
1st Type this in the document: "Version #", CTRL+F9 (to insert a field), "{ DOCVARIABLE VNUM }" Put his code in a module: Code:
Sub Increment() On Error Resume Next With ActiveDocument .Variables.Add ("VNUM") On Error GoTo 0 If IsNumeric(.Variables("VNUM")) = False Then .Variables("VNUM") = 1 Else If .Saved = False Then .Variables("VNUM") = .Variables("VNUM") + 1 Else MsgBox "No changes!" End If End If .Fields.Update .Save .Close End With End Sub The Ctrl+W is really necessary? Why not just save and close the document ? Test before use, backup your files before, use at your own. Last edited by eduzs; 03-07-2018 at 05:06 AM. |
#13
|
||||
|
||||
eduzs: Kindly stop deleting your posts after they've been responded to; the practice (which you've employed on a number of occasions now) makes it impossible for anyone else to understand the discussion that has taken place.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Ok, thanks! I think that this second code is more suitable.
|
#15
|
|||
|
|||
Works like a breeze! Thanks eduzs and Paul.
|
Tags |
macro, word 10 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Increase number in cell, based on value in adjacent cell | scottyb | Excel | 3 | 02-02-2017 03:51 AM |
How to find number of coma and then add that number of rows in word using macro? | PRA007 | Word VBA | 7 | 05-27-2015 10:45 PM |
Global macro across a number of different word files | daffy | Word VBA | 6 | 07-08-2014 05:42 PM |
Createing Macro to Increase Margin by 1pt | thundercats9595 | Word VBA | 2 | 01-29-2014 12:36 PM |
Increase serial number for every print | atomtm | Word | 1 | 06-15-2012 05:39 AM |