Solved

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

Posted on 2006-06-18
3
630 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now