?
Solved

printing a word document with vb .net

Posted on 2005-03-14
7
Medium Priority
?
227 Views
Last Modified: 2010-04-23
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
Comment
Question by:Sonny9
[X]
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
7 Comments
 
LVL 8

Accepted Solution

by:
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()
        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
 

Author Comment

by:Sonny9
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)
          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
 
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
        MyProcess.Start()
0
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
0
 
LVL 8

Expert Comment

by:bman9111
ID: 13544567
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
 

Author Comment

by:Sonny9
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.
0
 

Author Comment

by:Sonny9
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.
0

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