Word Interop 应用程序可将文本写入打开文档的末尾
我正在尝试编写一个 C# 应用程序,该应用程序将找到在 MS Word 中打开的文档,并使用 Word interop 将一些文本写入文档的末尾。这可能吗?
我知道可以使用 .NET Framework 中内置的 Process 和 Sendkeys 来解决这个问题,但我想使用 Word Interop 来解决这个问题,这样我就可以在以后添加更多功能(而且 sendkeys 实际上只能解决在某些特殊情况下会出现问题。)
谢谢!
更新:
我得到了以下部分解决方案的工作:
Application wordApp = new Application();
wordApp.Visible = true;
wordApp.Documents.Add();
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
但我想使用一个已经打开的单词实例,而不是创建一个新实例。谢谢!
更新2:
我已经很接近了!以下代码适用于关闭 UAC 的情况
Application wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
,但我不确定如何让它在启用 UAC 的情况下工作。 UAC 不会导致任何错误或异常,它只是不会将文本写入打开的文档。
感谢大家迄今为止的帮助,现在就在眼前了:)!
更新 3:
刚刚在打开 UAC 的情况下再次尝试,它起作用了......奇怪。不过,如果您知道有关互操作和 UAC 的任何好资源,请在此处发布链接!
I'm trying to write a C# application that will find a document open in MS Word and write some text to the end of the document using word interop. Is this possible?
I know it's possible to kind of solve this problem using Process and Sendkeys built into the .NET Framework, but I'd like to solve the problem using Word Interop so I can add more functionality down the road (also sendkeys would really only solve the problem in certain special cases.)
Thanks!
Update:
I got the following partial solution working:
Application wordApp = new Application();
wordApp.Visible = true;
wordApp.Documents.Add();
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
But I'd like to use an already open instance of word instead of creating a new one. Thanks!
Update 2:
I'm close! The following code works with UAC turned off
Application wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
But I'm not sure how to get it working with UAC enabled. UAC isn't causing any errors or exceptions, it just doesn't write the text to the open document.
Thanks for everyones help so far, the end is now in sight :)!
Update 3:
Just tried it again with UAC turned on and it worked... strange. Still if you know of any good resources about interop and UAC in general, please post a link here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用
Marshal.GetActiveObject("Word.Application")
来获取正在运行的应用程序,而不是创建一个新应用程序?Have you looked at using
Marshal.GetActiveObject("Word.Application")
to get the running application, rather than creating a new one?肯定是埃文. Microsoft Office 互操作程序集可让您通过 C# 执行几乎所有操作! SendKeys() 是一个问题。
http://msdn.microsoft.com/en- us/library/15s06t57(v=vs.80).aspx
我想我应该详细说明 SendKeys():它甚至不再可靠地工作,因为它是一个主要的黑客 工具。 MS Office 互操作程序集允许您使用每个 Office 组件执行大量操作。我在 MS Excel 中广泛使用了它们,还有一些在 Word 中使用,您几乎可以通过编程完成用户可以完成的任何操作。
Definitely Evan. The Microsoft Office Interop Assemblies let you do just about anything from C#! SendKeys() is an issue.
http://msdn.microsoft.com/en-us/library/15s06t57(v=vs.80).aspx
I guess I should elaborate about SendKeys(): it doesn't even work reliably anymore as it was a primary hacker tool. The MS Office Interop Assemblies allow you to do an enormous array of things with each of the Office components. I have used them extensively with MS Excel, and some with Word, and you can do just about anything a user can do programatically.
您可以在下面尝试。这里我给出了图像插入的示例。
You can try below .Here i am giving example with image insert.