PDA

View Full Version : Solved: Error using outlook automation in word



sandam
04-05-2005, 04:29 AM
I'm currently testing the usability of the global Word template thats to be implemented across the firm and I got this error (see screenshot) when testing the email function (see posted code). Any thoughts as to why this is happening and what I can do to go around it woudl be greatly appreciated.

Thanks in advance
Andrew;?


Sub DoEmail()
Dim OL As Object
Dim EmailItem As Object
Dim hLink As Range
Dim Doc As Document
Dim emailitem2 As Outlook.Application
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application") 'this line gives the error
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
FileNameForEmail = Doc.Name
FilePathForEmail = Doc.Path + Doc.Name
Doc.Save
With EmailItem
.Subject = "For your review"
.Importance = olImportanceNormal
.Display
Set hLink = ActiveDocument.Content
ActiveDocument.ActiveWindow.Activate
hLink.Select
Selection.TypeText Text:="Press <CTRL> + click on the link to review _
the document (" + FileNameForEmail + ")" + vbCrLf
hLink.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
FilePathForEmail, SubAddress:="", TextToDisplay:=FileNameForEmail
End With
Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub


forgot to add that all the office references have been added to the template in the VBE.

Killian
04-05-2005, 06:54 AM
Hiya,
You need to set a reference to the Outlook Object Library through VBE|Tool|References and it should be fine, no?
(without this line: Dim emailitem2 As Outlook.Application)

sandam
04-05-2005, 06:58 AM
Nope, references are set and that superfluous line has been eliminated with extreme prejudice. It just so happens that this error is on my bosses machine, he's helping me test for errors i might have missed. He restarted his Pc and is still getting the error, only itsw with more than one app so I'm guessing its actually his PC and not the code. I'll mark this one solved and move on.

Killian
04-05-2005, 07:09 AM
At the risk of stating the obvious - is he running the same version of Outlook you referenced? (or indeed any - e.g. Exchange won't cut it)

sandam
04-05-2005, 07:11 AM
yep. whole firm is on Office 2003 SP1 on XP SP1.

Killian
04-05-2005, 07:46 AM
Well that being the case you don't need to create the app as an object with that method. This will solve it (he says confidently... : pray2: )'declare the objects referencing the Outlook object library
Dim OL As New Outlook.Application
Dim EmailItem As MailItem
'don't need this now
'Set OL = CreateObject("Outlook.Application")

sandam
04-05-2005, 08:58 AM
it works for me but not for him. We have come to the stuning conclusion that his office install is corrupt which is why its not loading th ereferneces properly :). He's goign to reinstall and take it from there. :banghead: