View Single Post
  #40   Report Post  
Posted to microsoft.public.office.developer.outlook.vba,microsoft.public.outlook,microsoft.public.word.newusers,microsoft.public.word.vba.general
Sue Mosher [MVP-Outlook] Sue Mosher [MVP-Outlook] is offline
external usenet poster
 
Posts: 6
Default How to get to email from Word

It would be helpful if you would step through the code and pinpoint the issue more exactly.

Also, I would suggest a bit more precision in setting where to paste the copied content:

oItem.Display
Set objDoc = oItem.GetInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Paste


--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Graham Mayor" wrote in message ...
It just crashes without any vba error message - just the Word has
encountered an error and needs to close message, followed by the fault
reporting screen. Word then restarts.

The error occurs after the prompt for the Subject so presumably the fault
lies at

Selection.Copy
objDoc.Range.Paste
.Display

On the few occasions when it doesn't crash, the selected formatted text is
not pasted into the message window.

When Outlook is already running in the background, the macro works as
intended in both Word 2003 and 2007 (both with Outlook 2007).

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



Sue Mosher [MVP-Outlook] wrote:
Which statement causes the crash? Error messages?


"Graham Mayor" wrote in message
...
Sue

The suggestion set the little cogs in motion

The following now does work to paste the formatted text into the
body of the message, and I have added a routine to grab the
addressee information from Outlook. However while it does work when
Outlook is running already, it usually crashes Word when Outlook is
supposed to be started from the macro.

Sub Send_Extract_As_EMail()
' send the document in an Outlook Email message
' 2007 Graham Mayor, Tony Jollans, Doug Robbins
' & Sue Mosher

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim objDoc As Word.Document
Dim strEMail As String

strEMail = "PR_EMAIL_ADDRESS"
'Let the user choose the contact from Outlook
'And assign the email address to a variable

strEMail = Application.GetAddress("", strEMail, _
False, 1, , , True, True)
If strEMail = "" Then
MsgBox "User cancelled or no address listed", , "Cancel"
End If

On Error Resume Next

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, start it from code
If Err 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
Set objDoc = oItem.GetInspector.WordEditor
With oItem
.to = strEMail
.Subject = InputBox("Subject?")
Selection.Copy
objDoc.Range.Paste
.Display
End With

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub



Sue Mosher [MVP-Outlook] wrote:
Yes, it should have been Set not Dim. Sorry for the confusion.


"Graham Mayor" wrote in message
...
Thanks for that. I assume the second 'Dim' was a typo?


Sue Mosher [MVP-Outlook] wrote:
You might find the recent discussion at
http://www.outlookcode.com/threads.a...essageid=26382
useful, as it was on a similar subject.

I don't know if it is a clue, but if I enter
.GetInspector
and then a period vba prompts with the options - including
.WordEditor
If I add a period to the end of that, there isn't the usual
prompt offering .Range etc (though it doesn't baulk at its
addition).

Declare a Word.Document object and instantiate it:

Dim objDoc as Word.Document
Dim objDoc = MyMessage.GetInspector.WordEditor

You'll then get intellisense for objDoc.




"Graham Mayor" wrote in message
...
The only thing I do in Outlook is extract a line of code from a
daily e-mail and paste it into a Word table, using a variation of
the code we discussed on an earlier occasion.

I don't appear to be able to do anything in code with the object.

I don't get an OMG (?) prompt or any other prompt or error
message. The Outlook message window opens the addressee and
subject are filled and (if nothing is entered in the ".Body ="
line) the default theme is used. The cursor is in the body area
and nothing is pasted. Pressing CTRL V or clicking the Paste
button pastes the formatted text.

I don't know if it is a clue, but if I enter
.GetInspector
and then a period vba prompts with the options - including
.WordEditor
If I add a period to the end of that, there isn't the usual
prompt offering .Range etc (though it doesn't baulk at its
addition).

It doesn't make any difference whether or not Outlook is running.