![]() |
|
|
|
#1
|
|||
|
|||
|
I want to have a Field in Word which is updated every time i open the document.
Example: BIR-000001 will be BIR-000002 NexT time i open the document and so on. How can i solve this? |
|
#2
|
||||
|
||||
|
Insert the following DOCVARIABLE field in the document where you want the number to appear: { DOCVARIABLE VarNum \# "'BIR-'000000" }
Add the following macro to the document and save as a macro enabled document: Code:
Sub AutoOpen()
Dim oVars As Variables
Dim bVar As Boolean
Dim lngCount As Long
Set oVars = ActiveDocument.Variables
For Each oVar In ActiveDocument.Variables
If oVar.Name = "varNum" Then
bVar = True
lngCount = oVar.Value + 1
Exit For
End If
Next oVar
If Not bVar Then lngCount = 1
oVars("varNum").Value = lngCount
UpdateAllFields
ActiveDocument.Save
lbl_Exit:
Exit Sub
End Sub
Private Sub UpdateAllFields()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
lbl_Exit:
Exit Sub
End Sub
A simple way to do this is to use http://www.gmayor.com/BookmarkandVariableEditor.htm See also http://www.gmayor.com/automatic_numbering_documents.htm for two other methods that may be adapted to your requirements.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Thanks a lot!
That solved my problem!
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Re-using document title, number, revision number, etc.
|
bwofficer | Word | 4 | 12-11-2014 05:21 AM |
| Counting unique visitors by ward, counting monthly visits by status, editing existing workbook | JaxV | Excel | 9 | 11-14-2014 12:25 AM |
Counting how many times a table or a field of a table is used in a document
|
moose5267 | Word Tables | 1 | 08-15-2014 01:41 AM |
Counting Number of Months from a static date
|
bremen22 | Excel | 4 | 11-25-2013 11:57 AM |
| Add increasing number at document opening | HidExp | Word | 6 | 09-17-2012 12:28 AM |