none
I dont want to open my Word Doc once created, how i can fix RRS feed

  • Question

  • i want to created document by using office intro, i am now able to generate my word doc , but aftre generating my doc get open too i dont want to open , how i can prevent,

    even i am not using

    wordApp.Documents.Open,below code please let me know how i can fix

    public bool GetDOC(object saveas)
        {
          try
          {
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
    
            //Start Word and create a new document.
            Microsoft.Office.Interop.Word._Application oWord;
            Microsoft.Office.Interop.Word._Document oDoc;
            oWord = new Microsoft.Office.Interop.Word.Application();
            oWord.Visible = true;
    
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
              ref oMissing, ref oMissing);
    
            //Insert a paragraph at the beginning of the document.
            Microsoft.Office.Interop.Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;  //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();
    
            //Insert a paragraph at the end of the document.
            Microsoft.Office.Interop.Word.Paragraph oPara2;
            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara2.Range.Text = "Heading 2";
            oPara2.Format.SpaceAfter = 6;
            oPara2.Range.InsertParagraphAfter();
    
            //Insert another paragraph.
            Microsoft.Office.Interop.Word.Paragraph oPara3;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
            oPara3.Range.Font.Bold = 0;
            oPara3.Format.SpaceAfter = 24;
            oPara3.Range.InsertParagraphAfter();
    
            //Insert a 3 x 5 table, fill it with data, and make the first row
            //bold and italic.
            Microsoft.Office.Interop.Word.Table oTable;
            Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
            oTable.Range.ParagraphFormat.SpaceAfter = 6;
            int r, c;
            string strText;
            for (r = 1; r <= 3; r++)
              for (c = 1; c <= 5; c++)
              {
                strText = "r" + r + "c" + c;
                oTable.Cell(r, c).Range.Text = strText;
              }
            oTable.Rows[1].Range.Font.Bold = 1;
            oTable.Rows[1].Range.Font.Italic = 1;
    
            //Add some text after the table.
            Microsoft.Office.Interop.Word.Paragraph oPara4;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara4.Range.InsertParagraphBefore();
            oPara4.Range.Text = "And here's another table:";
            oPara4.Format.SpaceAfter = 24;
            oPara4.Range.InsertParagraphAfter();
    
            //Insert a 5 x 2 table, fill it with data, and change the column widths.
            wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);
            oTable.Range.ParagraphFormat.SpaceAfter = 6;
            for (r = 1; r <= 5; r++)
              for (c = 1; c <= 2; c++)
              {
                strText = "r" + r + "c" + c;
                oTable.Cell(r, c).Range.Text = strText;
              }
            oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2
            oTable.Columns[2].Width = oWord.InchesToPoints(3);
    
            //Keep inserting text. When you get to 7 inches from top of the
            //document, insert a hard page break.
            object oPos;
            double dPos = oWord.InchesToPoints(7);
            oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
            do
            {
              wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
              wrdRng.ParagraphFormat.SpaceAfter = 6;
              wrdRng.InsertAfter("A line of text");
              wrdRng.InsertParagraphAfter();
              oPos = wrdRng.get_Information
                      (Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
            }
            while (dPos >= Convert.ToDouble(oPos));
            object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
            object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
            wrdRng.Collapse(ref oCollapseEnd);
            wrdRng.InsertBreak(ref oPageBreak);
            wrdRng.Collapse(ref oCollapseEnd);
            wrdRng.InsertAfter("We're now on page 2. Here's my chart:");
            wrdRng.InsertParagraphAfter();
    
    
    
            oDoc.SaveAs(ref saveas, 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);
    
            //Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    
            //Microsoft.Office.Interop.Word.Document aDoc = null;
            //aDoc = wordApp.Documents.Open(ref saveas, 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);
    
          }
          catch (Exception ex)
          {
            Console.Write(ex.Message);
          }
          return true;
        }
    • Moved by Cindy Meister MVPModerator Saturday, January 22, 2011 6:33 AM not using VSTO technology (From:Visual Studio Tools for Office)
    Thursday, January 20, 2011 9:24 PM

Answers

  • As you're not working with the VSTO technology, I'm going to move your question to the Word Developer forum.

    However, it's not possible to generate a Word document using the "interop" without also showing it. If you want to generate a document without showing it, you don't use the "interop" - you create the file directly. Usually, that would mean using the Office Open XML file format. For more information on this, see the resources at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, February 9, 2011 2:30 AM
    Saturday, January 22, 2011 6:32 AM
    Moderator
  • I don't know the language so I'm not sure of all the details, but you Save the document you create, but you do not Close it, so it remains open. Either add a Close or replace the SaveAs with a Close (SaveChanges=True).
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Bruce Song Wednesday, February 9, 2011 2:30 AM
    Sunday, January 23, 2011 1:28 PM

All replies

  • As you're not working with the VSTO technology, I'm going to move your question to the Word Developer forum.

    However, it's not possible to generate a Word document using the "interop" without also showing it. If you want to generate a document without showing it, you don't use the "interop" - you create the file directly. Usually, that would mean using the Office Open XML file format. For more information on this, see the resources at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, February 9, 2011 2:30 AM
    Saturday, January 22, 2011 6:32 AM
    Moderator
  • I don't know the language so I'm not sure of all the details, but you Save the document you create, but you do not Close it, so it remains open. Either add a Close or replace the SaveAs with a Close (SaveChanges=True).
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Bruce Song Wednesday, February 9, 2011 2:30 AM
    Sunday, January 23, 2011 1:28 PM