2 Replies - 495 Views - Last Post: 21 June 2012 - 09:14 AM Rate Topic: -----

#1 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Dispose word document after printing

Posted 20 June 2012 - 10:32 PM

HI,
I have to print some content using word document, so that I created word document and print content and its work very fine and I able to get desired result but the thing is that when i shut down my pc(not my application) it asked me to
save book1 and this kind of many dialog boxes which automatically close and pc shut down properly .
I want to avoid this dialog boxes and the main trouble is that when i use my application on laptop it asked me same dialog box but they don't close and i have to close all this dialog boxes manually.
Actually I don't want to save any document i only want print.
so any solution to how to dispose this word document properly I tried many ways but I still getting this dialog boxes.

I only pasting here my main code
  private void Print_PromNote(int iGridRowNo, decimal dInstallments)
        {
   #region // Create & Print Word Document

                #region //The C# application developer starts Microsoft Word by creating a new Word application object:
                //Microsoft.Office.Interop.Word.Application oWord = new Application();
            
                Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
 
                //By default Microsoft Word will be run invisibly in the background. However, it can be useful to see the document during the development phase of the project:
                //oWord.Visible = true;

                // the programmer just needs to create an object that will handle these missing values:
                object oMissing = System.Reflection.Missing.Value;

                //this object can be used when the new document is created:
                Microsoft.Office.Interop.Word.Document oDoc = oWord.Documents.Add
                                      (ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            
                #endregion
             
                #region //**Adding Paragraphs to a Microsoft Word Document with C#

                //The C# Programmer adds content to a Microsoft Word document by adding paragraphs:
                Microsoft.Office.Interop.Word.Paragraph oPara1;
                oPara1 = oDoc.Paragraphs.Add(ref oMissing);

                oWord.Activedocument.PageSetup.TopMargin = (float)150;
                oWord.Activedocument.PageSetup.LeftMargin = (float)150;
                oWord.Activedocument.PageSetup.RightMargin = (float)60;

                //Any formatting of the paragraph can take place at this point. For example the paragraph can be turned into a heading:
                object styleHeading1 = "Heading 1";
                oPara1.Range.set_Style(ref styleHeading1);
                oPara1.Range.Font.Underline = WdUnderline.wdUnderlineSingle;
                //And, of course, the programmer can add text to the paragraph:
                oPara1.Range.Text = "PROMISSORY NOTE";
                oPara1.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                oPara1.Range.Font.Color= WdColor.wdColorBlack;
                oPara1.Range.Font.Name = "Engravers MT";
                oPara1.Range.Font.Size = 14;
                //oPara1.LineSpacing = 0.8f;
                //If further paragraphs are to be added then one final statement is required:
                oPara1.Range.InsertParagraphAfter();

                //This statement ensures that adding further paragraphs will not overwrite the existing one. Any new paragraphs are added in exactly the same way:
                Microsoft.Office.Interop.Word.Paragraph oPara2;
                oPara2 = oDoc.Paragraphs.Add(ref oMissing);
                oPara2.Range.Text = sPN_Content;
                oPara2.Range.Select();
                oPara2.Range.Font.Size = 14;
                oPara2.Range.Font.Name = "Times New Roman";
                oPara2.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
                oPara2.LineSpacingRule = WdLineSpacing.wdLineSpaceDouble;
                //oPara2.Range.InsertAfter(" Vishu");
                oPara2.Range.InsertParagraphAfter();
  
                Microsoft.Office.Interop.Word.Paragraph oPara3;
                oPara3 = oDoc.Paragraphs.Add(ref oMissing);
                oPara3.Range.Text = "For " + sPNCompanyName;
                oPara3.Range.Font.Size = 14;
                oPara3.Range.Bold = 1;
                oPara3.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle;
                oPara3.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                oPara3.Range.InsertParagraphAfter();

                Microsoft.Office.Interop.Word.Paragraph oPara4;
                oPara4 = oDoc.Paragraphs.Add(ref oMissing);
                oPara4.Range.Text = System.Environment.NewLine + System.Environment.NewLine + sPNSIgnature;
                oPara4.Range.Font.Size = 14;
                oPara4.Range.Bold = 1;
                oPara4.LineSpacingRule = WdLineSpacing.wdLineSpaceDouble;
                oPara4.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                oPara4.Range.InsertParagraphAfter();
                
                Microsoft.Office.Interop.Word.Paragraph oPara5;
                oPara5 = oDoc.Paragraphs.Add(ref oMissing);
                string sFooter = "Important :- Please submit the Promissory Note at the time of Maturity.";
                oPara5.Range.Text = sFooter;
                oPara5.Range.Bold = 0;
                oPara5.Range.Font.Size = 8;
                oPara4.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle;
                oPara5.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                oPara5.Range.InsertParagraphAfter();

                if (sInvestmentMode.Trim() == "CHEQUE")
                {
                    Microsoft.Office.Interop.Word.Paragraph oPara6;
                    oPara6 = oDoc.Paragraphs.Add(ref oMissing);
                    string sFooter1 = "Subject to TDS deduction.";
                    oPara6.Range.Text = sFooter1;
                    oPara6.Range.Bold = 0;
                    oPara6.Range.Font.Size = 8;
                    oPara6.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    oPara6.Range.InsertParagraphAfter();
                }

                object myTrue = true; // Print in background
                object myFalse = false;
                    oDoc.PrintOutOld(ref myTrue,
                ref myFalse, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref myFalse,
                    ref oMissing, ref oMissing, ref oMissing);

            //In the example above the new paragraphs will just have the default formatting (as can be see in figure 2).

                #endregion

               // return;

                #region //Saving a Microsoft Word Document with C#
                    ////Once the document contains all of the necessary information then it can be saved:
                    //object fileName = @"C:\Documents and Settings\User\Desktop\Test.doc";
                    //oDoc.SaveAs(ref fileName,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing,
                    //ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    ////However, it should be noted that this overwrites an existing file of the same name.
                    ////Closing a Microsoft Word Document

                    ////Finally the document can be closed:
                    //oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                    //oDoc = null;
                    //As can the Word application:
                    //oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                    //oWord = null; // Free up any memory used

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
           
                    #endregion

            #endregion
}



Is This A Good Question/Topic? 0
  • +

Replies To: Dispose word document after printing

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Dispose word document after printing

Posted 21 June 2012 - 07:52 AM

You could try calling garbage collection i do this when using Excel although Calling the GC is not best pratice as far as i am aware but try it and see if i makes a difference

GC.Collect(); 
GC.WaitForPendingFinalizers(); 


Was This Post Helpful? 0
  • +
  • -

#3 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Dispose word document after printing

Posted 21 June 2012 - 09:14 AM

All GC.Collect() does is a very expensive call which allows garbage to be collected on all levels. It doesn't actually force collection.

Using this method it basically for proper basket cases.

When to call GC.Collect()

I can't see where in the OPs code is appropriate to use it, to be honest I wasn't sure if this is what OP meant by "dispose of document" but proper disposing should involve the Using Statement.

using (Document doc = new Document("C:\MyDoc.txt"))
{
     //"doc" in scope within these brackets

     document.name = "example";
     //more code
}
// Brackets left, "doc" now out of scope. Disposed and Closed automatically.


This post has been edited by negligible: 21 June 2012 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1