Results 1 to 5 of 5

Thread: Code for dates

  1. #1
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    491
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Hi
    I am trying to generate a quarterly report that must sow the current date and the "Applicable Quarter". I have Used the CurrentDate field as a reference and the macrto looks fro 2 DocVariable fileds, Date1 & Date2, and inserts the month and year. Can't the life of me figger out why the macro will not work.
    Seems pretty basic, but???

    Sub DateAdd()
    Dim myDate As Date
    Dim myRng As Range

    'Set the starting date with the value of a field
    Set myDate = ActiveDocument.Fields(4).Result
    myDate = myRng

    With ActiveDocument.Variables
    .Item("Date1").Value = Format(myDate - 90, "MMMM yyyy")
    .Item("Date2").Value = Format(myDate - 30, "MMMM yyyy")
    End With
    ActiveDocument.Fields.Update
    End Sub
    cheers

    Phil Carter

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,477
    Thanks
    211
    Thanked 849 Times in 781 Posts
    Bonriki,

    I'm not up on Word VBA, Excell & Access are my bag, but I'd venture a guess that you problem is myDate = myRng.
    First myRng isn't initialized, i.e. ISNothing and secondly assigning objects of different types to each other is a NO-NO.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Can you post a document containing the relevant fields and code? It's hard to follow from the code alone. Please clear out any sensitive/confidential content before posting.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,978
    Thanks
    0
    Thanked 208 Times in 189 Posts
    Hi Bonriki,

    You can, of course, do these calculations without vba. To see how, check out my Date calc tutorial: http://lounge.windowssecrets.com/ind...owtopic=249902
    In particular, look at the item 'Calculate a month and year, using n months delay'. For your purposes, you could use two copies of the field there, one with 'Delay' = -1 and the other with 'Delay' = -3.

    On the coding front, I believe you could achieve what you're after with something along the lines of:
    Code:
    Sub DateAdd()
    Dim myDate As Date
    With ActiveDocument
      'Set the starting date with the value of a field
      myDate = CDate(.Fields(4).Result)
    
      With .Variables
        .Item("Date1").Value = Format(myDate - 90, "MMMM yyyy")
        .Item("Date2").Value = Format(myDate - 30, "MMMM yyyy")
      End With
      .Fields.Update
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    491
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Jefferson

    That's great

    Thank you
    cheers

    Phil Carter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •