Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can Word Subtract 2 dates and leave an Integer 1

Status
Not open for further replies.

BAWC01

Technical User
Nov 23, 2005
79
CH
Is it possible for Word to Subtract two dates and leave an integer

Phil
 

Yes. What integer would you like? I can offer an infinite variety of them.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony

Just want to give the difference between two dates in a table

I have a table with a date the job was started and field with the number of days the job has been running.

When the Word document is opened I want the current date as the reference and all total days running to be updated

Can this be done.

Phil
 
Hi Phil,

If you are happy to have VBA code it would be, by far, the easier option. If not, it can be done with Word Fields, but not very easily. Even with fields, to get them to 'automatically' update needs code. So, is code alright?

Assuming the date the job started is just text, what format is it in? And is it the only text in the cell? And is there one in every row in the table - or does it have header rows or blank rows or (I hope not) other data rows in a different format?


Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony

I'm proficiant in Microsoft Access VBA, so I will give code code a go.

each row has an entry and the dsate can either be a string or date.

Can you give me an example

Thanks for your help

Phil
 
Hi Phil,

Not sure what you mean by a date (if it's a Word Field this code may not work) - but assuming this is the first table in the document and it has two columns and your date is text in the first column then put this code in the ThisDocument module of your document
Code:
Private Sub Document_Open()
    Dim StartDate As Date
    Dim WorkRow As Row
    Dim CellText As String
    For Each WorkRow In Me.Tables(1).Rows
        CellText = WorkRow.Cells(1).Range.Text
        On Error Resume Next
        StartDate = CDate(Left$(CellText, Len(CellText) - 2))
        If Err.Number = 0 Then
            WorkRow.Cells(2).Range.Text = CStr(Date - StartDate)
        End If
    Next
End Sub
You will have to change cell and/or table reference numbers if your setup isn't quite like this.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top