微软词自动化

我们有一个 UWP 应用程序,我们希望有以下场景:

  1. 用文档打开 Microsoft Word

  2. 编辑文件

  3. 关闭文档并获取数据到我们的应用程序。

我们有一个 Silverlight 应用程序,它使用下面的代码并很好地解决了问题。我们可以在 UWP 中做类似的事情吗?以编程方式打开 Word 并等待实例关闭。

private void SetupWordInstance(bool visible = true)

    {

        if (AutomationFactory.IsAvailable)

        {

            _wordApp = null;


            try

            {

                _wordApp = AutomationFactory.CreateObject("Word.Application");

                _wordVersion = _wordApp.Version;

            }

            catch

            {

                try

                {

                    _wordApp = AutomationFactory.CreateObject("Word.Application");

                    _wordVersion = _wordApp.Version;

                }

                catch (Exception)

                {

                    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();

                    FocusWordInstance();

                }

            }

        }

        else

        {

            Utils.ShowMessage(Resource.MissingAutomationErrorMessage);

        }

    }


慕哥6287543
浏览 181回答 1
1回答

桃花长相依

有可能它可以通过微软提供的称为桌面桥接的技术进行对应。这是一个解释。很简单,就是提取UWP中没有的Windows桌面功能,和应用程序一起提供。Docs/Windows/UWP/Develop/Porting apps to Windows 10/Desktop Bridge以下是使用 Excel 时的示例。桌面应用程序桥接到 UWP 示例UWP 调用 Office Interop APIWindows 应用程序打包项目示例Excel.Interop由于Word API的定义如下,看来可以如上使用。Microsoft. Office. Interop. Word 命名空间
打开App,查看更多内容
随时随地看视频慕课网APP