簡體   English   中英

Microsoft Word自動化

[英]Microsoft word automation

我們有一個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);
        }
    }

它可能與Microsoft提供的稱為Desktop Bridge的技術相對應。 這是一個解釋。 輕松提取UWP中不可用的Windows桌面功能,並將其與應用程序一起提供。

Docs / Windows / UWP / Develop / Porting apps to Windows 10 / Desktop Bridge

以下是使用Excel時的示例。

桌面應用程序與UWP示例的橋梁

UWP調用Office Interop API

Windows應用程序打包項目示例

Excel.Interop

由於Word API的定義在下面,因此似乎可以如上使用。

Microsoft.Office.Interop.Word命名空間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM