Solved

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

Posted on 2014-03-25
7
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

617 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