locked
Word Macro Wont Run in Outlook RRS feed

  • Question

  • I have a macro that runs in word 2010 to remove double spacing between words.  I want to use this macro in Outlook 2010 but get a run time error 424 when I run it.
    Sub SingleSpace()
      Selection.HomeKey Unit:=wdStory
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      With Selection.Find
        .Text = "([ ]){2,}"
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
      End With
      Selection.Find.Execute replace:=wdReplaceAll
    End Sub
    What do I have to change for this to work in Outlook?
    Tuesday, March 15, 2011 8:23 PM

Answers

  • Hi,

    You need set a reference to the Microsoft Word X.0 Object Model in the
    References dialog within the VBA Editor.

    You may also need to alter the Word macro to set a reference to the document
    you are automating, and hook it into the event or action where you want it to
    run using the Outlook Object Model.

    There are some great resources on VBA programming in Outlook at this link:

    Visual Basic and VBA Coding in Microsoft Outlook:
    http://www.outlookcode.com/d/vb.htm

    Sincerely

    • Proposed as answer by Harry Yuan Monday, March 21, 2011 7:04 AM
    • Marked as answer by Harry Yuan Monday, March 21, 2011 7:04 AM
    Thursday, March 17, 2011 7:33 AM

All replies

  • Hi,

    You need set a reference to the Microsoft Word X.0 Object Model in the
    References dialog within the VBA Editor.

    You may also need to alter the Word macro to set a reference to the document
    you are automating, and hook it into the event or action where you want it to
    run using the Outlook Object Model.

    There are some great resources on VBA programming in Outlook at this link:

    Visual Basic and VBA Coding in Microsoft Outlook:
    http://www.outlookcode.com/d/vb.htm

    Sincerely

    • Proposed as answer by Harry Yuan Monday, March 21, 2011 7:04 AM
    • Marked as answer by Harry Yuan Monday, March 21, 2011 7:04 AM
    Thursday, March 17, 2011 7:33 AM
  • Hi Harry,

    Thanks for your reply, I haven't hada chance to look at the VBA coding yet but will.

    Wednesday, March 23, 2011 6:55 PM