Automatically calculate date of birth (1 Viewer)

Mylton

Member
Local time
Today, 05:30
Joined
Aug 3, 2019
Messages
123
Inglês
good morning
Could anyone help me create an age calculation in Word? I'm trying and catching a little.
I've already tried several formulas I saw on the internet, I've tried putting in an Excel table, but this loses the "function" of the calculation.
I attached a doc with a table.
I appreciate any tips.
Thanks.


Português - BR
bom dia
Alguém poderia me ajudar a criar um cálculo de idade no word? Estou tentando e apanhando um pouco.
Já tentei várias fórmulas que vi na internet, tentei colocar uma tabela de excel, só que esta perde a "função" do cálculo.
Anexei um doc com uma tabela.
Agradeço qualquer dica.
Obrigado.
 

Attachments

  • Birth date.zip
    10.4 KB · Views: 81

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 13:30
Joined
Jul 9, 2003
Messages
16,282
ChatGPT Said:-
Creating an age calculation in Word can be a bit tricky, as Word isn't designed for complex calculations like Excel. However, you can use a workaround by embedding an Excel sheet into your Word document. This allows you to use Excel's formula capabilities within Word. Here's a basic guide:
  1. Embed Excel in Word: In your Word document, go to the "Insert" tab, click on "Object", and then select "Microsoft Excel Worksheet". This will embed an Excel sheet into your Word document.
  2. Enter Your Data: In the embedded Excel sheet, you can enter birthdates and the current date.
  3. Use an Excel Formula for Age Calculation: In a cell next to your birthdate entry, use the formula =DATEDIF(A1, TODAY(), "Y") to calculate the age in years. Replace A1 with the cell reference that contains the birthdate.
  4. Update Age Automatically: The age should update automatically as the file is opened on different days, as the TODAY() function always uses the current date.
Remember, this method requires that the Word document viewer has Excel installed for the embedded worksheet to function correctly.
 

tvanstiphout

Active member
Local time
Today, 05:30
Joined
Jan 22, 2016
Messages
222
Yeah right, I'm going to have all of the overhead of Excel Automation just to run a few lines of VBA. ChatGPT, you still have a ways to go.

1699831341869.png
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 13:30
Joined
Jul 9, 2003
Messages
16,282
I schooled chatGPT with your solution and:-

ChatGPT replied to thus:-
The VBA method in the image you've provided is a good approach to calculating age in Word. The code is extracting the birth date from a table in the document, calculating the age using DateDiff, and then inserting the result back into the table. However, there is a missing part in the DateDiff function where the birth date should be compared to the current date.

Here's a revised version of the VBA code:

Code:
Option Explicit

Sub test1()
    Dim birthDate As Date
    Dim age As Integer
    Dim s As String

    ' Get the birth date from the table
    s = Trim(ActiveDocument.Tables(1).Columns(2).Cells(1).Range.Text)
    s = Left$(s, Len(s) - Len(vbCr)) ' Remove trailing vbCr
    birthDate = CDate(s) ' Convert string to date

    ' Calculate age
    age = DateDiff("yyyy", birthDate, Date) - IIf(Format(birthDate, "mmdd") > Format(Date, "mmdd"), 1, 0)

    ' Insert age back into the table
    ActiveDocument.Tables(1).Rows(2).Cells(2).Range.Text = age
End Sub

This code correctly adjusts for whether the current date has passed the birth date this year or not, which gives a more accurate age.
 

Mylton

Member
Local time
Today, 05:30
Joined
Aug 3, 2019
Messages
123
Inglês
good afternoon
I understand your point.
Is there no formula at all?
I would also like to thank you and everyone for your help

Português br
boa tarde
entendo a colocação dos senhores.
será que não existe uma formula qualquer?
no mais agradeço a ajuda dos senhores e de todos.
 

Users who are viewing this thread

Top Bottom