Log in

View Full Version : How to move the cursor?



Pampel
03-22-2017, 12:13 AM
Hello to everyone,

in a MS-Access 2010 application I have to send information with an email. I create this email with


Private Sub btnMail2_Click()
Dim myMail As Outlook.MailItem
Dim myOutlApp As Outlook.Application

Set myOutlApp = New Outlook.Application
Set myMail = myOutlApp.CreateItem(olMailItem)

With myMail
.To = Me!email
.Subject = "Betreff"
.Body = "Body"
.Display
End With

Set myMail = Nothing
Set myOutlApp = Nothing
End Sub

Works fine!!! But I want to place the cursor to a certain line in the body text so that the user may easily insert additional information by himself.

I recorded an VBA-sequence (in German: Makro, I don't know the english expression for that) in MS-Word to find out how the cursor is controlled in Word. In Word

Selection.MoveDown Unit:=wdLine, Count:=4
moves the cursor down four lines.

But when I insert this code in my MS-Access application the debugger highlights wdLine and tells me that this variable is not declared.

How can I control the cursor in an MS-Outlook email, that was created by the code shown above?


Many thanks for any hints to solve this VBA problem,


Stefan

gmayor
03-22-2017, 03:55 AM
Outlook is a bit fussy to program from other applications however see the comments in line


Private Sub btnMail2_Click()
Dim myMail As Object
Dim myOutlApp As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object
'Use the function from Ben Clothier - http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'To open or create the Outlook application or the process will not work as you require
Set myOutlApp = OutlookApp() 'calls Ben Clothier's code
Set myMail = myOutlApp.CreateItem(0) 'Create a new mail item
With myMail
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor 'get the Word document inspector to edit the message body
Set oRng = wdDoc.Range ' Set a range to the document body
.To = Me!EMail
.Subject = "Betreff"
oRng.Collapse 1 'Collapse the range to its start i.e the message start
'(the only other thing in the message at this point should be the default signature'
'Add text from code
'oRng.Text = "Some message text"
'oRng.Collapse 0 'collapse the range to its end
oRng.Select 'type here as required
.Display
End With

Set myMail = Nothing
Set myOutlApp = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
End Sub

Pampel
03-22-2017, 06:04 AM
Thanks a lot, Graham - I'll give it a try

Pampel
03-24-2017, 10:00 AM
Hello again,



...
Set myOutlApp = OutlookApp() 'calls Ben Clothier's code
...


debugger stopps code and heighlights OutlookApp(). There is an error message (hope I translate it correctly)

Error when compiling: Sub or function not declared.

So unfortunately your code doesn't work.

Do you have any idea why?

Stefan

gmayor
03-24-2017, 09:47 PM
Did you add the code from the web site named in the macro that provides the function to open Outlook - the function which is causing the error because it is missing?
The attached zip contains a VBA module with the required code. Extract and import it into your VBA editor. The macro should then work.