Word文档内容被替换 [英] Word Document content getting replaced

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

问题描述

在我的应用程序中,我试图以编程方式访问word文档。

我的C#代码将以特定时间间隔将段落文本(其标记/分隔符)写入活动word文档。 br />


那部分工作正常。

问题是,同一文档被另一个应用程序同时访问,写了一些文本进入它。



但是当我以特定间隔添加标记时,它会替换其他应用程序添加的现有文本。

但是它保留了较旧的标记,只有其他应用程序编写的内容才会被替换。



我的代码被复制,请帮忙搞清楚。



In my application, I am trying to access word document programmatically.
My C# code will write a paragraph text (its a marker/separator) into the active word document at particular time interval.

That part is working perfectly.
The problem is, the same document is being accessed by another Application at the same time which write some text into it.

But when i add the marker at a particular interval, it replaces the existing text added by the other application.
But it retains older markers, only the content written by other application is getting replaced.

My code is copied, please help to figure out.

using System;
using System.Reflection;
using log4net;
using log4net.Config;
using Word = Microsoft.Office.Interop.Word;

namespace WordOperation{

class Program {

 private static ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 private static object _oMissing = System.Reflection.Missing.Value;
 private static object _oEndOfDoc = "\\endofdoc";
 private static object _oEoF = Word.WdUnits.wdStory;

 static void Main(string[] args)
 {
   var wordApplication = new Word.ApplicationClass {Visible = true};
   Word.Document document = wordApplication.Documents.Add();
   document.Activate();
    while (true)
      {
      sleep(5000);
      object oRng = document.Bookmarks.get_Item(ref _oEndOfDoc).Range;
      Word.Paragraph markerParagraph = document.Content.Paragraphs.Add(ref oRng);
      markerParagraph.Range.Text = Common.MarkerTextContent;
      markerParagraph.Range.InsertParagraphAfter();
      wordApplication.ActiveWindow.Selection.EndKey(ref _oEoF, ref _oMissing);
      }
    }
  }
}

推荐答案

Word,Excel文档和一般情况所有基于文件的程序都不是数据库,你不应该期望使用它们(来自不同地方的多个更新)。



你应该重新思考你是如何做到的避免这种情况。
Word, Excel documents and generally all file based programs are not databases and you should not expect to use them as such (multiple updates from different places).

You should rethink how you are doing things to avoid this.


多次更新,一切正常。

问题是最新文本被替换。

我解决了通过插入一行代码来解决问题。转到文档的末尾。

Multiple updates and everything are working fine.
The problem was the latest text getting replaced.
I solved the issue by inserting just one line of code.. goto the end of the document.
wordApplication.ActiveWindow.Selection.EndKey(ref _oEoF, ref _oMissing);


这篇关于Word文档内容被替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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