Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Open Excel File on Monitor 2

Description
DisplayFusion can open more than one copy of Excel, and put the copies on different monitors, but Excel remembers the last position of the file(s), and wants to put them back there -- on the same monitor. (At least, Office 2010/Office 14 does this.) Trying to force the location to different monitors during startup *starts* to work, but the program overrides the new location; in the end, the only thing that actually moves to the new monitor is the startup screen.
This script allows Excel to do as it pleases, waits a bit, then finds the new window and puts it where it was supposed to go. The timeout probably depends on certain computer-related factors. On my (far-from-flagship) 2.5GHz Phenom II X4, the delay needs to be about 900ms. I set it to 1200ms to be safe. YMMV.
Language
C#.net
Minimum Version
Created By
Bruce Wahler
Contributors
-
Date Created
Mar 16, 2016
Date Last Modified
Mar 17, 2016

Scripted Function (Macro) Code

using System;
using System.Drawing;

// The 'windowHandle' parameter will contain the window handle for the:
// - Active window when run by hotkey
// - Window Location target when run by a Window Location rule
// - TitleBar Button owner when run by a TitleBar Button
// - Jump List owner when run from a Taskbar Jump List
// - Currently focused window if none of these match
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		//location of MS Excel and file name
		string excelLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.EXE";
		string excelFile = "\"C:\\Documents\\Excel File Name.xlsx\"";

		//shortFileText is a piece of the file's name, can be the whole file name without path
		string shortFileText = "*Excel File*";

		//check for the file, and if not open, do so
		// if the file is already open, don't open another copy (Excel won't like it!)
		IntPtr excelWindow = BFS.Window.GetWindowByText(shortFileText);

		if (excelWindow == IntPtr.Zero)
		{
			uint appId = BFS.Application.Start(excelLocation, excelFile);
			excelWindow = BFS.Application.GetMainWindowByAppID(appId);

			//if we couldn't get the Excel window, exit the function
			if (excelWindow == IntPtr.Zero)
				return;

			//make sure that the program is all the way open on the current monitor
			BFS.General.ThreadWait(1200);

			//then find it
			excelWindow = BFS.Window.GetWindowByText(shortFileText);

			//move file to Monitor 2
			BFS.Window.MoveToMonitor(2, excelWindow);
		}

		//otherwise, just highlight the file
		else
		BFS.Window.Focus(excelWindow);
	}
}