Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Sep 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word: Days between two dates

    Hello!
    I am searching for a solution for my problem. I searched on google and did my best.
    Can anyone look at my VBA Code for a Action Button in Office Word! I need to calculate the days between two dates. I know its much easier in Excel but i need to do it in Word.

    Code:
    Private Sub CommandButton1_Click()
    Dim Adatum As Date, Bdatum As Date
    Dim differenz As Integer
    
    Adatum = Datum1
    Bdatum = Datum2
                            
    differenz = DateDiff("d", Adatum, Bdatum)
    ActiveDocument.FormFields("differenz").Result = differenz 'Format(differenz, "dd.MM.yyyy")
    
    
    Active.Document.FormFields
    End Sub
    I Bookmarked the dates with Datum1 and Datum2.
    When I click the ActionButton the Difference is always "0"

  2. #2
    Silver Lounger
    Join Date
    Mar 2014
    Location
    Forever West
    Posts
    2,072
    Thanks
    0
    Thanked 259 Times in 248 Posts
    I'm going to grasp at a straw, seeing the word differenz suggests the question of are you using an English version of Word but German language? I'm not that versed with VBA Code but was curious.

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Since you're using a formfield for the output, that suggests you might be inputting the two dates via formfields. In that case, you can calculate the difference without recourse to VBA. To see how to do this and just about everything else you might want to do with dates in Word, check out my Microsoft Word Date Calculation Tutorial, at:
    http://windowssecrets.com/forums/sho...ation-Tutorial
    In particular, look at the items titled Calculate the # Days Difference Between Two Dates', Calculate the # Years Months & Days Difference Between Two Dates and Date and Time Calculations In A Word Form. Do read the document's introductory material.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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