Microsoft Developer Network > Página principal de foros > Visual C# General > can I create & print Word document without saving it as file?
Formular una preguntaFormular una pregunta
 

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

  • jueves, 05 de noviembre de 2009 16:52Yoss613 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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

Respuestas

  • jueves, 05 de noviembre de 2009 17:14Tamer OzMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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);
                   
                }
    
    • Propuesto como respuestaTamer OzMVPviernes, 06 de noviembre de 2009 18:28
    • Marcado como respuestaYoss613 domingo, 08 de noviembre de 2009 5:44
    •  

Todas las respuestas

  • jueves, 05 de noviembre de 2009 17:14Tamer OzMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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);
                   
                }
    
    • Propuesto como respuestaTamer OzMVPviernes, 06 de noviembre de 2009 18:28
    • Marcado como respuestaYoss613 domingo, 08 de noviembre de 2009 5:44
    •  
  • domingo, 08 de noviembre de 2009 5:51Yoss613 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi Tamer,

    Thank you very much!

    Yoss.