作者: pepsl179
查看: 1167|回复: 2
打印 上一主题 下一主题

[求助] 对已打开的WORD文档进行操作的问题

[复制链接]
跳转到指定楼层
楼主
pepsl179 发表于 2017-3-23 16:42:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 1167|回复: 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;

namespace MultipleConnect
{
    public class PmConnect
    {
        [DllImport("ole32.dll")]
        static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
        [DllImport("ole32.dll")]
        public static extern void GetRunningObjectTable(int reserved, out IRunningObjectTable prot);      
        private List<object> GetRunningInstances(string[] progIds)
        {
            List<string> clsIds = new List<string>();           
            
            foreach (string progId in progIds)
            {
                Type type = Type.GetTypeFromProgID(progId);
               
            if (type != null)
                    clsIds.Add(type.GUID.ToString().ToUpper());
            }
            
            IRunningObjectTable Rot = null;
            GetRunningObjectTable(0, out Rot);
            if (Rot == null)
                return null;
            
            IEnumMoniker monikerEnumerator = null;
            Rot.EnumRunning(out monikerEnumerator);

            if (monikerEnumerator == null)
                return null;

            monikerEnumerator.Reset();

            List<object> instances = new List<object>();

            IntPtr pNumFetched = new IntPtr();

            IMoniker[] monikers = new IMoniker[1];

           
            while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
            {
                IBindCtx bindCtx;
                CreateBindCtx(0, out bindCtx);
                if (bindCtx == null)
                    continue;

                string displayName;
                monikers[0].GetDisplayName(bindCtx, null, out displayName);

               
                foreach (string clsId in clsIds)
                {
                   if (displayName.ToUpper().IndexOf(clsId) > 0)
                  
                    {
                        object ComObject;
                        Rot.GetObject(monikers[0], out ComObject);

                        if (ComObject == null)
                            continue;                       

                        instances.Add(ComObject);
                        
                           
                        break;
               }
                }
            }

            return instances;
        }

        public void TestROT()
        {
           
            string[] ProgIds =
            {

                  "WORD.Application",
             };

            List<object> instances = GetRunningInstances(ProgIds);

            foreach (object acadObj in instances)
            {
                try
                {
                                  }
                catch
                {

                }
            }
        }
    }
}
这样只能连接第一个打开的WORD文档,我需要任意连接打开的文档,望大神们指点,谢谢!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
回复 论坛版权

使用道具 举报

沙发
人死卵朝天 发表于 2017-3-24 11:14:45 | 只看该作者
我去 二话不说 直接就是 大段代码!
楼主太耿直了

你这什么叫 只能连接第一个打开的word文档
到底是你自己程序 只能打开1个文档
还是 要打开 已经被第三方程序打开的word文档

关于第二点 文件人家是独占模式打开 你肯定不能随便搞吧
变相一下 复制一份丢到temp目录打开不就行了么
只是保存的时候提示一下 被占用 无法保存
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-5-19 22:21, Processed in 0.593877 second(s), 28 queries , Gzip On.

快速回复 返回顶部 返回列表