MSDN > Home page del forum > Visual C# General > can I create & print Word document without saving it as file?
Formula una domandaFormula una domanda
 

Con rispostacan I create & print Word document without saving it as file?

  • giovedì 5 novembre 2009 16.52Yoss613 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi,
    I wan't to print a word xml (2003) file from memory without saving it to the disk before.
    My code now - with physical file:
    Microsoft.Office.Interop.Word.Application oApp=new Application();
    Microsoft.Office.Interop.Word.Document oDoc=new Document();
    oDoc = oApp.Documents.Open(ref FileName....        Can I use here memory stream or something like that?
    oDoc.PrintOut(ref objTrue...

    Thanks,
    Yoss

Risposte

  • giovedì 5 novembre 2009 17.14Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Hi,

    You can use the code like following.

    Note:This is for Office 2007 but using in office 2003 is same.Only parameters may differ.

    Microsoft.Office.Interop.Word.Application insApplication = new Microsoft.Office.Interop.Word.Application();
                System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
                object missing = System.Reflection.Missing.Value;
                object objFalse = false;
                object objTrue = false;
                try
                {
    
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    
                    Microsoft.Office.Interop.Word.Document insDocument;
                    insApplication.Visible = false;
    
                    object Template = Type.Missing;
                    object newTemplate = false;
                    object DocumentType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
                    object Visible = true;
    
                    insDocument = insApplication.Documents.Add(ref Template, ref newTemplate, ref DocumentType, ref Visible);
    
    
                    Microsoft.Office.Interop.Word.Range r;
                    object start = 0;
                    object end = 0;
                    r = insDocument.Range(ref start, ref end);
    
                    r.Text = "Sample";
                    insDocument.PrintOut(ref objTrue, ref objTrue, 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, ref missing);
                    insApplication.Quit(ref objFalse, ref missing, ref missing);
                    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
                }
                catch (Exception ex)
                {
                    
                    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
    
                    Console.WriteLine(ex);
                   
                }
    
    • Proposto come rispostaTamer OzMVPvenerdì 6 novembre 2009 18.28
    • Contrassegnato come rispostaYoss613 domenica 8 novembre 2009 5.44
    •  

Tutte le risposte

  • giovedì 5 novembre 2009 17.14Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Hi,

    You can use the code like following.

    Note:This is for Office 2007 but using in office 2003 is same.Only parameters may differ.

    Microsoft.Office.Interop.Word.Application insApplication = new Microsoft.Office.Interop.Word.Application();
                System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
                object missing = System.Reflection.Missing.Value;
                object objFalse = false;
                object objTrue = false;
                try
                {
    
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    
                    Microsoft.Office.Interop.Word.Document insDocument;
                    insApplication.Visible = false;
    
                    object Template = Type.Missing;
                    object newTemplate = false;
                    object DocumentType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
                    object Visible = true;
    
                    insDocument = insApplication.Documents.Add(ref Template, ref newTemplate, ref DocumentType, ref Visible);
    
    
                    Microsoft.Office.Interop.Word.Range r;
                    object start = 0;
                    object end = 0;
                    r = insDocument.Range(ref start, ref end);
    
                    r.Text = "Sample";
                    insDocument.PrintOut(ref objTrue, ref objTrue, 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, ref missing);
                    insApplication.Quit(ref objFalse, ref missing, ref missing);
                    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
                }
                catch (Exception ex)
                {
                    
                    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
    
                    Console.WriteLine(ex);
                   
                }
    
    • Proposto come rispostaTamer OzMVPvenerdì 6 novembre 2009 18.28
    • Contrassegnato come rispostaYoss613 domenica 8 novembre 2009 5.44
    •  
  • domenica 8 novembre 2009 5.51Yoss613 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi Tamer,

    Thank you very much!

    Yoss.