当前位置: 代码迷 >> ASP.NET >> 关于asp.net 操作 Word 剪贴板 的有关问题
  详细解决方案

关于asp.net 操作 Word 剪贴板 的有关问题

热度:9813   发布时间:2013-02-25 00:00:00.0
关于asp.net 操作 Word 剪贴板 的问题
有谁遇到过类似的问题:
两个word文档之间进行copy、paste操作时出现以下错误: 
此方法或属性无效,因为剪贴板是空的或无效的!

生成服务合同的时候,合同附件有一个服务内容描述,这是需要根据配置不同的产品服务内容都不一样。
从对应的字模板中读出内容显示在合同中。
生成服务合同我用的是Office的COM组件,从子模板中读内容到服务合同,我就用了复制方法。  
Range.Copy();



C# code
 object Nothing = System.Reflection.Missing.Value;                Word.Application attachApp = new Application();                object type = WdBreakType.wdSectionBreakContinuous;                Word.Document attachDoc = attachApp.Documents.Open(ref docFile, ref Nothing, ref Nothing, ref Nothing,                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);                attachDoc.Select();                attachApp.Selection.WholeStory();                attachDoc.Sections[1].Range.Copy();                object attach_ServiceContent = "attach_ServiceContent";                doc.Bookmarks.get_Item(ref attach_ServiceContent).Range.InsertBreak(ref type);                 doc.Bookmarks.get_Item(ref attach_ServiceContent).Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);                attachApp.NormalTemplate.Saved = true;                object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;                attachDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing);                attachApp.Quit(ref doNotSaveChanges, ref Nothing, ref Nothing);


但是现在不稳定,有时间能生成成功,有时候报错:此方法或属性无效,因为剪贴板是空的或无效的。

我在网上找资料,也没有查出什么结果,有人说是:由于执行上述动作时间不够导致。 
  如果能捕获这种错误,并进行延时直到它完成这个动作;则能解决这个问题; 
  但我不知如果才能捕获上述错误;并进行相应处理. 

//IDataObject data=Clipboard.GetDataObject();

有些人说用以上的做法,得到剪贴板的内容。
这种做法取到的data为null根本不能达到效果,此时是因为word中的剪切板和系统剪切板并不一样

求助,怎么样得到 Word 剪贴板 的内容,然后判断 Word 剪贴板 是否为空,为空就延时5秒再复制一次。

------解决方案--------------------------------------------------------
有问题,帮顶。。。
------解决方案--------------------------------------------------------
C# code
object clipboardData = Clipboard.GetData("userEntites");this.btnPaste.Enabled = (clipboardData != null);  private void btnCopy_Click(object sender, EventArgs e)        {            // 读取数据            List<BaseUserEntity> userEntites = new List<BaseUserEntity>();            for (int i=0; i<this.DTUser.Rows.Count; i++)            {                BaseUserEntity userEntity = new BaseUserEntity(this.DTUser.Rows[i]);                userEntites.Add(userEntity);            }            // 复制到剪切板            Clipboard.SetData("userEntites", userEntites);            this.btnPaste.Enabled = true;        }        private void btnPaste_Click(object sender, EventArgs e)        {            object clipboardData = Clipboard.GetData("userEntites");            if (clipboardData != null)            {                List<BaseUserEntity> userEntites = (List<BaseUserEntity>)clipboardData;                string[] addUserIds = new string[userEntites.Count];                for (int i = 0; i < userEntites.Count; i++)                {                    addUserIds[i] = userEntites[i].Id.ToString();                }                // 添加用户到角色                ServiceManager.Instance.RoleService.AddUserToRole(this.UserInfo, this.TargetRoleId, addUserIds);                // 加载窗体                this.FormOnLoad();                // 设置按钮状态                this.SetControlState();            }        }
------解决方案--------------------------------------------------------
用富文本框把word中数据插入数据库.
然后用label读取出来.效果和原本一样.
  相关解决方案