Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSW2010: Increment date in header section of page in MS word doc 1

Status
Not open for further replies.

paulpma

Programmer
May 5, 2014
2
US
Hello,

Thank you for reading my post. I know time is valuable thing when it comes to programing. I am not an expert but I know a bit about programming and currently stuck in a situation. I am trying to auto-increment date in header of Microsoft Word 2010 document for about 100 pages, pages could be less. Example:
Page1 Header: Sunday, May 4th 2014
Page2 Header: Monday, May 5th 2014
Page3 Header: Tuesday, May 6th 2014 etc. until given page.

The reason I want to put this date in the header section of a document, because this date is repeating. Also, if there is another way to place it automatically into every page with VB, that is also a solution for me.

Here is what I have for code:

CODE -->

Code:
Sub EditHeadersAndFooters()

Dim i As Long
Dim mBefore As Date


For i = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i)
       
   mBefore = Format(DateAdd("d", i, Date), "dd mmmm yyyy")
   
        .Headers(wdHeaderFooterPrimary).Range.Text = mBefore
        .Footers(wdHeaderFooterPrimary).Range.Text = foo
    End With
Next

End Sub

This code works but it does not auto-increment the date. From code you can see that I am not an expert. However, if you can help me in a way that I can use your solution I will reward you, if you have pay-pal account.

Thank you.
 
Your code works for me, so I maybe your section headers are linked, which would cause them all to be the same.

When you select a section's header, if it says "Same as Previous", then turn off the "Link to Previous" button. You'll need to check each section.
 
Unless there is an exact 1:1 correspondence between your Sections and pages, you cannot obtain differential dates in the header that way. Rather, you would need a complex field code. To see what's involvedm and how to do just about everything you might want to do with dates in Word, check out my Microsoft Word Date Calculation Tutorial, at:
or:
Do read the document's introductory material.

Cheers
Paul Edstein
[MS MVP - Word]
 
@ Larena:
Yes, you are right, this code works ONLY if sections are created and they are unlinked. Thank you. I didn't know that. However, I think this will not solve my 100+page document.

@ Marcopod
Thank you for your information. I have used filed codes before, however, they don't work for me. Here is an example taken directly from DateCalc_V2.86 file:
Code:
Calculate a Date Sequence
The following fields calculate a sequence of dates starting from today:
Wednesday, 7 May 2014
Saturday, 5 February 2011
Sunday, 6 February 2011
Monday, 7 February 2011
Tuesday, 8 February 2011
Wednesday, 9 February 2011

As you can see the first date is correct, however, calculated dates are in error? They should be plus one day. I have no idea why.
 
Did it occur to you to update the fields? being in the body of the document, they won't update automagically...

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top