Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Word Automation problem - Error:"Use of unassigned local variable oDoc"

Posted on 2006-06-18
3
Medium Priority
?
649 Views
Last Modified: 2008-02-01
Hello,

Got a frustrating problem here.

Here's what I'm trying to do:
-grab some data from a db store it into a dataset.
-iterate through dataset, writing 3 records at a time to a word doc, then save the file.
-then create a new word doc..grab 3 more records, create a new file..etc...until there is no more records in the dataset.


The code worked great when dealing with a single record, but after adding the loop and the logic to only write out 3 recs per file, I get the following compile time error:
"Use of unassigned local variable'oDoc'" at this line in the code "oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);"


I believe this is occurring because I am implicitly creating an instance of the document by using the "add method" on the application object, and "oDoc" is now not in scope because it's defined within the "if block",  but I'm not really sure how to get this work though.

Here is a simplified version of the code, using a for loop. Any ideas would be greatly appreciated.

Thanks


       private void button2_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;
           Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            Word.Paragraph oPara1;
            oWord.Visible = true;

            int fileNameCounter = 0;
            int numOfItems = 0;
          
            for (int i = 0; i < 12; i++)
            {
                numOfItems++;

                if (numOfItems == 1)
                {
                    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }

                //Insert a paragraph at the beginning of the document.
                oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara1.Range.Text = ""WRITE A RECORD TO WORD";
                oPara1.Range.InsertParagraphAfter();


                if (numOfItems == 3)
                {
          fileNameCounter++;

                    string filePath = @"C:\_Temp\Test" + fileNameCounter + ".DOC";
                    object filePath2 = (object)filePath;
                    oDoc.SaveAs(ref filePath2,
                        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);

                    //Release the reference.
                   
                    oWord = null;
                }
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
               
            } //ends for loop

            MessageBox.Show("files created");
               
           
        }
0
Comment
Question by:rfrankson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
dttri earned 2000 total points
ID: 16932611
Greetings,
I think this block of code causes the compile time error:
<<<<<<
    if (numOfItems == 1)
                {
                    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }

                //Insert a paragraph at the beginning of the document.
                oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara1.Range.Text = ""WRITE A RECORD TO WORD";
                oPara1.Range.InsertParagraphAfter();
>>>>>>>>
As you can see, if numOfItems isn't equal to 1, then oDoc is not initialized, and then the line "oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);" will cause the error because it access the content of the unassigned variable oDoc.

Try to change the code to the block below to see if this help:

<<<<CODE>>>>>>>>>
private void button2_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            Word.Paragraph oPara1;
            oWord.Visible = true;

            int fileNameCounter = 0;
            int numOfItems = 0;

            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);   //add this line so that the compiler don't warning the "use of unassigned local variable"
         
            for (int i = 0; i < 12; i++)
            {
                numOfItems++;

                if (numOfItems == 1)
                {
                    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }

                //Insert a paragraph at the beginning of the document.
                oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                oPara1.Range.Text = ""WRITE A RECORD TO WORD";
                oPara1.Range.InsertParagraphAfter();


                if (numOfItems == 3)
                {
                   fileNameCounter++;
                   numOfItems=0;           //reset the numOfItems variable to zero for the next document

                   string filePath = @"C:\_Temp\Test" + fileNameCounter + ".DOC";
                   object filePath2 = (object)filePath;
                   oDoc.SaveAs(ref filePath2,
                        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);
                }
               
            } //ends for loop

            MessageBox.Show("files created");
        }
<<<<END CODE>>>>>>>>>
0
 

Author Comment

by:rfrankson
ID: 16934116
Thank you very much dttri, that did the trick.
0
 
LVL 7

Expert Comment

by:dttri
ID: 16935245
You're welcome! And thanks for the point!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question