Solved

C# - How to determine if a variable has value or needs to be initialzed.

Posted on 2014-03-25
7
223 Views
Last Modified: 2014-04-01
I converted a 2008 app to 2012.  Below is a snippet of code i'm working with.  I want to know in the Process method if I need to initialize my masterDoc variable.  If it was used already I want to use what is in the local variable if not then I want to initialize.

Any ideas?

public class PrintApp
{
    protected Microsoft.Office.Interop.Word.Application wApp;
    protected Microsoft.Office.Interop.Word.Document masterDoc;

    private void Initialize()
    {
            masterDoc = new Microsoft.Office.Interop.Word.Document();  
            masterDoc.Application.Visible = false;
    }

    private void Process()
    {
        //Want to use value that was initialized previously.
        masterDoc = new Microsoft.Office.Interop.Word.Document();  
        masterDoc.Select();
        masterDoc.SaveAs
    }
}

Open in new window

0
Comment
Question by:CipherIS
7 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39954566
if (masterDoc == null) {
      masterDoc = new Microsoft.Office.Interop.Word.Document();  
}
0
 
LVL 1

Author Comment

by:CipherIS
ID: 39954585
That not working.  When the code gets to

masterDoc.Activate();

it errors.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39954589
I don't see any "Activate" in your code.

If it is a NullReferenceError you may need to do the initialization before Activate is called.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 39955327
you can use like below.

        // Open a doc file.
	Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application();
	Microsoft.Office.Interop.Word.Document masterDoc = wApp.Documents.Open("C:\\word.doc");

	// Loop through all words in the document.
	int count = masterDoc.Words.Count;
	for (int i = 1; i <= count; i++)
	{
	    // Write the word.
	    string text = masterDoc.Words[i].Text;
	    Console.WriteLine("Word {0} = {1}", i, text);
	}
	// Close word.
	wApp.Quit();

Open in new window

0
 
LVL 40
ID: 39955353
I cannot test where I am now, but if my memory is good, you cannot Activate a document when the application is not Visible. This makes sense. What is the use of activating something that is not visible.

Note that you do not need to activate the document to work with it with code. A lot of the stuff you see when recording a macro assume that the user is working with the document, and is thus useless when manipulating the document through code.
0
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 39959327
I resolved it by adding the below code to the Initialize Method.  This results in the reference being kept throughout the code.

    object oDoc = "masterDoc.docx";
    masterDoc.SaveAs(ref oDoc, ref missing, ref missing, ref missing, ref missing,
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
           ref missing, ref missing, ref missing,ref missing, ref missing);

private void Initialize()
{
    wApp = new Microsoft.Office.Interop.Word.Application();
    //wApp.Visible = false;
    wApp.Visible = true;

    wApp.Options.SuggestSpellingCorrections = false;
    wApp.Options.CheckGrammarAsYouType = false;
    wApp.Options.CheckSpellingAsYouType = false;
    wApp.Options.CheckGrammarWithSpelling = false;
    
    masterDoc = new Microsoft.Office.Interop.Word.Document();
            missing = System.Reflection.Missing.Value;
    oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
    masterDoc = wApp.Documents.Add(ref missing, ref missing, ref missing, ref oFalse);
    //masterDoc.Application.Visible = false;
    masterDoc.Application.Visible = true;

    //BELOW IS THE FIX.  I GAVE MASTERDOC A NAME AND IT KEEPS IT REFERENCE 
    //THROUGHOUT THE CODE
    object oDoc = "masterDoc.docx";
    masterDoc.SaveAs(ref oDoc, ref missing, ref missing, ref missing, ref missing, 
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
           ref missing, ref missing, ref missing,ref missing, ref missing);

    masterDoc.Application.Options.SuggestSpellingCorrections = false;
    masterDoc.Application.Options.CheckGrammarAsYouType = false;
    masterDoc.Application.Options.CheckSpellingAsYouType = false;
    masterDoc.Application.Options.CheckGrammarWithSpelling = false;
}

Open in new window


Thank You.
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 39968809
Saved the document in the initialize method.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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