none
Code a simple screen refresh (F5) in vba for Outlook 2007? RRS feed

  • Question

  • I've got some heady VBA subs we're running as a function of mail processing. A recent addition to the script is leaving behind unwanted "phantoms" that a refresh will clean up. However I can not seem to find a codified solution.

    I've tried .Repaint and SendKeys neither seems to solve my issue. Any suggestions?

    Tuesday, January 21, 2014 5:45 PM

Answers

  • I found the solution to my issue in this article

    http://www.vbforums.com/showthread.php?483679-RESOLVED-Sendkeys-from-Excel-to-Outlook&p=3056758#post3056758

    • Marked as answer by James D. Cook Tuesday, January 21, 2014 9:30 PM
    Tuesday, January 21, 2014 9:30 PM

All replies

  • Usually switching folders and back is about the only way to update the display to get rid of artifacts.

    Without knowing what your "phantoms" are and what your code is that creates them that's about all anyone could say.


    Ken Slovak MVP - Outlook

    Tuesday, January 21, 2014 6:54 PM
    Moderator
  • I was requested to write up a VBA that will move emails from the inbox to specific archive folders at the press of a tool bar button*. To accomplish this I dynamically get the srcFolder and dstFolder and then iterate through the srcFolder items count.

      For i = srcItems.Count To 1 Step -1
            srcItems.Item(i).Move dstFolder
       Next

    The users are not satisfied because when this script is run while in the unread or inbox view the previously painted window is not updated. So emails still show as being in the inbox even though they've been moved. Pressing F5 or refreshing in anyway triggers the repaint and solves the problem. I am hoping to be able to add that in to the macro and be done with it. However everything I have tried has failed. 

    Any suggested solutions?

    * I am aware I could configure a rule, but they wanted all mail to enter the inbox so it can be backed up(?) and then process it from there to a specifically structure archive folder.

    Tuesday, January 21, 2014 8:37 PM
  • Other than hacks such as SendKeys(), or using Win32 API calls to repaint the Outlook ActiveExplorer window, there's not too much you can do other than switch folders (change ActiveExplorer.CurrentFolder to another folder and back). Outlook will repaint after all the moved items are unloaded.

    The best you can do is try to speed that up a bit by using Move() as a function as it was intended, and get and release the moved items. That would involve rewriting the code to something like this, with no guarantee that it will help at all:

    Dim mySrc As Object
    Dim myMoved As Object
    For i = srcItems.Count To 1 Step -1
        Set mySrc = srcItems.Item(i)
        Set myMoved = mySrc.Move(dstFolder)
       
        Set mySrc = Nothing
        Set myMoved = Nothing
    Next

    FWIW, Outlook VBA is not intended for code for distribution. It's for personal macros and for prototyping code. Code for deployment to other Outlook installations should be done as a COM addin.


    Ken Slovak MVP - Outlook

    Tuesday, January 21, 2014 9:03 PM
    Moderator
  • I found the solution to my issue in this article

    http://www.vbforums.com/showthread.php?483679-RESOLVED-Sendkeys-from-Excel-to-Outlook&p=3056758#post3056758

    • Marked as answer by James D. Cook Tuesday, January 21, 2014 9:30 PM
    Tuesday, January 21, 2014 9:30 PM
  • Ken Slovak, I saw the help above and was hoping you would help me. I use the Notes: feature in the contacts cards all the time, I am used to hitting F5 in Notepad to put in Date & Time. In outlook, I have to switch tabs, to Insert pick the Date&Time option then I have it now defaulted to the Date 10/23/14 as an example I then have to go back and do a (space) - (space) and fill in the information. I was able to make a macro in MS Word, and it works tied to the Ctrl+F5 key, but when I copied into Outlook VBA it fails. I will see if I can cut and past what I have. Will you help me? as you can see I did both the Date & Time, but I figure if I remove the h:mm am/pm it will just do the date? My concern is with the Outlook. I have used the send.key command in MVBA but it is different than this VBA? Any help will be appreciated, this is for personal  use on my own computer.

    Sub DateTimeSp()
    '
    ' DateTimeSp Macro
    '
    '
        Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
            InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
            wdCalendarWestern, InsertAsFullWidth:=False
        Selection.TypeText Text:=" - "
    End Sub

    Wednesday, October 22, 2014 6:37 PM
  • Outlook macros are not tied to keys or function keys. You'd have to call your macro directly using Alt+F8 or put it on the ribbon or QAT.

    How do you want to run this macro, on an open item or one that's selected in a folder view or what?

    Let's assume you want it run on an open item that has the focus (ActiveInspector). The underlying email editor in Outlook is a subset of Word. So code something like this:

    Dim oDoc As Word.Document

    Set oDoc = ActiveInspector.WordEditor ' WordEditor = Word.Document

    Dim oSel As Word.Selection

    Set oSel = oDoc.Parent.Selection ' Document.Parent = Word.Application

    oSel.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= wdCalendarWestern, InsertAsFullWidth:=False

    oSel.TypeText Text:=" - "

    See if that works, explicitly setting the Word objects after getting the Document object from an Inspector.


    Ken Slovak MVP - Outlook

    Wednesday, October 22, 2014 6:52 PM
    Moderator
  • Thank you all for the help, I am using the following, but sure would like to know how to add some different icons to the options in Outlook Quick Access tool bar.

    Public Sub DateInsert()
    ' 102614 - Added line for time and it worked, but am trying to find out how to add a space and dash. updated question
    SendKeys Date
    SendKeys " - "
    DoEvents
    End Sub
    Public Sub DateTime()
    ' 102614 - ReNamed to DateTime
    ' 102614 - Added lines for " " (space) then " - " for another space and dash
    SendKeys Date
    SendKeys " "
    SendKeys Time
    SendKeys " - "
    DoEvents
    End Sub

    Both of the above work, but wanted a different Icon and do not know how to add any to the options given??


    RJB Phillips III

    Monday, October 27, 2014 6:36 PM