[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:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Sub Command1_Click()
ShellExecute Me.hWnd, "Print", "C:\MyFile.doc", vbNullString, "C:\", SW_HIDE
End Sub
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
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>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnPrint;
/// <summary>
/// Required designer variable.
/// </summary>
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
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code
/// <summary>
/// The main entry point for the application.
/// </summary>
[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;
}
}
}
}