Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I print a document without opening it?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    [FAQ's: OD] How do I print a document without opening it?

    To print a Word document without opening it is best to use the ShellExecute API function call for VB 6 coding.

    If your using .NET then you will want to use the Process class.


    Word 2003 And VB 6 Code Example:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_HIDE As Long = 0
    7. Private Const SW_SHOWNORMAL As Long = 1
    8. Private Const SW_SHOWMINIMIZED As Long = 2
    9. Private Const SW_SHOWMAXIMIZED As Long = 3
    10.  
    11. Private Sub Command1_Click()
    12.     ShellExecute Me.hWnd, "Print", "C:\MyFile.doc", vbNullString, "C:\", SW_HIDE
    13. End Sub
    Last edited by RobDog888; Aug 23rd, 2006 at 04:13 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I print a document without opening it?

    Word 97-2003 And VB.NET 2003/2005 Code Example:
    Code:
    Option Explicit On 
    Option Strict On
    
    Public Class Form1
    
        Inherits System.Windows.Forms.Form
    
        Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            Dim oProcess As New System.Diagnostics.Process
            With oProcess.StartInfo
                .CreateNoWindow = True
                .WindowStyle = ProcessWindowStyle.Hidden
                .Verb = "print"
                .UseShellExecute = True
                .FileName = "C:\MyFile.doc"
            End With
            oProcess.Start()
        End Sub
    
    End Class
    Last edited by RobDog888; May 27th, 2008 at 01:30 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I print a document without opening it?

    Word 2003 And C# 2003 Code Example:
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace Print_Word_CS
    {
        /// 
        /// Summary description for Form1.
        /// 
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button btnClose;
            private System.Windows.Forms.Button btnPrint;
            /// 
            /// Required designer variable.
            /// 
            private System.ComponentModel.Container components = null;
            private Word.Application moApp;
            public Form1()
            {
                // Required for Windows Form Designer support
                InitializeComponent();
                // TODO: Add any constructor code after InitializeComponent call
            }
            /// 
            /// Clean up any resources being used.
            /// 
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
    
            Windows Form Designer generated code
    
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
    
            private void btnClose_Click(object sender, System.EventArgs e)
            {
                this.Close();
            }
    
            private void btnPrint_Click(object sender, System.EventArgs e)
            {
                System.Diagnostics.Process oProcess = new System.Diagnostics.Process();
                oProcess.StartInfo.CreateNoWindow = true;
                oProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                oProcess.StartInfo.Verb = "print";
                oProcess.StartInfo.UseShellExecute = true;
                oProcess.StartInfo.FileName = this.txtFilePath.Text;
                oProcess.Start();
            }
    
            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                object a = false;
                object r_missing = System.Reflection.Missing.Value;
                if (moApp.Documents.Count > 0)
                {
                    moApp.Documents.Close(ref a, ref r_missing, ref r_missing);
                }
                moApp.Quit(ref a, ref r_missing, ref r_missing);
                moApp = null;
            }
    
            private void Form1_Load(object sender, System.EventArgs e)
            {
                moApp = new Word.Application();
            }
    
            private void btnBrowse_Click(object sender, System.EventArgs e)
            {
                System.Windows.Forms.FileDialog oDlg = new OpenFileDialog();
                oDlg.CheckFileExists = true;
                oDlg.CheckPathExists = true;
                oDlg.Filter = "Word Documents Only (*.doc)|*.doc";
                oDlg.FilterIndex = 1;
                oDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                oDlg.ShowHelp = false;
                oDlg.Title = "RobDog888s Office FAQ";
                if (oDlg.ShowDialog() == DialogResult.OK)
                {
                    this.txtFilePath.Text = oDlg.FileName;
                }
            }
        }
    }
    Last edited by RobDog888; Apr 20th, 2007 at 04:21 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width