We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Create or import macros into Outlook 2007

Medium Priority
2,120 Views
Last Modified: 2012-05-06
I used the learn macro function in MS Word to create two macros: I assign a key or ribbon icon to "double indent" which means that after I highlight a paragraph, the macro  causes the text to be single space, and indented 2" on both the left and right borders.  I use this to insert quotes into my papers. I have a second macro that returns to normal by making the spacing double, and restoring the paragraph to 0" indent on the left and right.

I want to import these two macros into Outlook 2007, or alternatively crerate them in Outlook 2007. However, I have not been able to use  or understand the macro editior in Outlook or to create any macro that works.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
Hi, focusers.

Macros and how they work is completely different in Outlook than in other Office products, especially Word and Excel.  Outlook does not have the ability to record macros like those two products do.  Outlook macros have to be written from scratch using VBA.  I'd have to see one of these Word macros to be sure, but I'm inclined to think that it probably won't work in Outlook at all.  If you want to post one of them here, then I'll be glad to have a look and let you know.  Since Outlook 2007 uses Word for it's editor, there might be a way to call the Word macro from Outlook, but I'm not certain of that.  

The macro editor, really VBA editor, in Outlook is a combination code editor and debugger.  Writing code is simple enough if you are comfortable with VBA (Microsoft Visual Basic for Applications) and have some knowledge of Outlook's object model.

Author

Commented:
Blue:

The code for the  two macros I use in Word 2007 (that I want to use in Outlook 2007) is as follows (one is called "dbindt" --for double indenting a paragraph I had highlighted-- and the other is called "Normal" --for returning the page to its original status):

Sub dbindt()
'
' dbindt Macro
'
'
    With Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(1.5)
        .RightIndent = InchesToPoints(1.5)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 10
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub
Sub normal()
'
' normal Macro
'
'
    With Selection.Font
        .Name = "Calibri"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(0)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 10
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub


My goal is to have these work in Outlook 2007 as imported macros or re-written for Outlook 2007.

Thanks.

(Should I raise the difficulty point level of this question?)
CERTIFIED EXPERT
Top Expert 2010

Commented:
Here's they are revised to work from Outlook.  Follow these instructions to use them.

1. Start Outlook.
2. Click Tools > Macro > Visual Basic Editor.
3. If not already expanded, expand Modules and click on Module1.  If Module1 doesn't exist, then create it.
4. Copy the code below and paste it into the right-hand pane of the VB Editor.
5. Edit the code as needed. I placed comments where things must/should be edited.
6.  Click Tools > References
7.  Scroll down through the list of references until you find Microsoft Word 12.0 Object model.  Check the box next to it and click OK.
8. Click the diskette icon on the toolbar to save the changes.
9. Close the VB Editor.

Open a message and selected some text.  Run one of the macros.

The point value of the question is up to you.
Sub dbindt()
    Dim olkMsg As Outlook.MailItem, _
        olkDoc As Object, _
        wrdSelection As Object
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set olkDoc = Application.ActiveInspector.WordEditor
    Set wrdSelection = olkDoc.Application.Selection
    With wrdSelection.ParagraphFormat
        .LeftIndent = olkDoc.Application.InchesToPoints(1.5)
        .RightIndent = olkDoc.Application.InchesToPoints(1.5)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 10
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = olkDoc.Application.LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = olkDoc.Application.InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub
 
Sub normal()
    Dim olkMsg As Outlook.MailItem, _
        olkDoc As Object, _
        wrdSelection As Object
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set olkDoc = Application.ActiveInspector.WordEditor
    Set wrdSelection = olkDoc.Application.Selection
    With wrdSelection.Font
        .Name = "Calibri"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .Strikethrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .Smallcaps = False
        .Allcaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With wrdSelection.ParagraphFormat
        .LeftIndent = olkDoc.Application.InchesToPoints(0)
        .RightIndent = olkDoc.Application.InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 10
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = olkDoc.Application.LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = olkDoc.Application.InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub

Open in new window

Author

Commented:
Blue,
Thank you for the code, I really do appreciate it, and I am sorry I did not make this a 500 point question. If I can change it, tell me how, and I will.

We are almost there. There are two problems: On each of the macros, I get the following error message-

                                 Microsoft Visual Basic
                                 ---------------------------
                                Runtime error '5148';
                                The number must be between 1 and 1o

                               [END]               [DEBUG]                 [HELP]


Then the  VB editior opens automatically . When I click "END" and the macro works.

How do I get rid of  this error.

The second issue is that in the double indent macro (dbindt) I need the line spacing to be single space.

Thanks again.
CERTIFIED EXPERT
Top Expert 2010

Commented:
What line are you taken to in the debugger?

Author

Commented:
In the "dbindt" macro, the line says".OutlineLevel = wdOutlineLevelBodyText". (See, below)

Sub dbleindt()
Dim olkMsg As Outlook.MailItem, _
olkDoc As Object, _
wrdSelection As Object
Set olkMsg = Application.ActiveInspector.CurrentItem
Set olkDoc = Application.ActiveInspector.WordEditor
Set wrdSelection = olkDoc.Application.Selection
With wrdSelection.ParagraphFormat
.LeftIndent = olkDoc.Application.InchesToPoints(1.5)
.RightIndent = olkDoc.Application.InchesToPoints(1.5)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 10
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = olkDoc.Application.LinesToPoints(1.15)
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = olkDoc.Application.InchesToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
 
--------------------------------------------
In the "Normal" macro, the lnie says:. "OutlineLevel = wdOutlineLevelBodyText"  as shown below;
 

Sub normal()
    Dim olkMsg As Outlook.MailItem, _
        olkDoc As Object, _
        wrdSelection As Object
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set olkDoc = Application.ActiveInspector.WordEditor
    Set wrdSelection = olkDoc.Application.Selection
    With wrdSelection.Font
        .Name = "Calibri"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .Strikethrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .Smallcaps = False
        .Allcaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With wrdSelection.ParagraphFormat
        .LeftIndent = olkDoc.Application.InchesToPoints(0)
        .RightIndent = olkDoc.Application.InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 10
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = olkDoc.Application.LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = olkDoc.Application.InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
End Sub

  I hope that I gave you what you need.
 
thaks.
CERTIFIED EXPERT
Top Expert 2010

Commented:
I just ran both macros and did not get any errors.  Do you have any other code in Outlook?  wdOutlineLevelBodyText is a constant with a value of 10.  The only reason I can think of that you'd get an error when I don't is if there is other code in Outlook that's changing the declaration of that constant name.

Author

Commented:
Blue,

In Macros, I ahve just the two you wrote.

Where can I look to see if there is the other code you suspect?

is there a workaround/ I mean, is there a substitute for wdOutlineLevelBodyText that m Outlook might accept?

Also, how do I get dbindt to go to single line space instead of double line space?

Author

Commented:
Blue:

I deleted the two offending items of code--and SUCCESS!

Now I need to impose on you to change the code in dbindt  so it does not double space the text. Since I use this to quote  paragraphs, I need the quote in single space.

So far, so good.

thanks for sticking with this.

Author

Commented:
I figured out how to rauise the points.
CERTIFIED EXPERT
Top Expert 2010
Commented:
"Where can I look to see if there is the other code you suspect?"
Look through all code modules for any reference to wdOutlineLevelBodyText.

"is there a workaround/ I mean, is there a substitute for wdOutlineLevelBodyText that m Outlook might accept?"
Yes.  Add this line at the top of the module the code is in.

Const wdOutlineLevelBodyText = 10

"Also, how do I get dbindt to go to single line space instead of double line space?"
Sorry, I don't know the answer to that.  That's really a Word issue since the original code came from Word.  I used the code exactly as provided with only minor changes to make it work in Outlook.  None of those changes were to the formatting choices.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2010

Commented:
I think there must be another setting on your system that's causing the double spacing.  It doesn't double-space on my system.  Also, curious what you deleted that got it working.

Author

Commented:
Great Air-Traffic-Control type of help. A difficult problem was solved.

Author

Commented:
I deleted the exact lines I quoted to you above (which were highlighted in the debug screen.

As for the double spacing, the Word Macro actually does single spacing (even when the quoted paragraph is double spaced).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.