![]() |
|
#1
|
|||
|
|||
![]()
My company uses Word VBA to create templates for our customer service letters. In one of them I use a Date Picker to input the start date of a policy, but also need to calculate the age of that policy in years. I have tried use:
b = Format(Me.DTPStartDate.Value, "d MMMM yyyy") c = Format(Val(Date - Me.DTPStartDate.Value), "y") Where c is the age of the policy Any help would be much appreciated! |
#2
|
||||
|
||||
![]()
What kind of a Date Picker is this (e.g. content control, on a userform, etc.)? It might also help if you post a copy of the document (with the code - but remove anything sensitive). You can do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
The Date Picker is on a User Form.
Unfortunately, I am unable to post a copy of the document due to our company's IT polices. |
#4
|
||||
|
||||
![]()
Surely you can at least post a bare document containing a userform with whatever date-picker are you using? The standard Windows 7/Office 2010 set only includes the Calendar Control and I have no idea whether that's what you're referring to, something that's not supported in later Windows OSs (something you'll need to consider if anyone in your organisation uses Win 7 or 8), or a third-party product. Without knowing what you're actually using, so I can know what it returns, there's not much I can do to help ...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]() Quote:
I have tried another method to no avail... Quote:
|
#6
|
||||
|
||||
![]()
The function you need is most likely DateDiff. The attached demo, using a Calendar Control, should get you started. Considerably more code would be needed to return the number of year months and month days.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]() Quote:
I have tried to do this by Dim TheDate As Date Dim AgeofPlan As Date TheDate = DTPStartDate.Value AgeofPlan = DateDiff(yyyy, TheDate, Now) But keep getting a Run-time error '5': Invalid procedure call or argument on the last line. |
#8
|
||||
|
||||
![]()
You have:
Dim AgeofPlan As Date but you're passing 'DateDiff(yyyy, TheDate, Now)' to it, which is a number, not a date. To insert the content into the document, bookmark the destination, then use code like the attached.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]() Quote:
The destination is already bookmarked as 'Years1', I have no need for days or months being added, just the year (with no additional content), which will be inserted into the bookmark 'Years1' that is already in a sentence in the document, however, this is all dependent upon a series of logic arguments. Therefore, I need the age to be set as an expression in the same manner that I set the start date (b = Format(Me.DTPStartDate.Value, "d MMMM yyyy"), although in this case I want it to be "c"). |
#10
|
|||
|
|||
![]()
All sorted, it would appear the issue I was having was that I didn't use quotation marks around the interval arguement in the DateDiff...
My apologises, bit of a schoolboy error!!! |
#11
|
|||
|
|||
![]()
D'oh, I thought I'd got this sorted, but unfortunately, the calculation appears to be giving out the wrong age
![]() The line of code is: Code:
c = DateDiff("yyyy", DTPStartDate.Value, Date) Where
Does anyone have any ideas? |
#12
|
||||
|
||||
![]()
That's because your second date is in the 21st year after the first date. Try using:
Code:
c = DateDiff("yyyy", DTPStartDate.Value, Date) - 1
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
Thank you that has corrected the code.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Default dates for a Date Picker | BoringDavid | Word VBA | 2 | 09-11-2013 01:42 AM |
![]() |
Andy2011 | Word VBA | 4 | 11-24-2012 10:07 PM |
![]() |
nashville | Word | 16 | 04-06-2012 04:12 AM |
Date picker | trintukaz | Excel | 0 | 12-30-2011 12:42 AM |
![]() |
jamierbooth | Excel | 2 | 02-22-2011 03:35 AM |