Processing Ajax...

Close Dialog


Close Dialog

Close Dialog

A recent AVG and Avast antivirus update is preventing DisplayFusion 9.9 from launching on some systems.
If you're running into this issue, please update to the latest DisplayFusion 10.0 Beta.

Move New Outlook Message Windows or Word Windows to Monitor 2

This is a script that loops in the background, looking for new Outlook message windows or Word windows that need to be moved to monitor 2. This is a workaround because Window Created Trigger rules don't work with Outlook and Word, due to the fact that they re-use window handles from closed windows. You can change the target monitor on line 10 (for split monitors, use a 0 instead of a period, like this: 202).
Minimum Version
Created By
Keith Lammers (BFS)
Date Created
May 12, 2020
Date Last Modified
Aug 18, 2020

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Collections.Generic;

public static class DisplayFusionFunction
	public static void Run(IntPtr windowHandle)
        // Set the monitor you want the windows to move to
        uint targetMonitor = 2;
        // Set the amount of time (in milliseconds) between loops (lowering this time will increase CPU usage)
        uint waitTime = 1000;
        // Script setup stuff
        var previousList = new List<IntPtr>();
        var currentList = new List<IntPtr>();

		while (true) // Loop forever (exit the script via its icon in the system tray)
            // Clear the current list of windows
            BFS.General.LogText("MoveNewOutlookAndWordWindows: Current list cleared: " + currentList.Count.ToString());
            // Add the current Outlook or Word window handles to a list
            currentList = GetOpenOutlookAndWordWindows();
            // Compare the list from the previous run to the list from the current run and move any new ones
            foreach (IntPtr window in currentList)
                if (!(previousList.Contains(window)))
                    BFS.Window.MoveToMonitorMaximized(targetMonitor, window);
                    BFS.General.LogText("MoveNewOutlookAndWordWindows: Window moved to monitor " + targetMonitor.ToString() + ": " + window.ToString() + " " + BFS.Window.GetText(window));
                    BFS.General.LogText("MoveNewOutlookAndWordWindows: Window added to previous list: " + window.ToString() + " " + previousList.Count.ToString());
            // If the previous list has windows that aren't in the new list, mark them for deletion
            var closedWindows = new List<IntPtr>();
            foreach (IntPtr window in previousList)
                if (!(currentList.Contains(window)))
                    // Re-check the current list for Outlook windows that were quickly removed/re-added
                    var refreshedList = new List<IntPtr>();
                    refreshedList = GetOpenOutlookAndWordWindows();
                    if (!(refreshedList.Contains(window)))
                        BFS.General.LogText("MoveNewOutlookAndWordWindows: Window marked for deletion: " + window.ToString());
            // Delete the closed windows from the previous list
            foreach (IntPtr window in closedWindows)
                BFS.General.LogText("MoveNewOutlookAndWordWindows: Window deleted: " + window.ToString()  + " " + previousList.Count.ToString());
            // Wait before looping to prevent the CPU from running up
	private static List<IntPtr> GetOpenOutlookAndWordWindows()
        var windowList = new List<IntPtr>();
        foreach (IntPtr window in BFS.Window.GetVisibleAndMinimizedWindowHandles())
            if ((BFS.Application.GetMainFileByWindow(window).ToLower().Contains("outlook.exe") && BFS.Window.GetText(window).Contains("Message ("))
                || (BFS.Application.GetMainFileByWindow(window).ToLower().Contains("winword.exe") && (BFS.Window.GetText(window).Contains(".doc") || BFS.Window.GetText(window).Contains(".docx") || BFS.Window.GetText(window).Contains(".rtf"))))
                BFS.General.LogText("MoveNewOutlookAndWordWindows: Window added to current list: " + window.ToString() + " " + windowList.Count.ToString());
        return windowList;