Consulting

Results 1 to 7 of 7

Thread: Solved: Error using outlook automation in word

  1. #1
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    Solved: Error using outlook automation in word

    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;?

    [vba]
    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
    [/vba]

    forgot to add that all the office references have been added to the template in the VBE.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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)
    K :-)

  3. #3
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    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.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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)
    K :-)

  5. #5
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    yep. whole firm is on Office 2003 SP1 on XP SP1.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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... )[VBA]'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")
    [/VBA]
    K :-)

  7. #7
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    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.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •