[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create or import macros into Outlook 2007

Posted on 2009-02-10
14
Medium Priority
?
1,966 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.
0
Comment
Question by:focusers
  • 8
  • 6
14 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 23609786
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.
0
 

Author Comment

by:focusers
ID: 23613523
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?)
0
 
LVL 76

Expert Comment

by:David Lee
ID: 23614731
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:focusers
ID: 23618218
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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 23618321
What line are you taken to in the debugger?
0
 

Author Comment

by:focusers
ID: 23618389
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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 23618477
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.
0
 

Author Comment

by:focusers
ID: 23618798
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?
0
 

Author Comment

by:focusers
ID: 23618829
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.
0
 

Author Comment

by:focusers
ID: 23618834
I figured out how to rauise the points.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 23618842
"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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 23618855
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.
0
 

Author Closing Comment

by:focusers
ID: 31545466
Great Air-Traffic-Control type of help. A difficult problem was solved.
0
 

Author Comment

by:focusers
ID: 23618866
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).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
I came across an unsolved Outlook issue and here is my solution.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question