Home All Groups Group Topic Archive Search About

add new line to task body

Author
31 Jan 2006 5:23 PM
vonclausowitz
Hi All,

I need to add a line of text to the body of a task every day.
Everytime I update a task I want to add a line with the date of today
at the top:

20060128
20060129
20060130

etc.

However when I say task.body = Format(Date, "YYYYMMDD") I will only see
the latest added date but I want to keep all the dates so I can see on
which days
the task has been changed.

How should I proceed here?

Marco

Author
31 Jan 2006 5:32 PM
Jeff Johnson [MVP: VB]
<vonclausow***@gmail.com> wrote in message
news:1138728190.306835.100200@z14g2000cwz.googlegroups.com...

> However when I say task.body = Format(Date, "YYYYMMDD") I will only see
> the latest added date but I want to keep all the dates so I can see on
> which days
> the task has been changed.

task.body = Format(Date, "YYYYMMDD") & vbCrLf & task.body
Author
31 Jan 2006 5:36 PM
vonclausowitz
This is great, thanks Jeff.

Now I want to erase all these dates after two weeks and start fresh.
Can this be done without deleting the rest of the text in the body?

Marco
Author
31 Jan 2006 6:35 PM
Rick Rothstein [MVP - Visual Basic]
> Now I want to erase all these dates after two
> weeks and start fresh. Can this be done without
> deleting the rest of the text in the body?

You two sentences seem to be contradictory. If you want to start fresh after
two weeks, what "rest of the text in the body" will there be not to delete?
If you mean that you want to build the list up from a starting date,
continually adding new dates every day until there are 14 days in the list
and then, on the 15 day, list only today's date (with no other text in the
file), then try this code...

  Dim FirstDate As Date
  FirstDate = Format(Mid$(TaskBody, InStrRev(TaskBody, _
                     vbLf) + 1), "@@@@-@@-@@")
  If DateDiff("d", FirstDate, Date) < 14 Then
    TaskBody = Format(Date, "YYYYMMDD") & vbCrLf & TaskBody
  Else
    TaskBody = Format(Date, "YYYYMMDD")
  End If

If this is not what you meant, then you will have to describe your problem
in more detail.

Rick
Author
31 Jan 2006 7:00 PM
vonclausowitz
Rick,

I think it is what i meant.
You see we store some info in the task body, like comments, an url to a
website etc.
these must remain. Now on top we want to store the dates on which the
task has been edited. Like for example:

20060112
20060114
20060117
20060118

This is my url:
http://www.test.com

Finally after a period (say two weeks) we want to remove the dates and
start filling in the new dates. We don't want to keep the dates for
more then two weeks or so.

We use these dates to check for gaps in the task updates.

Marco
Author
31 Jan 2006 7:10 PM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
> I think it is what i meant.
> You see we store some info in the task body, like comments, an url to a
> website etc.
> these must remain. Now on top we want to store the dates on which the
> task has been edited. Like for example:
>
> 20060112
> 20060114
> 20060117
> 20060118
>
> This is my url:
> http://www.test.com
>
> Finally after a period (say two weeks) we want to remove the dates and
> start filling in the new dates. We don't want to keep the dates for
> more then two weeks or so.
>
> We use these dates to check for gaps in the task updates.

If you have other text (besides those date strings), then the code I posted
won't work (the other text will get in the way). Can you post a sample of
the text you are dealing with (preferably one in which there is a mix of
date strings and the other types of text in the file)? That may make it
easier to figure out a method for you.

Rick
Author
31 Jan 2006 7:15 PM
vonclausowitz
it is exactly what I wrote earlier:

for example:

-------------------- Task.body ----------------------------------
20060112
20060114
20060117
20060118

This is my url:
http://www.test.com

this is a comment which I want to keep.
--------------------------------------------------------------------

The problem is that the format we use is always different and depends
on what
someone wants to add to the body: an url or two or comments or else.
The only format is the dates on top.

