Insert text at end of journal entry

C

Charles Kenyon

I would like to insert the text at the end of a journal entry. Specifically,
I am trying to add, on a new line:

Call returned at (date) (time) inserting the date and time.

Which vba methods, objects and properties should I be using here?

I am brand new to Outlook programming but have done quite a bit with Word
vba.

TIA

--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charles Kenyon

Thank you for the pointer. I inserted the following code and it works but
it flashes the following message (twice):

Message:
A program is trying to access e-mail addresses that you have stored in
Outlook. Do you want to allow this? ...

When I click to allow (twice) it runs and inserts what I want.

Code:
Sub CallBack()
' code adapted from http://www.outlookcode.com/d/code/stampdate.htm
' 14 February 2005
'
Dim objApp As Application
Dim objItem As Object
Dim objNS As NameSpace

Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objItem = objApp.ActiveInspector.CurrentItem
If objItem.Class = olJournal Then
objItem.Body = objItem.Body & vbCrLf _
& "Returned call at " & Now() _
& " - " & objNS.CurrentUser
End If

Set objItem = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Sub
 
S

Sue Mosher [MVP-Outlook]

What Outlook version are you using? If it's Outlook 2003, replace this
statement:

Set objApp = CreateObject("Outlook.Application")

with

Set objApp = Application

so that you can use the intrinsic Application object, which will not trigger
security prompts when you access the value of the Body property.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Charles Kenyon

Thank you. Yes, it is OL 2003 and the change works well.
--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
G

Guest

Hi Charles,

Found your post while trying to research the same problem. I wanted to
simply insert the date/time in the current Outlook item at the current cursor
position and then move the cursor to the beginning of the next line ready to
type.

Having a programming background, I thought the task should be easy but like
everyone else here, I was also ending up with something far more complex than
it was worth. After studying you initial code and the replies, I finally came
up with the following:

Sub InsertDateTime()
SendKeys Format(Now, "m/d/yyyy h:nn a/p") & "INSERT YOUR STUFF HERE"
SendKeys "{ENTER}" ' moves cursor to next line
End Sub

Besides being much simpler, this method is superior to the ".body" method
suggested because with that method you cannot insert what you want at the
current cursor position. Your only choices are at the top and at the bottom.

My method also allows it to be used on ANY form that is open and not just in
the body but in any appropriate field such as the Subject line. The insert
goes at the current cursor position no matter where that might be.

But be aware that this code MUST be executed ONLY from an open item. If you
simply "Run" it in the the VB Editor, your SendKeys will be going there and
not to the form where you want it. So once the code is pasted into the VB
Editor, then assign it as a macro to a toolbar button on each form where you
wish to use it. For example, put a macro button on toolbars in a:
Contact form
Appointment form
Journal Entry form
Task form
and so on. If you execute it at an inappropriate place, there will be an
error beep.

Then open a form and move the cursor where you want the date/time inserted.
Then click on the new custom button, and voila', you have your date/time
inserted.

Of course the code can be further enhanced by including such things as your
name, etc. Tag anything else into additional SendKeys statements at your
heart's content.

Hope this helps,
Bill
 
G

Guest

Hey Bill,

Is there a way to programmatically put the caret into the body of an outlook
item? I don't want to rely on the user positioning the caret into the body
because they probably won't.
 
G

Guest

Hi John,

I am not sure what the significance of the "caret" is. But any character can
be inserted using the same method I used.

SendKeys "INSERT YOUR STUFF HERE"

At the place above where it says, "INSERT YOUR STUFF HERE", insert anything
you can type directly within the quotes. For those characters that cannot be
typed from the keyboard, use the function, CHR(nnn) where "nnn" is replaced
by the ASCII character code.

SendKeys "INSERT YOUR STUFF HERE" & CHR(nnn)

For a list of the ASCII codes, search help for the CHR() function and then
from there click on the "See Also" link at the top left corner of the page.
There are 2 pages of codes--0-127 and 128-255. The ASCII code for the caret,
"^" is CHR(94) but it is not necessary to use the CHR() function for that.
Just type it in from the keyboard within quotes.

The limitation to my method is that your text will ONLY be inserted exactly
where the cursor is currently located. I know of no EAST method of being more
specific as to the location. But there is a much more complex method. That
would be to set a varaiable to contain the full text in the field using code
similar to this:

With objContact
TextToChange = .body
' use the InStr() function here to search through TextToChange
' to find where you want it, and then
' insert your text using a combination of MID() functions
.body = ChangedText
End With

But this method requires a fair amount of programming experience and, more
specifically, some advanced use of string manipulation. I feel that would be
beyond the scope of this forum.

But I hope this will at least get you started in the right direction. Good
luck!

Bill Molony
 
G

Guest

Bill, by caret I mean the cursor in the text field vs the mouse cursor. The
problem I am having is if the caret is in the subject field then the text
gets inserted in the subject field, not the body. I want the text to be
inserted in the body, but not have the user have to click in the body first.
 
G

Guest

Hi John,

This is not a very elegant way of doing it. I guess if I had more time, I
might find a better way but at least this works.

This assumes that the user has a standard Appointment form open and has not
yet moved the cursor out of the Subject field. The user can use the tab key
to move between fields. So this coder just sends enough tab keystrokes (in
this case 12) to move it down to the beginning of the Body field.

SendKeys "{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}"

Note that the entire string of {TAB}'s are enclosed within a set of quotes.
I found the number of them to use by opening an Appt form and simply tabbing
through it till I got to the Body.

But this will only work with a specific form--in this case the Appt form.
Although I have not taken the time to look it up right now, I am sure there
are ways to test as to which type of form is open--Appt, Contact, etc. Then
modify the number of {TAB}'s sent accordingly to move the cursor to the field
you want it in.

Hope this helps.

Bill
 
M

Michael Bauer

Hi John,

please see my example in the thread "How to set focus on the MailtItem's
Body", from 01/26/2005 in this NG.
 
G

Guest

Hi Michael,
Tried your module in OL2003 with an Appointment item. Did not work.
The sub GetBodyHandle() fails to return a handle.
I could not determing why.
Bill
 
M

Michael Bauer

Hi Bill,

the sample was written for OL2k. Obvioulsy the windows structure and/or
some class names are different from OL 2003. Sorry, but currently I
haven´t the time for modifying the sample.
 
G

Guest

Hi Michael,
I can surely sympathize with that all too well! :)
Never enough hours.
But thanks for your input!
Be well,
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top