PDA

View Full Version : Move Cursor to Start of Body Text



Gronwold
01-26-2017, 10:29 AM
Hi,
I have written a short piece of VBA code for outlook using bits of code I have found on the internet and assigned it to a button on the toolbar, with the aim that when I want to update the task I click the button to run the code that then creates a timestamp, and a solid line to signify a new entry.
However, when I run it I would like to get the cursor to position itself at the start of the main text body for me to start typing. I have tried
objItem.Move wdstory, -1 but it doesn't work, hence turning it into a comment. Does anyone know how to modify this so that the cursor positions itself correctly so that when I click the button I can just start typing without having to click the start of the main text body?
Many thanks.
James

Sub StampDate()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objItem As Object
Dim strStamp As String
On Error Resume Next
Set objOL = Application
Set objItem = objOL.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
Set objNS = objOL.Session
strStamp = Now & " - " & objNS.CurrentUser.Name
objItem.Body = vbCrLf & vbCrLf & strStamp & vbCrLf & "____________________________________________________" & vbCrLf & objItem.Body
' objItem.Move wdstory, -1
End If
objItem.Body.Select
Set objOL = Nothing
Set objNS = Nothing
Set objItem = Nothing
End Sub

SamT
01-26-2017, 06:09 PM
I don't know anything about Outlook or Word, but wdstory, sure looks like a Word VBA constant to me.

gmayor
01-26-2017, 10:05 PM
Maybe


Sub StampDate()
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim objItem As Object
Dim strStamp As String

On Error Resume Next
Set objItem = CreateItem(olMailItem)
With objItem
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
.Display
strStamp = Now & " - " & Session.CurrentUser.Name
oRng.Text = vbCrLf & vbCrLf & strStamp & vbCrLf & "____________________________________________________" & vbCrLf
oRng.collapse 0
End With
Set objItem = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
End Sub

or if the message has been created and the cursor is in the message body


Sub StampDate2()
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim strStamp As String
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
Set olInsp = ActiveInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
strStamp = Now & " - " & Session.CurrentUser.Name
oRng.Text = vbCrLf & vbCrLf & strStamp & vbCrLf & "____________________________________________________" & vbCrLf
oRng.collapse 0
oRng.Select
End If
End If
lbl_Exit:
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
ErrHandler:
Beep
Resume lbl_Exit
End Sub

Gronwold
01-30-2017, 07:29 AM
Hi Graham,
Many thanks for this, but did not work. Can I check that the first one you suggested involves creating a task as well?
The second script kept the cursor where it was, so if you typed in a subject then ran the macro, the date stamp appeared in the main window (I think outlook refers to this as the body?) but the cursor remains in the subject window so continuing to type ends up modifying the subject.
Cheers,
james