|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
add new line to task bodyI 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 <vonclausow***@gmail.com> wrote in message
news:1138728190.306835.100200@z14g2000cwz.googlegroups.com... task.body = Format(Date, "YYYYMMDD") & vbCrLf & task.body> 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. 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 > Now I want to erase all these dates after two You two sentences seem to be contradictory. If you want to start fresh after> weeks and start fresh. Can this be done without > deleting the rest of the text in the body? 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 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
Show quote
Hide quote
> I think it is what i meant. If you have other text (besides those date strings), then the code I posted> 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. 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 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
Show quote
Hide quote
> it is exactly what I wrote earlier: Alright, give this code a try and tell me if it does what you want...> > 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. 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
Show quote
Hide quote
> Dim X As Long Whoops! I left my variable name in whereas I meant to substitute your> 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 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 Rick,
I'm trying to implement but can I put this in a seperate function? What do I need to add? Function AddDates(......... Marco > I'm trying to implement but can I put this in a This should do what you want...> seperate function? > What do I need to add? 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 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 > I don't think it is working correctly. I tested the code with the I'm not 100% sure I know what "working correctly" would be.<g>> following in the task. Show quoteHide quote > 20060125 My problem is I have to guess at the parts of what you want that you don't> 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!!! 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 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 <vonclausow***@gmail.com> wrote in message
news:1138744041.132995.315460@g44g2000cwa.googlegroups.com... - What kind of a task? A task in Outlook?> I have a task which is normally updated every day (but not > necessarily). - 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. 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
Show quote
Hide quote
> Sorry for being such an @.... Alright, give this function a try instead....> 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..... 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 Rick,
The code seems to be working great, thanks. I will test it for some days to be sure. Marco 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 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 Rick,
I get an 13 on this line: FirstDate = Format(Mid$(Task.Body, InStrRev(Task.Body, _ vbLf) + 1), "@@@@-@@-@@") type mismatch. Marco > I get an 13 on this line: The code I posted was copied directly from a working sample (although I used> > FirstDate = Format(Mid$(Task.Body, > InStrRev(Task.Body, _ > vbLf) + 1), "@@@@-@@-@@") > > > type mismatch. 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 |
|||||||||||||||||||||||