// in your button click handler, just call PrintOut() function
 private void ButtonClickHandler(object sender, EventArgs e)
 {
     // if doc == null, open the document
     if (doc == null)
     {
         // here i assume fileName has been assigned
         doc = word.Documents.Open(fileName, ReadOnly: readOnly, Visible: isVisible);
     }
     doc.PrintOut();
 }
示例#2
0
        // ---------------------------------------------
        //             Print Document
        // ---------------------------------------------
        public static void PrintDocument(object fromFileName)
        {
            if (!File.Exists((string)fromFileName))
            {
                MessageBox.Show("File not found. " + fromFileName);
                return;
            }


            Word.Application vkWordApp =
                new Word.Application();

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            vkMyDoc.PrintOut();

            vkMyDoc.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(vkMyDoc);

            vkWordApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(vkWordApp);

            return;
        }