printing a word document with vb .net

Posted on 2005-03-14
Medium Priority
Last Modified: 2010-04-23
     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.
Question by:Sonny9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Accepted Solution

bman9111 earned 400 total points
ID: 13536936
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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.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"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(496, 262)
        Me.Name = "Form1"
        Me.Text = "Form1"

    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)
        wordapp = Nothing

    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....

Author Comment

ID: 13538536
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)
          appExcel = Nothing

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.
LVL 70

Expert Comment

by:Éric Moreau
ID: 13541335
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 70

Expert Comment

by:Éric Moreau
ID: 13541339
I forgot to say that you have to do this import:

Imports System.Diagnostics

Expert Comment

ID: 13544567
easy one change this line:

Dim wbkExcel As New Excel.Workbook


 Dim wbkExcel As Excel.Workbook

I just took the "New" out of the line, should work now.

Author Comment

ID: 13545972
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.

Author Comment

ID: 13546001
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.

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question