• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

printing a word document with vb .net

Hi,
     I want to open and then print a word document from a windows application i am writing in vb.net.  The document is on my computer in c:\budget\budget05.doc.  is there a fairly  easy way to do this?  Thanx.
0
Sonny9
Asked:
Sonny9
  • 3
  • 2
  • 2
1 Solution
 
bman9111Commented:
thats pretty easy

mports Microsoft.Office.Interop.Word
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 56)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(128, 48)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(496, 262)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wordapp As New Microsoft.Office.Interop.Word.Application
        Dim worddoc As New Microsoft.Office.Interop.Word.Document

        worddoc = wordapp.Documents.Open("C:\test.doc", , False)
        worddoc.PrintOut()
        wordapp = Nothing
        worddoc.Close()



    End Sub
End Class



now u have to reference ur word. I have 11.0 and this is the reason why my

   Dim wordapp As New Microsoft.Office.Interop.Word.Application
   Dim worddoc As New Microsoft.Office.Interop.Word.Document

if u have 9.0 or 10.0 then
should be
   Dim wordapp As New Word.Application
      Dim worddoc As New Word.Document

same for import

let me know if this is what u wanted....
0
 
Sonny9Author Commented:
Thank You very much!  It works great.

I am doing same thing with excel file but I get the error below when it execute second line.  I used Workbook because Doument wasn't available as an option.

          Dim appExcel As New Excel.Application
          Dim wbkExcel As New Excel.Workbook
          wbkExcel = appExcel.Workbooks.Open(sFileName, , False)
          wbkExcel.PrintOut()
          wbkExcel.Close()
          appExcel = Nothing

error:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PrintBudget.exe

Additional information: COM object with CLSID {00020819-0000-0000-C000-000000000046} is either not valid or not registered.
0
 
Éric MoreauSenior .Net ConsultantCommented:
You can also use the Process component:

        Dim MyProcess As New Process

        'Set the file to open
        MyProcess.StartInfo.FileName = "c:\temp\test.doc"
        MyProcess.StartInfo.Verb = "Print"
        'Start the process
        MyProcess.Start()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Éric MoreauSenior .Net ConsultantCommented:
I forgot to say that you have to do this import:

Imports System.Diagnostics
0
 
bman9111Commented:
easy one change this line:

Dim wbkExcel As New Excel.Workbook

to

 Dim wbkExcel As Excel.Workbook


I just took the "New" out of the line, should work now.
0
 
Sonny9Author Commented:
Thank You bman9111.  You were very helpful.

emoreau, I am sure your answers work, but I don't want to mess with something that I finaly got ot work.  But thank you for your time.
0
 
Sonny9Author Commented:
I just wanted to say after taking the new out of that line it now works fine.  I don't know why it did not work with new in, I am sure bman9111 does.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now