Automatically calculate date of birth

Mylton

Member
Local time
Yesterday, 17:43
Joined
Aug 3, 2019
Messages
124
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

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.
 
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
 
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.
 
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

Back
Top Bottom