Solved

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

Posted on 2014-03-25
7
218 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
Comment Utility
if (masterDoc == null) {
      masterDoc = new Microsoft.Office.Interop.Word.Document();  
}
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
That not working.  When the code gets to

masterDoc.Activate();

it errors.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
Saved the document in the initialize method.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

11 Experts available now in Live!

Get 1:1 Help Now