Marco
Author
31 Jan 2006 7:58 PM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
> it is exactly what I wrote earlier:
>
> for example:
>
> -------------------- Task.body ----------------------------------
> 20060112
> 20060114
> 20060117
> 20060118
>
> This is my url:
> http://www.test.com
>
> this is a comment which I want to keep.
> --------------------------------------------------------------------
>
> The problem is that the format we use is always different and depends
> on what
> someone wants to add to the body: an url or two or comments or else.
> The only format is the dates on top.

Alright, give this code a try and tell me if it does what you want...

  Dim X As Long
  Dim FirstDate As Date
  Dim Records() As String
  Records = Split(TaskBody, vbCrLf)
  For X = UBound(Records) To 0 Step -1
    If Records(X) Like "########" Then
      FirstDate = Format(Records(X), "@@@@-@@-@@")
      Exit For
    End If
  Next
  If DateDiff("d", FirstDate, Date) >= 14 Then
    For X = 0 To UBound(Records)
      If Records(X) Like "########" Then Records(X) = "X\X/X"
    Next
    TaskBody = Join(Records, vbCrLf)
    Do While InStr(TaskBody, "X\X/X" & vbCrLf) > 0
      TaskBody = Replace$(TaskBody, "X\X/X" & vbCrLf, "")
    Loop
  End If
  TaskBody = Format(Date, "YYYYMMDD") & vbCrLf & TaskBody

Rick
Author
31 Jan 2006 8:04 PM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
>   Dim X As Long
>   Dim FirstDate As Date
>   Dim Records() As String
>   Records = Split(TaskBody, vbCrLf)
>   For X = UBound(Records) To 0 Step -1
>     If Records(X) Like "########" Then
>       FirstDate = Format(Records(X), "@@@@-@@-@@")
>       Exit For
>     End If
>   Next
>   If DateDiff("d", FirstDate, Date) >= 14 Then
>     For X = 0 To UBound(Records)
>       If Records(X) Like "########" Then Records(X) = "X\X/X"
>     Next
>     TaskBody = Join(Records, vbCrLf)
>     Do While InStr(TaskBody, "X\X/X" & vbCrLf) > 0
>       TaskBody = Replace$(TaskBody, "X\X/X" & vbCrLf, "")
>     Loop
>   End If
>   TaskBody = Format(Date, "YYYYMMDD") & vbCrLf & TaskBody

Whoops! I left my variable name in whereas I meant to substitute your
property value before posting. Here is the corrected code for you to try...

  Dim X As Long
  Dim FirstDate As Date
  Dim Records() As String
  Records = Split(Task.Body, vbCrLf)
  For X = UBound(Records) To 0 Step -1
    If Records(X) Like "########" Then
      FirstDate = Format(Records(X), "@@@@-@@-@@")
      Exit For
    End If
  Next
  If DateDiff("d", FirstDate, Date) >= 14 Then
    For X = 0 To UBound(Records)
      If Records(X) Like "########" Then Records(X) = "X\X/X"
    Next
    Task.Body = Join(Records, vbCrLf)
    Do While InStr(Task.Body, "X\X/X" & vbCrLf) > 0
      Task.Body = Replace$(Task.Body, "X\X/X" & vbCrLf, "")
    Loop
  End If
  Task.Body = Format(Date, "YYYYMMDD") & vbCrLf & Task.Body

Rick
Author
31 Jan 2006 8:30 PM
vonclausowitz
Rick,

I'm trying to implement but can I put this in a seperate function?
What do I need to add?

