获取活动的Word文档对象 [英] Get active word document object

查看:157
本文介绍了获取活动的Word文档对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将VB6项目转换为C#,并且需要获取有效的Word文档对象.在vb6中,使用

I am converting a VB6 project to C# and I need to get the Active word document object. In vb6 it is easy by using

dim objWordDoc as Word.Document
set objWordDoc = Word.ActiveDocument

我需要使用C#获取Word的活动Word文档. 我在项目中添加了以下引用

I need to get the Word's active word document using C#. I have added the below references to the project

并将对类的引用添加为

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

Private Word.Document _activeDocument;

是否有C#中的任何功能来获取活动的Word文档?

Is there any function like in C# to get the active word document?

_activeDocument = Word.Application.ActiveDocument;

推荐答案

我找到了答案.

object word;
Word.Document _activeDocument;

        try
        {
            word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
            //If there is a running Word instance, it gets saved into the word variable
        }
        catch (Exception ex)
        {
            //If there is no running instance, it creates a new one
            Type type = Type.GetTypeFromProgID("Word.Application");
            word = System.Activator.CreateInstance(type);
        }


        Word.Application oWord = (Word.Application) word;
        _activeDocument = oWord.ActiveDocument

我已经用这个问题的答案找到了这个. 找到Office应用程序的现有实例

I have used the answer in this question to find this. Find existing instance of Office Application

这篇关于获取活动的Word文档对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