开发者

Word Interop app to write text to the end of an open document

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开发者_如何学C here!


Have you looked at using Marshal.GetActiveObject("Word.Application") to get the running application, rather than creating a new one?


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.

 WordC.Application wordApp = new WordC.Application();
                //  create Word document object
                WordC.Document aDoc = null;
                object readOnly = false;
                 object isVisible = false;
                 wordApp.Visible = false;
               //  wordApp.DisplayAlerts = false;
//docFileName is the filename with complete path. ex c://test.doc

                 aDoc = wordApp.Documents.Open(docFileName, Type.Missing, ref readOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ref isVisible, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
aDoc.Activate();
aDoc.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing, Type.Missing);

                aDoc.Save();
                aDoc.Close(Type.Missing, Type.Missing, Type.Missing);
                wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