Function AddDates(.........

Marco
Author
31 Jan 2006 8:42 PM
Rick Rothstein [MVP - Visual Basic]
> I'm trying to implement but can I put this in a
> seperate function?
> What do I need to add?

This should do what you want...

Function AddDates(ByVal TaskBody As String) As String
  Dim X As Long
  Dim FirstDate As Date
  Dim Records() As String
  Records = Split(TaskBody, vbCrLf)
  For X = UBound(Records) To 0 Step -1
    If Records(X) Like "########" Then
      FirstDate = Format(Records(X), "@@@@-@@-@@")
      Exit For
    End If
  Next
  If DateDiff("d", FirstDate, Date) >= 14 Then
    For X = 0 To UBound(Records)
      If Records(X) Like "########" Then Records(X) = "X\X/X"
    Next
    TaskBody = Join(Records, vbCrLf)
    Do While InStr(TaskBody, "X\X/X" & vbCrLf) > 0
      TaskBody = Replace$(TaskBody, "X\X/X" & vbCrLf, "")
    Loop
  End If
  AddDates = Format(Date, "YYYYMMDD") & vbCrLf & TaskBody
End Function

Simply pass in your Task.Body property and the function will return the
correct text which, I'm guessing, you will want to reassign to your
Task.Body property.

Task.Body = AddDates(Task.Body)

Rick
Author
31 Jan 2006 9:05 PM
vonclausowitz
Rick,

I don't think it is working correctly. I tested the code with the
following in the task.

20060125
20060104
20060103
20060102

some other text......

I updated the task and ended up with:

20060131
20060131

So it removed all the other dates, although 20060125 is within 14 days.
and returned me the last date twice!!!

Marco
Author
31 Jan 2006 9:35 PM
Rick Rothstein [MVP - Visual Basic]
> I don't think it is working correctly. I tested the code with the
> following in the task.

I'm not 100% sure I know what "working correctly" would be.<g>

Show quoteHide quote
> 20060125
> 20060104
> 20060103
> 20060102
>
> some other text......
>
> I updated the task and ended up with:
>
> 20060131
> 20060131
>
> So it removed all the other dates, although 20060125 is within 14 days.
> and returned me the last date twice!!!

My problem is I have to guess at the parts of what you want that you don't
describe. For example, I don't know what Task is, but I am assuming it is a
"something" which has a Body property that is a String. I also don't know
how often the program will be run. Right now, if you run it twice on the
same day, today's date will be added twice. The reason it removed all dates
(even though 20060125 is within 14 days) is because you originally asked

     "Now I want to erase all these dates after two
      weeks and start fresh. Can this be done without
      deleting the rest of the text in the body?"

So I design the code to do that... after two weeks, erase all of the dates
but leave the non-date stuff. I'm guessing, now, that I misinterpreted what
you wrote. Can you explain, in detail, what you are looking for? Don't leave
anything out... remember, you know what you want, so it may seem obvious to
you, but we have no idea of what your situation is, what its needs are or
how you need to proceed when interacting with it... so, you have to tell us.

Rick
Author
31 Jan 2006 9:47 PM
vonclausowitz
Rick,

Sorry for being such an @....
I should have been more clear. So I will try to explain (as a
non-native speaker I am).

I have a task which is normally updated every day (but not
necessarily).
When I update a task a date should be entered in the body like we saw:
20060131.
There can be gaps in these dates. When we don't update the task no date
is entered.
That is no problem.
To keep the amount of dates in the body to a minimum but also because
I'm not interested
in dates older than 14 days I want to get rid of a date when it is
older than 14 days.

So the code should, when an update is done, check all the dates in the
body and when it is older than 14 days remove it, for the rest leave
the younger dates in and add the new date to it.....

That's about it.

Marco
Author
31 Jan 2006 9:59 PM
Jeff Johnson [MVP: VB]
<vonclausow***@gmail.com> wrote in message
news:1138744041.132995.315460@g44g2000cwa.googlegroups.com...

> I have a task which is normally updated every day (but not
> necessarily).

- What kind of a task? A task in Outlook?

- What does it mean that you "update" the task?

- Does the body of the task contain anything else besides these "last
updated" dates? If so, do you want that other information preserved?

We (mainly Rick) are really working in the dark here.
Author
31 Jan 2006 10:04 PM
vonclausowitz
Jeff,

Yes they are Outlook Tasks.

Updating means that we Complete the task and send it to reaccure the
next day.

Yes the body contains some other text like an Url or some comments
someone made about the task and this should be preserved.

Marco
Author
31 Jan 2006 10:42 PM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
> Sorry for being such an @....
> I should have been more clear. So I will try to explain (as a
> non-native speaker I am).
>
> I have a task which is normally updated every day (but not
> necessarily).
> When I update a task a date should be entered in the body like we saw:
> 20060131.
> There can be gaps in these dates. When we don't update the task no date
> is entered.
> That is no problem.
> To keep the amount of dates in the body to a minimum but also because
> I'm not interested
> in dates older than 14 days I want to get rid of a date when it is
> older than 14 days.
>
> So the code should, when an update is done, check all the dates in the
> body and when it is older than 14 days remove it, for the rest leave
> the younger dates in and add the new date to it.....

Alright, give this function a try instead....

Function AddDates(ByVal TaskBody As String) As String
  Dim X As Long
  Dim FirstDate As Date
  Dim RecordDate As Date
  Dim Records() As String
  Records = Split(TaskBody, vbCrLf)
  For X = 0 To UBound(Records)
    If Records(X) Like "########" Then
      RecordDate = Format(Records(X), "@@@@-@@-@@")
      If DateDiff("d", RecordDate, Date) >= 14 Then
        Records(X) = "X\X/X"
      End If
    End If
  Next
  TaskBody = Join(Records, vbCrLf)
  Do While InStr(TaskBody, "X\X/X" & vbCrLf) > 0
    TaskBody = Replace$(TaskBody, "X\X/X" & vbCrLf, "")
  Loop
  AddDates = Format(Date, "YYYYMMDD") & vbCrLf & TaskBody
End Function

It will still add the date more than once if executed more than once per
day. If you want that taken out, let me know.

Rick
Author
1 Feb 2006 6:20 PM
vonclausowitz
Rick,

The code seems to be working great, thanks.
I will test it for some days to be sure.

Marco
Author
31 Jan 2006 9:34 PM
vonclausowitz
Rick,

Forget about the two same dates at the end.
The only problem is that the code also deletes dates within the range
of the 14 days.

Maybe it has something to do with the order of the dates:
from the latest on top until the oldest at the bottom:

20060125
20060104
20060103
20060102

Marco
Author
8 Feb 2006 8:32 PM
vonclausowitz
Rick,

A follow up.
It seems that when a task gets updated and text is found in the first
line from the top then two extra lines are inserted.
For example:

----------
20060209
20060208

Can I change the function you gave me so that it will start at line
three?

Marco
Author
31 Jan 2006 7:12 PM
vonclausowitz
Rick,

I get an 13 on this line:

                            FirstDate = Format(Mid$(Task.Body,
InStrRev(Task.Body, _
                                        vbLf) + 1), "@@@@-@@-@@")


type mismatch.

Marco
Author
31 Jan 2006 7:22 PM
Rick Rothstein [MVP - Visual Basic]
> I get an 13 on this line:
>
>                             FirstDate = Format(Mid$(Task.Body,
> InStrRev(Task.Body, _
>                                         vbLf) + 1), "@@@@-@@-@@")
>
>
> type mismatch.

The code I posted was copied directly from a working sample (although I used
a variable named TaskBody in place of your Task.Body property). Of course,
the contents of that variable were ONLY dates without comments or URLs etc.
That is probably the cause of the problem (as discussed in the other
subthread). Let me see what I can dummy up for you (I'll post it in the
other sub-thread when I devise something).

Just to be sure we are on the same page though, what version of VB are you
using?

Rick
Author
31 Jan 2006 8:45 PM
vonclausowitz
6.0

Marco