position cursor in body

  • Thread starter Thread starter Nick Hill
  • Start date Start date
N

Nick Hill

Folks, I have created a snippet to insert a datestamp into the body of
task. This bit works fine, my question is this - how do I then place the
cursor at the end of the inserted text, is this possible?

Code as follows:

Sub Insert_Datestamp()
Dim objitem As Object
Set objitem = Application.ActiveExplorer.Selection.Item(1)
With objitem
.Body = Str$(Date) + vbTab + "-" + vbTab + vbNewLine + .Body
**NEED CODE HERE TO MOVE CURSOR TO END OF LINE**
End With
End Sub
 
Are you sure you want to do that for the currently *selected* , not the
currently *displayed* message (ActiveExplorer vs ActiveInspector)? Note that
if the message has any formatting, it will be lost as you are reading and
setting the plain text body.
For the currently selected message, there is really no such thing as the
current cursor postion. Even if you mean the preview pane, the cursor
position makes no sense as you cannot type in the preview pane. If you want
to modify the currently dislplayed message and position the cursor, you
might want to use the SafeInspector object exposed by the Redemption
library: http://www.dimastr.com/redemption/safeinspector.htm

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Thanks for the reply, firstly it is the body of currently open TASK item
I want to datestamp, not a mail.

Second, you are absolutely right - I want to insert the date into the
currently open task, not the currently selected one (although I should
imagine in most cases this will be the same thing). Basically, I want to
be able to open a task, click a toolbar button, or use an accelerator,
and insert into the top line todays date and a 'gap' so I can then enter
an update for that task. What I have so far is almost there (other than
the cursor position).

However, if the way I am doing it is not 'clean' I can only say it is my
first ever attempt at VB in Outlook, and I am definately open to
suggestions!!!

Cheers,
Nick
 
Nick,
vbend takes the cursor to the end of the selected line, but your current
code does not affect cursor position. I'm using Outlook 2000 and your
ActiveExplorer code didn't work for me. I think the code below does what you
need. Note that this code should only be executed from the task item, not
from the visual basic editor.

Sub Insert_Datestamp()
Dim objitem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set objitem = myOlApp.Inspectors.Item(1).CurrentItem
mylocation = InputBox("The cursor is currently located in the:" &
Chr(10) & "1: Subject" & Chr(10) & "2: Body")
'Tasks open with the cursor in the Subject area
With objitem
'Uncomment the line below if an input box is an acceptable way to
enter updates; otherwise, I think the code below does what you need.
'myupdate = InputBox("Enter update here.")
.Body = Str$(Date) + vbTab + "-" + vbTab + myupdate + vbNewLine +
..Body
' **NEED CODE HERE TO MOVE CURSOR TO END OF LINE**
'I assume you mean the end of the line just inserted instead of the
end of the body? "HERE" is at the end of the body.
'If you prefer to just be in the edit mode, use the addition below
End With
If mylocation = 1 Then
SendKeys "{tab 8}{end}" 'Do not run this from within Visual Basic Editor
or the keys sent will be sent to the editor, not the task. This takes the
cursor from the Subject area to the end of the first line of the body.
Else
SendKeys "^{home}{end}" 'This takes the cursor from anywhere in the body
area to the end of the first line of the body.
End If
End Sub
 
Correction, I'm using Outlook 2002, sp2

Nick Hill said:
Thanks for the reply, firstly it is the body of currently open TASK item
I want to datestamp, not a mail.

Second, you are absolutely right - I want to insert the date into the
currently open task, not the currently selected one (although I should
imagine in most cases this will be the same thing). Basically, I want to
be able to open a task, click a toolbar button, or use an accelerator,
and insert into the top line todays date and a 'gap' so I can then enter
an update for that task. What I have so far is almost there (other than
the cursor position).

However, if the way I am doing it is not 'clean' I can only say it is my
first ever attempt at VB in Outlook, and I am definately open to
suggestions!!!

Cheers,
Nick
 
Thanks, Dimitry, this is spot on.

I've chopped it about a bit, but kept in the msg box request - a much
cleaner way of inserting the update, and I don't need to worry where the
cursor is at all before or after the code executes

Thanks again, I now have exactly what I need - a quick and easy method to
update tasks.

Nick
 
Back
Top