用 C# 來控制一個已開啟的 Word ,及其文件和內容(類似VBA裡的GetObject函式功能) Marshal.GetActiveObject

解題過程都以實境秀的方式記錄下來了:
難在這二行
obj = Marshal.GetActiveObject("Word.Application");
找到GetActiveObject來取用,如VBA的GetObject方法
 Word.Application app = word_app as Word.Application;
將 GetActiveObject 傳回的 Object 型態值 轉成 Word.Application 才能用Word提供的類別和方法、屬性來操作
也分別附上當時我主要參考的帖文網址了。過程應該實境秀都入鏡了 感恩感恩 南無阿彌陀佛 
至於小朱先生的指點和鍵議,末學有時間會來琢磨的。有結果也會在此公布分享。謝謝大家。阿彌陀佛
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using Word = Microsoft.Office.Interop.Word;
using ado = ADODB;
using System.Runtime.InteropServices;
using Microsoft.Vbe.Interop;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            object obj = null;
            try
            {
                obj = Marshal.GetActiveObject("Word.Application");//Marshal.GetActiveObject 方法 (String)

            }
            catch (Exception e)
            {
                //Console.WriteLine(e.ToString());
                //throw;
            }
            if (obj != null)
            {
                hzShengFu(obj);
            }

           void hzShengFu(object word_app)
            {
                //Word._Application word_app = new Microsoft.Office.Interop.Word.Application();
                //Word._Application word_app = Obj;//Activator.GetObject("Word.Document ",);                
                Word.Application app = word_app as Word.Application;//[C#.NET] 類型轉換前要不要先判斷?直接強制轉型好嗎?(余小章 @ 大內殿堂)
                if (app.Documents.Count>0)
                {
                        Word.Document word_doc= app.ActiveDocument;
                        string docContent = word_doc.Content.Text;
                        //StringInfo docContentInfo = new StringInfo(docContent);
                        TextElementEnumerator docContentEnum = StringInfo.GetTextElementEnumerator(docContent);
                        //docContentEnum就相當於VBA的 tbxD.TextRange.Characters
                        //接下來就是 For Each a In tbxD.TextRange.Characters
                        while (docContentEnum.MoveNext())
                        {                
                            string a = docContentEnum.Current.ToString(); //就相當於 a(每個Character)
                            //if (a.Equals(Convert.ToChar(13))) //a==Convert.ToChar(13)
                            if (a.Equals("\r")) //a==Convert.ToChar(13) 
                                                  //https://dotblogs.com.tw/mis2000lab/2013/11/06/126917
                            {

                            }
                            else//a!="\r"  //https://msdn.microsoft.com/zh-tw/library/c35t2ffz.aspx
                            {
                                Console.Write(a);
                                //Console.ReadKey();
                            }
                        }
                        //word_app.Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
                     Console.WriteLine("done!!");
                    Console.ReadKey();
                }
            }
        }
    }
}

From:
https://social.msdn.microsoft.com/Forums/office/en-US/203fb9b4-a518-45d2-a2c8-15cc89a41078/-c-word-vbagetobject?forum=233


留言

熱門文章