Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2015, 01:51 AM
sannhei sannhei is offline Counting the number of opening of a document Windows 8 Counting the number of opening of a document Office 2013
Novice
Counting the number of opening of a document
 
Join Date: Mar 2015
Posts: 7
sannhei is on a distinguished road
Default Counting the number of opening of a document

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?
Reply With Quote
  #2  
Old 03-27-2015, 05:23 AM
gmayor's Avatar
gmayor gmayor is offline Counting the number of opening of a document Windows 7 64bit Counting the number of opening of a document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
If you need to change the number set the value of the document variable 'varNum' as required.

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
Reply With Quote
  #3  
Old 03-27-2015, 05:40 AM
sannhei sannhei is offline Counting the number of opening of a document Windows 8 Counting the number of opening of a document Office 2013
Novice
Counting the number of opening of a document
 
Join Date: Mar 2015
Posts: 7
sannhei is on a distinguished road
Thumbs up Thanks

Thanks a lot!

That solved my problem!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting the number of opening of a document 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 the number of opening of a document 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 the number of opening of a document 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft