none
Call CoAllowSetForegroundWindow in c# RRS feed

  • Question

  • Hi,

    just trying to find a solution to allow Office Word to take focus. I use Silverlight 5 in browser application & Automation to open a Word instance. Using Word Visible and Activate() doesn't work on Vista and above. Just tried to use CoAllowSetForegroundWindow but can't find a way to call the method.  

    [DllImport("ole32.dll")]
     public static extern int CoAllowSetForegroundWindow(IntPtr pUnk, IntPtr lpvReserved);

    My question is what should I use for the first parameter?

    This is the way we create the Word instance:

    private void SetupWordInstance(bool visible = true)
            {
                if (AutomationFactory.IsAvailable)
                {
                    try
                    {
                        _wordApp = AutomationFactory.CreateObject("Word.Application");
                        _wordVersion = _wordApp.Version;
                    }
                    catch
                    {
                        Utils.ShowMessage(Resource.MissingWordApplicationErrorMessage);
                    }

                    if (_wordApp != null)
                    {
                        AutomationEvent beforeCloseEvent = AutomationFactory.GetEvent(_wordApp, "DocumentBeforeClose");
                        beforeCloseEvent.AddEventHandler(new BeforeCloseAppDelegate(BeforeCloseApp));

                        AutomationEvent quitEvent = AutomationFactory.GetEvent(_wordApp, "Quit");
                        quitEvent.AddEventHandler(new QuitAppDelegate(QuitApp));

                        if (visible)
                        {
                            _wordApp.Visible = true;
                            _wordApp.Activate();
                        }
                    }
                }
                else
                {
                    Utils.ShowMessage(Resource.MissingAutomationErrorMessage);
                }
            }

    Thank you,

    Dragos

    Friday, October 26, 2012 8:04 AM

Answers

  • Hi Dragos,

    I'm not family with Sliverlight development, and you say all will run on client machine so I create a sample code to convert http://support.microsoft.com/kb/243844?wa=wsignin1.0 to C# code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            [DllImport("Ole32.dll")]
            private static extern int CoAllowSetForegroundWindow(IntPtr pUnk, IntPtr lpvReserved);
            [DllImport("User32.dll")]
            public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Word.Application wdApp = new Word.Application();
                wdApp.Visible = true;
                Word.Document doc = wdApp.Documents.Add();
                IntPtr Handler = FindWindow(null, wdApp.ActiveWindow.Caption);
                if (Handler != null)
                {
                    CoAllowSetForegroundWindow(Handler, IntPtr.Zero);
                }
                wdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;
                wdApp.Top = -3000;
                wdApp.Activate();
                Clipboard.Clear();
                Clipboard.SetText(textBox1.Text);
                doc.Content.Paste();
                doc.Activate();
                doc.CheckSpelling();
                textBox1.Text = Clipboard.GetText(TextDataFormat.Text);
                doc.Saved = true;
                doc.Close();
            }
        }
    }

    In this code, I add the "Microsoft.Office.Interop.Word" assembly to the project mannually.

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 5, 2012 6:28 AM
    Moderator

All replies

  • Hi Dragos,

    Thanks for posting in the MSDN Forum.

    As far as I know that Office COM object will not support Server side, and you will not add PIA (Primary Interop Assembly) to you Sliverlight project. Please take a look at http://support.microsoft.com/kb/257757.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 29, 2012 4:41 AM
    Moderator
  • Hi Tom,

    the component runs client side in browser not server side.

    function initOfficePlugin() {
        Silverlight.createObject(
                "Controls/OfficePlugin.xap?v=" + __currentOfficePluginVersion,
                document.getElementById('officePluginHost'),
                "OfficePlugin",
                {
                    width: "0px",
                    height: "0px",
                    background: "transparent",
                    minRuntimeVersion: "5.0.61118.0",
                    autoUpgrade: "true"
                },
                {
                    onError: onSilverlightError,
                    onLoad: pluginLoad
                },
                "",
                "context"
            );
    }

    All works OK. the Word instance opens the only problem is to get to focus on the application. On Vista and above the application doesn't focus. Found this similar post http://social.msdn.microsoft.com/Forums/en/worddev/thread/017a1343-99d2-4fa0-87c4-96ad54a6f481 where they suggest to call CoAllowSetForegroundWindow method. Currently I use the workaround

    word.WindowState = 2; //Minimize word.WindowState = 1; //Maximize

    but this is not an elegant solution.

    Also found this article how to call the method in VB but no articles how to call it in C#

    http://support.microsoft.com/kb/243844?wa=wsignin1.0

    Hope now you have more info.

    Cheers,

    Dragos



    • Edited by dragosraduc Wednesday, October 31, 2012 6:53 PM
    Wednesday, October 31, 2012 6:49 PM
  • Hi Dragos,

    I'm not family with Sliverlight development, and you say all will run on client machine so I create a sample code to convert http://support.microsoft.com/kb/243844?wa=wsignin1.0 to C# code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            [DllImport("Ole32.dll")]
            private static extern int CoAllowSetForegroundWindow(IntPtr pUnk, IntPtr lpvReserved);
            [DllImport("User32.dll")]
            public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Word.Application wdApp = new Word.Application();
                wdApp.Visible = true;
                Word.Document doc = wdApp.Documents.Add();
                IntPtr Handler = FindWindow(null, wdApp.ActiveWindow.Caption);
                if (Handler != null)
                {
                    CoAllowSetForegroundWindow(Handler, IntPtr.Zero);
                }
                wdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;
                wdApp.Top = -3000;
                wdApp.Activate();
                Clipboard.Clear();
                Clipboard.SetText(textBox1.Text);
                doc.Content.Paste();
                doc.Activate();
                doc.CheckSpelling();
                textBox1.Text = Clipboard.GetText(TextDataFormat.Text);
                doc.Saved = true;
                doc.Close();
            }
        }
    }

    In this code, I add the "Microsoft.Office.Interop.Word" assembly to the project mannually.

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 5, 2012 6:28 AM
    Moderator