Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-18
3
Medium Priority
?
654 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
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

877 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