创建专属单词实例

发布于 2024-09-24 07:37:14 字数 273 浏览 0 评论 0原文

我正在使用 ac# .net4 winforms 应用程序创建一个具有互操作性的 word (14) 实例来处理文档。如果某些 Word 文档在我的应用程序之外打开,则会使用相同的 Word 实例,从而干扰我的应用程序。

简单问题:有什么方法可以将我的单词实例设置为我的应用程序专用?

提前致谢。

顺便说一句:发现了一些带有 Exclusive/word/office/isolated/block/instance 的东西,但无论如何都没有答案。

i am creating a word (14) instance with interop out of a c# .net4 winforms application to work with a document. If some word document gets opened beyond my application the same word instance will be used an disturbs my application.

Simple question: Is there any way to set my word instance exclusive for my application?

Thanks in advance.

Btw: Found some stuff with exclusive/word/office/isolated/block/instance but no answers anyhow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-10-01 07:37:14

有一种解决方案,但它并不漂亮。
主要问题是Word 将自身注册到ROT(运行对象表)中,然后其他应用程序可以轻松访问在ROT 中注册的Word 实例(这就是VB GetObject 函数的作用)。

因此,在您的应用程序中,您基本上必须做两件事

  1. 尝试 GetObject< /a> (即查询正在运行的实例的 ROT)
  2. 如果您得到一个,您就知道您必须创建一个新的 Word 实例才能使用(CreateObject 在VB中,该过程在其他语言中有所不同)。
  3. 如果您没有获得一个,则必须创建 2 个新的 Word 实例。第一个会自动在 ROT 中注册,第二个则不会。使用第二个实例,并且完全使用第一个实例。

即使您终止第一个实例,它也不会“追溯”在 ROT 中注册自身,并且其他应用程序通常不会反对使用对其的引用,它们会自动创建一个新实例,因为没有其他实例不再在 ROT 中注册,然后将被注册。

也就是说,其他应用程序仍然有可能获取您的 Word 实例,因此这种技术并非万无一失。如何?因为 Word 还将每个加载的文档注册到 ROT 中。但这是一个很少使用的功能。

There's sort of a solution, but it's not pretty.
The main issue is that Word registers itself in the ROT (Running Object Table), and other applications can then easily get access to the instance of Word registered in the ROT (that's what the VB GetObject function does for instance).

So, in your app, you'd basically have to do 2 things

  1. Try to GetObject (ie query the ROT for a running instance)
  2. If you get one, you know you HAVE to create a new instance of Word to use (CreateObject in VB, the process is different in other langs).
  3. If you DON'T get one, you have to create 2 new instances of Word. The first will automatically register itself in the ROT, the second won't. Use the second instance, and quite the first instance.

Even though you terminate that first instance, It won't "retroactively" register itself in the ROT, and other applications will generally not object a reference to it to use, they'll automatically create a new instance, which, since no other instance is registered in the ROT anymore, will then get registered.

That said, it is still possible for other apps to get at your instance of Word, so this technique isn't bulletproof. How? Because Word ALSO registers each loaded DOCUMENT in the ROT. But that's a pretty seldom used feature.

删除→记忆 2024-10-01 07:37:14

不,您无法仅为自己锁定 Word 实例。

但是,根据您的评论,解决该问题很容易 - 不要使用 ActiveDocument。您可以通过调用特定的文档,然后使用该变量(无论您是打开现有文档还是创建新文档)来绕过使用 ActiveDocument。

例如:

Sub NewDoc()
    Dim d As Document
    Set d = Documents.Add(Visible:=False)
End Sub
Sub ExistingDoc()
    Dim d As Document
    Set d = Documents.Open(FileName:="C:\myexisting.doc")
End Sub

在上述两种情况下,您只需使用 d 代替以前使用的 ActiveDocument

No, there is no way for you to lock an instance of Word just for yourself.

But, based on your comment, it's easy to work around the issue - don't use ActiveDocument. You can get around using ActiveDocument by calling the document something specific and then using that variable (whether you are opening an existing doc or creating a new one).

For example:

Sub NewDoc()
    Dim d As Document
    Set d = Documents.Add(Visible:=False)
End Sub
Sub ExistingDoc()
    Dim d As Document
    Set d = Documents.Open(FileName:="C:\myexisting.doc")
End Sub

In both cases above, you'd just use d in place of where you used to use ActiveDocument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文