Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:40+00:00 2026-05-28T23:34:40+00:00

I have this little code to add a watermark image in a doc file,

  • 0

I have this little code to add a watermark image in a doc file, only have a little issue, if i convert it one time it works ok, but if i try to convert another file gives me this error:

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

in this line:

wordFile = oWord.Documents.Open(row.filename);

This is my code:

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
        Document wordFile = new Document();

        //OTHER VARIABLES
        Object oClassType = "Word.Document.8";
        Object oTrue = true;
        Object oFalse = false;
        Object oMissing = System.Reflection.Missing.Value;

private void BtnInserirImagem_Click(object sender, RoutedEventArgs e)
        {
            foreach(MyDocsList row in dataGridList.Items)
            {
                wordFile = oWord.Documents.Open(row.filename);
                addWatermark(wordFile, row.filename);

                //QUITTING THE APPLICATION
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }

        private void addWatermark(Document doc, string filename)
        {
            object refmissing = System.Reflection.Missing.Value;
            string watermarkPath = Directory.GetCurrentDirectory() + "\\fundo.jpg";

            if (File.Exists(watermarkPath))
            {

                object linkToFile = false;
                object saveWithDocument = true;

                WdHeaderFooterIndex hfIndex = WdHeaderFooterIndex.wdHeaderFooterPrimary;

                HeaderFooter headerFooter;

                for (int i = 1; i < doc.Sections.Count + 1; i++)
                {

                    if (doc.Sections[i].Headers != null)
                    {
                        headerFooter = doc.Sections[i].Headers[hfIndex];
                    }
                    else if (doc.Sections[i].Footers != null)
                    {
                        headerFooter = doc.Sections[i].Footers[hfIndex];
                    }
                    else
                    {
                        headerFooter = null;
                    }

                    if (headerFooter != null)
                    {

                        try
                        {
                            Microsoft.Office.Interop.Word.Shape watermarkShape;
                            watermarkShape = headerFooter.Shapes.AddPicture(watermarkPath, ref linkToFile, ref saveWithDocument, ref refmissing,
                                                                            ref refmissing, ref refmissing, ref refmissing, ref refmissing);

                            watermarkShape.Left = Convert.ToSingle(WdShapePosition.wdShapeLeft);
                            watermarkShape.Top = Convert.ToSingle(WdShapePosition.wdShapeCenter);
                        }
                        catch (System.Runtime.InteropServices.COMException e)
                        {
                            MessageBoxResult result = MessageBox.Show(String.Format("{0} - Error Code = {1}", e.Message, e.ErrorCode));
                            throw e;
                        }
                    }
                }

                oWord.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
                oWord.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Could not find watermark image");
            }

            //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
            string outstringFile="";
            string pathfile = System.IO.Path.GetDirectoryName(filename);
            string filenameFile = System.IO.Path.GetFileName(filename);
            outstringFile = pathfile + "\\OG" + filenameFile;

            Object oSaveAsFile = (Object)(outstringFile);

            wordFile.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);


            //CLOSING THE FILE
            wordFile.Close(ref oFalse, ref oMissing, ref oMissing);


        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:34:41+00:00Added an answer on May 28, 2026 at 11:34 pm

    Taken from http://social.msdn.microsoft.com/Forums/en-NZ/vbinterop/thread/2c8689e4-4f8c-42c1-b092-f8ae41b9779c

    When you first launch Word programmatically, it connects to Word via an RPC server. When you close the document, this server gets closed without your application knowing about it. The solution is to trap an error and re-initialise your Word object. you will then be able to continue.

    Edit:

    You initialize your oWord object outside and only once. Then you are calling the .Quit() method on it in your foreach loop in the first run after which it is not available anymore. You will either have to remove the oWord.Quit(); in BtnInserirImagem_Click and live with a constantly initialized word object or you could initialize it when needed and call quit on it afterwards.

        Document wordFile = new Document();
    
        //OTHER VARIABLES
        Object oClassType = "Word.Document.8";
        Object oTrue = true;
        Object oFalse = false;
        Object oMissing = System.Reflection.Missing.Value;
    
        private void BtnInserirImagem_Click(object sender, RoutedEventArgs e)
        {
            //Init Word App Object here
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    
            foreach (MyDocsList row in dataGridList.Items)
            {
                wordFile = oWord.Documents.Open(row.filename);
                //pass the Word App instance
                addWatermark(wordFile, row.filename, oWord);
            }
            // Quitting oWord outside of the loop
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
    
        //added the Word App instance to the signature
        private void addWatermark(Document doc, object filename, Microsoft.Office.Interop.Word.Application oWord)
        {
            object refmissing = System.Reflection.Missing.Value;
            string watermarkPath = Directory.GetCurrentDirectory() + "\\fundo.jpg";
    
            if (File.Exists(watermarkPath))
            {
    
                object linkToFile = false;
                object saveWithDocument = true;
    
                WdHeaderFooterIndex hfIndex = WdHeaderFooterIndex.wdHeaderFooterPrimary;
    
                HeaderFooter headerFooter;
    
                for (int i = 1; i < doc.Sections.Count + 1; i++)
                {
    
                    if (doc.Sections[i].Headers != null)
                    {
                        headerFooter = doc.Sections[i].Headers[hfIndex];
                    }
                    else if (doc.Sections[i].Footers != null)
                    {
                        headerFooter = doc.Sections[i].Footers[hfIndex];
                    }
                    else
                    {
                        headerFooter = null;
                    }
    
                    if (headerFooter != null)
                    {
    
                        try
                        {
                            Microsoft.Office.Interop.Word.Shape watermarkShape;
                            watermarkShape = headerFooter.Shapes.AddPicture(watermarkPath, ref linkToFile, ref saveWithDocument, ref refmissing,
                                                                            ref refmissing, ref refmissing, ref refmissing, ref refmissing);
    
                            watermarkShape.Left = Convert.ToSingle(WdShapePosition.wdShapeLeft);
                            watermarkShape.Top = Convert.ToSingle(WdShapePosition.wdShapeCenter);
                        }
                        catch (System.Runtime.InteropServices.COMException e)
                        {
                            MessageBoxResult result = MessageBox.Show(String.Format("{0} - Error Code = {1}", e.Message, e.ErrorCode));
                            throw e;
                        }
                    }
                }
    
                oWord.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
                oWord.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Could not find watermark image");
            }
    
            //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
            string outstringFile = "";
            string pathfile = System.IO.Path.GetDirectoryName(filename);
            string filenameFile = System.IO.Path.GetFileName(filename);
            outstringFile = pathfile + "\\OG" + filenameFile;
    
            Object oSaveAsFile = (Object)(outstringFile);
    
            wordFile.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
    
            //CLOSING THE FILE
            wordFile.Close(ref oFalse, ref oMissing, ref oMissing);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little code: alert(1); $('input[name^=Quantity_]').each(function () { alert(2); $(this).rules(add, { required: true,
I have this little piece of jQuery/jqModal code which works fine. However, I am
its a little bit hard to understand. in the header.php i have this code:
Ok. I have this little bit of code that is supposed to use text
I have this little piece of code : private Dictionary<string, IList<KeyValuePair<int, string>>> EnumsCollection =
I have this code that creates a little speech bubble. I am going to
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I have this little script to toggle a contact form when a button is
I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.