Solved

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

Posted on 2014-03-25
7
222 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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