Consulting

Results 1 to 9 of 9

Thread: Automatically set zoom level to x% when an Outlook message is opened.

  1. #1

    Automatically set zoom level to x% when an Outlook message is opened.

    The first thing I do when I open a message is increase the zoom level. Is there a way to do this programatically, triggered by the act of opening a message?

    Similar question: can the same thing be made to happen when I give keyboard focus to a message displayed in the Reading pane?

  2. #2
    I discovered a partial solution:

    ' Automatic zoom changer
    ' Insert code in "ThisOutlookSession" module
    ' Set Tools > Reference to "Microsoft Word 14.0 Object Library"
    
    Option Explicit
     Dim WithEvents objInspectors As Outlook.Inspectors
     Dim WithEvents objOpenInspector As Outlook.Inspector
     Dim WithEvents objMailItem As Outlook.MailItem
    Private Sub Application_Startup()
     Set objInspectors = Application.Inspectors
    End Sub
    Private Sub Application_Quit()
     Set objOpenInspector = Nothing
     Set objInspectors = Nothing
     Set objMailItem = Nothing
     End Sub
    Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
     If Inspector.CurrentItem.Class = olMail Then
        Set objMailItem = Inspector.CurrentItem
        Set objOpenInspector = Inspector
     End If
    End Sub
    Private Sub objOpenInspector_Close()
        Set objMailItem = Nothing
    End Sub
    Private Sub objOpenInspector_Activate()
    Dim wdDoc As Word.Document
     Set wdDoc = objOpenInspector.WordEditor
     wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = 150   ' Set zoom level here
    End Sub
    To enable the macro, exit VBA, and exit and restart Outlook.

    It works when I open (most) messages.

    But it would be great if the macro was activated when I move directly from one message to the next. (In my case, I open a message in the normal way, and then press Ctrl + > to open the next message, and Ctrl + < to open the previous message.

    Any thoughts on how to add this functionality?

  3. #3
    Let me try again...

    There are two very useful hotkeys in Outlook that are not well known: When a message has been opened -- in other words, the message is in a window in the foreground, and Outlook is in the background -- you can "jump" to the next message by pressing

    Ctrl + > (The hotkey is really Ctrl + period, but I think Ctrl + > is easier to remember)

    Or you can jump to the previous message by pressing

    Ctrl + < (Actually Ctrl + comma)

    These hotkeys make it very fast to review one message after another, as they do away with the need to open messages by returning to the Outlook UI. There is obviously thought behind the programming of these hotkeys. For example, when you press a key to go to the previous or next message, but there are no more messages, the hotkey closes the message window and returns focus to the most recently viewed message in the Outlook UI. Nice work, Microsoft!

    So back to the problem. the code (in the previous message) sets the zoom level for messages to 150%. But the macro only works if I open each message from the Outlook UI. Can anyone think of a way to either modify the macro, or come up with others, that automatically change the zoom level when I navigate from message to message via the two built in hotkeys?

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Try using the selection change to Activate the mailitem.

    ' Automatic zoom changer
    ' Insert code in "ThisOutlookSession" module
    ' Set Tools > Reference to "Microsoft Word 14.0 Object Library"
    
    Option Explicit
    
    Dim WithEvents myOlExp As Outlook.Explorer  ' <===
    
    Dim WithEvents objInspectors As Outlook.Inspectors
    Dim WithEvents objOpenInspector As Outlook.Inspector
    Dim WithEvents objMailItem As Outlook.mailItem
    
    Private Sub Application_Startup()
        Set objInspectors = Application.Inspectors
        Set myOlExp = Application.ActiveExplorer  ' <===
    End Sub
    
    Private Sub Application_Quit()
        Set objOpenInspector = Nothing
        Set objInspectors = Nothing
        Set objMailItem = Nothing
    End Sub
    
    Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
        If Inspector.currentItem.Class = olMail Then
            Set objMailItem = Inspector.currentItem
            Debug.Print "Set objMailItem = Inspector.currentItem"
            Set objOpenInspector = Inspector
        End If
    End Sub
    
    Private Sub objOpenInspector_Close()
        Set objMailItem = Nothing
    End Sub
    
    Private Sub objOpenInspector_Activate()
        Dim wdDoc As Word.Document
        Set wdDoc = objOpenInspector.WordEditor
        wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = 150 ' Set zoom level here
    End Sub
     
    Private Sub myOlExp_SelectionChange()
        Dim currItem As mailItem
        On Error Resume Next ' If there is no open mailitem.
        Set currItem = Application.ActiveInspector.currentItem
    
        objOpenInspector_Activate  ' <===
    
    End Sub
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  5. #5
    Thank you!! Your code works brilliantly, both when opening messages from the main Outlook user interface, and when jumping to the next or previous message via hotkeys.

    Is there a way to modify the code to increase the default zoom level in the Reading pane? Or would that be a separate macro?

    By the way... after inserting (or updating) the code, I found it was necessary to exit and restart Outlook. When exiting Outlook, you will be prompted to save a file. Say Yes.

  6. #6
    I spoke too soon. Outlook is now crashing regularly. The problem happens after opening, or trying to open, email messages from the main UI. Jumping from message to message via the hotkeys seems to work fine.

    I tried rebooting the computer, but the problem persists. Any thoughts on how to crashproof the code?

  7. #7
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Quote Originally Posted by acantor View Post
    I spoke too soon. Outlook is now crashing regularly. The problem happens after opening, or trying to open, email messages from the main UI. Jumping from message to message via the hotkeys seems to work fine.

    I tried rebooting the computer, but the problem persists. Any thoughts on how to crashproof the code?
    Go to the link in my signature. Set breakpoints as described.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  8. #8
    The link to the debugging guide was helpful, but the content is mostly new to me, so I am not understanding it all.

    However, I was able to step through each macro line by line. I did this two ways: with the main Outlook UI open; and with an email message opened. The results were the same both ways:

    Here are my findings:

    1. Nothing happens when I attempt to step through the code for this macro:

    Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)

    2. When I step through Private Sub objOpenInspector_Activate(), I get an error message: "Run-time error '91': Object variable or With block variable not set" on this line:

    Set wdDoc = objOpenInspector.WordEditor


    3. However, no error message occurs when I step through Private Sub myOlExp_SelectionChange(), which calls

    objOpenInspector_Activate ' <===

    So I am assuming that in the normal scheme of things, myOlExp_SelectionChange() always calls objOpenInspector_Activate.

    In addition, I monitored the Immediate window while doing step by step testing, and nothing appeared in it.

    Outlook did not crash during testing. However, the crashes appear to happen when I am pressing up and down arrow keys to navigate through the list of email messages in the Outlook user interface.

  9. #9
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Set the breakpoints, then open messages, then step through where the code is yellow.

    Just in case you see the error in the same place, try bypassing to see what happens.

    Private Sub objOpenInspector_Activate() 
    Dim wdDoc As Word.Document 
    On Error Resume Next   ' <----
    Set wdDoc = objOpenInspector.WordEditor 
    wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = 150 ' Set zoom level here
    On Error GoTo 0        ' <----
    End Sub
    If you stop zooming while testing, run Application_Startup manually.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

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