Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-25-2022, 05:58 PM
harry harry is offline Macro not working Windows 10 Macro not working Office 2019
Advanced Beginner
Macro not working
 
Join Date: Jul 2010
Posts: 31
harry is on a distinguished road
Default Macro not working


1. I created the following simple macro in Word, to remove space before and after paragraphs in a document.

Code:
Sub ClearParagraphSpacing()
Selection.WholeStory
With Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
End With
End Sub
This works fine in Word, but when I copy it into Outlook, I get an error when I run it on a new email or a reply: "Object variable or With block variable not set." How can I fix this?

2. I can add a shortcut for the macro on the Ribbon or the Quick Access Toolbar, but when I click on it, nothing happens. However, as the macro needs to work in a message editing window (not in the main folder window), does it need a different kind of shortcut?
Reply With Quote
  #2  
Old 12-04-2022, 10:24 AM
AlanCantor AlanCantor is offline Macro not working Windows 10 Macro not working Office 2019
Competent Performer
 
Join Date: Nov 2020
Posts: 137
AlanCantor is on a distinguished road
Default

VBA code for Word won't work in Outlook -- or in any other VBA-supported Microsoft product.


VBA comes in different "flavours." Each application has a unique object model, and for that reason, VBA for each application has to be different.



I find VBA for Outlook especially difficult to understand. Hopefully you'll find code samples on the web that give hints on how to accomplish what you want to do.


Another way to do this, albeit less elegant, is to use a third-party automation tool to do the heavy lifting. For example, this Macro Express script (tested only once) might get the job done by sending sequences of keys. To make this script reliable, you may need to add delays after some of the steps.



Text Type (Simulate Keystrokes): <CONTROL>a // Select all
Text Type (Simulate Keystrokes): <F10> // Give the Ribbon focus
Text Type (Simulate Keystrokes): o // Format ribbon
Text Type (Simulate Keystrokes): pg // Paragraph settings
Text Type (Simulate Keystrokes): <ALT>b // Navigate to Spacing Before field
Text Type (Simulate Keystrokes): 0 pt
Text Type (Simulate Keystrokes): <TAB> // Navigate to Spacing After field
Text Type (Simulate Keystrokes): 0 pt
Text Type (Simulate Keystrokes): <ENTER> // Activate the default OK button
Reply With Quote
  #3  
Old 12-04-2022, 09:58 PM
gmayor's Avatar
gmayor gmayor is offline Macro not working Windows 10 Macro not working Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

As Outlook uses a subset of Word to edit messages, you can use some Word VBA in Outlook messages, but you have to tell the macro what you are doing. The following will remove the paragraph spacing (attributable to the paragraph formatting) of the selected message:

Code:
Sub ClearParagraphSpacing()
'Graham Mayor - https://www.gmayor.com - Last updated - 05 Dec 2022 

 Dim olMsg As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object

    On Error Resume Next
    Select Case Outlook.Application.ActiveWindow.Class
        Case olInspector
            Set olMsg = ActiveInspector.currentItem
        Case olExplorer
            Set olMsg = Application.ActiveExplorer.Selection.Item(1)
    End Select
    On Error GoTo lbl_Exit

    With olMsg
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        .Display
        With oRng.ParagraphFormat
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
        End With
    End With
lbl_Exit:
    Set olMsg = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 12-12-2022, 12:22 AM
harry harry is offline Macro not working Windows 10 Macro not working Office 2019
Advanced Beginner
Macro not working
 
Join Date: Jul 2010
Posts: 31
harry is on a distinguished road
Default

Thanks for your replies. Graham's code does exactly what I wanted, and I have successfully assigned it to a button on the Quick Access Toolbar.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with this Macro-Partially Working rsrasc Excel Programming 10 09-10-2020 02:38 PM
Just one macro not working Ulodesk Word VBA 1 04-01-2019 09:16 AM
How to move data - Macro not working ecarter312 Excel Programming 1 08-10-2016 11:26 PM
Macro not working Fraction macro not working Ulodesk Word VBA 1 07-18-2012 04:01 PM
Macro to open template not working Brandi Mail Merge 2 06-24-2010 09:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:07 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft