Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-25
7
Medium Priority
?
226 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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