Microsoft 开发人员网络 > 论坛主页 > Visual C# General > can I create & print Word document without saving it as file?
提出问题提出问题
 

已答复can I create & print Word document without saving it as file?

  • 2009年11月5日 16:52Yoss613 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

答案

  • 2009年11月5日 17:14Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    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);
                   
                }
    
    • 已建议为答案Tamer OzMVP2009年11月6日 18:28
    • 已标记为答案Yoss613 2009年11月8日 5:44
    •  

全部回复

  • 2009年11月5日 17:14Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    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);
                   
                }
    
    • 已建议为答案Tamer OzMVP2009年11月6日 18:28
    • 已标记为答案Yoss613 2009年11月8日 5:44
    •  
  • 2009年11月8日 5:51Yoss613 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Tamer,

    Thank you very much!

    Yoss.