Solved

vb.net and ms word

Posted on 2009-04-11
9
377 Views
Last Modified: 2013-12-17
I can write to a word doc using this code from vb.net.

1)THis doesnt save the file name so how do i do this?
2)how do i open and existing word doc to append to the table?
3)is this the right way to connect to ms word as i had  to use add a reference. I thought i could have used an imports instead ?
Imports System.IO

Imports Microsoft.Office
 
 

Public Class Form1
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim oApp As Word.Application

        Dim oDoc As Word.Document
 

        'Start a new document in Word            

        oApp = CType(CreateObject("Word.Application"), Word.Application)

        oDoc = oApp.Documents.Add()
 

        ' Clear out any existing information.

        oDoc.Range.Delete()
 

        ' Set up the header information.

        Dim rng As Word.Range = oDoc.Range(0, 0)

        rng.Font.Name = "Verdana"

        rng.Font.Size = 16
 

        Dim tlb As Word.Table = oDoc.Tables.Add(Range:=rng, NumRows:=4, NumColumns:=3)
 

        'Filling data to table

        'Header Row

        tlb.Cell(1, 1).Range.Text = "Code"

        tlb.Cell(1, 2).Range.Text = "Name"

        tlb.Cell(1, 3).Range.Text = "Amount"
 

        'Data Row

        tlb.Cell(2, 1).Range.Text = "0001"

        tlb.Cell(2, 2).Range.Text = "Sandip"

        tlb.Cell(2, 3).Range.Text = "1000"

        '    tlb.Cell(2, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
 

        tlb.Cell(3, 1).Range.Text = "0002"

        tlb.Cell(3, 2).Range.Text = "Susmit"

        tlb.Cell(3, 3).Range.Text = "2000"

        '   tlb.Cell(3, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
 

        tlb.Cell(4, 1).Range.Text = "0003"

        tlb.Cell(4, 2).Range.Text = "Jon Doe"

        tlb.Cell(4, 3).Range.Text = "8000"

        '  tlb.Cell(4, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
 

        oApp.Visible = True

    End Sub

Open in new window

0
Comment
Question by:jagguy
  • 5
  • 4
9 Comments
 

Author Comment

by:jagguy
ID: 24124966
this does not write to the file i want and for some reason i have 2 doc files opened.
   Dim oApp As New Word.Application

        Dim oDoc As New Word.Document
 

        'Start a new document in Word            

  

        oApp.Documents.Open("d:\andrew visual basic\randomMath2\template.doc")
 
 

        ' Clear out any existing information.

        oDoc.Range.Delete()
 

        ' Set up the header information.

        Dim rng As Word.Range = oDoc.Range(0, 0)

        rng.Font.Name = "Verdana"

        rng.Font.Size = 16
 

        Dim tlb As Word.Table = oDoc.Tables.Add(Range:=rng, NumRows:=4, NumColumns:=2)
 

        'Filling data to table

        'Header Row

        tlb.Cell(1, 1).Range.Text = "Code"

        tlb.Cell(1, 2).Range.Text = "Name"
 
 

        'Data Row

        tlb.Cell(2, 1).Range.Text = "0001"

        tlb.Cell(2, 2).Range.Text = "Sandip"

  

        tlb.Cell(3, 1).Range.Text = "0002"

        tlb.Cell(3, 2).Range.Text = "Susmit"

   

        tlb.Cell(4, 1).Range.Text = "0003"

        tlb.Cell(4, 2).Range.Text = "Jon Doe"

 
 

        oApp.Documents.Save(, "d:\andrew visual basic\randomMath2\template.doc")

        oApp.Documents .Close  

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24125080
1. You can save the file with
oDoc.SaveAs "MyFile.Doc"

2. To Open a file

oDoc = oApp.Documents.Open("C:\MyPath\MyFile.doc")

3. You do need to add a reference if you want to use early binding. The alternative is to Dim the Word objects simply as Object.


0
 

Author Comment

by:jagguy
ID: 24125140
no this isnt working.

if you look at the code above i cant write data to a word doc which has a table.
I want to write data to an existing table and i dont want the word doc opened as i do it.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24125366
Sorry. I had the browser open at this question for quite a while and didn't see your comment.

Try this:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim oApp As New Word.ApplicationClass()

        Dim oDoc As New Word.Document()
 

        'Start a new document in Word            

        oApp.Visible = True 'while testing - to avoid hidden instances of Word after aborts
 

        oDoc = oApp.Documents.Open("d:\andrew visual basic\randomMath2\template.doc")
 
 

        ' Clear out any existing information.

        'oDoc.Range.Delete()
 

        ' Set up the header information.

        Dim rng As Word.Range = oDoc.Range(0, 0)

        rng.Font.Name = "Verdana"

        rng.Font.Size = 16
 

        Dim tlb As Word.Table

        tlb = oDoc.Tables.Item(1)
 

        'Filling data to table

        'Header Row

        tlb.Cell(1, 1).Range.Text = "Code"

        tlb.Cell(1, 2).Range.Text = "Name"
 
 

        'Data Row

        tlb.Cell(2, 1).Range.Text = "0001"

        tlb.Cell(2, 2).Range.Text = "Sandip"
 

        tlb.Cell(3, 1).Range.Text = "0002"

        tlb.Cell(3, 2).Range.Text = "Susmit"
 

        tlb.Cell(4, 1).Range.Text = "0003"

        tlb.Cell(4, 2).Range.Text = "Jon Doe"
 
 

        oApp.Documents.Save()

        oApp.Documents.Close()

        oApp.Quit()

    End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24125461
I used Word.ApplicationClass to overcome an error regarding ambiguity in using
oApp.Quit.

I have found several places where this is said to be wrong, though I can't understand why. Nevertheless I now recommend using

CType(oApp, Word._Application).Quit()

returning to

Dim oApp As New Word.Application

0
 

Author Comment

by:jagguy
ID: 24125530
it works but a saveAs for normal.dot appears all the time and it is hard ti get rid of.
how can i avoid this and what is normal.dot again?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 24125541
That is probably because you have some invisible instances of Word open. Use Task Manger to kill them.
0
 

Author Comment

by:jagguy
ID: 24125548
ok i checked task manager and got rid of word instances.
what happens is that if i now open template.doc it says file in use etc. IT isnt in use so what do i do?

How do i do saveAs for this because i need to save the file under a different name than template .doc as this is a template?
        oApp.Documents.Save()
0
 

Author Comment

by:jagguy
ID: 24125604
i have vb.net express 2008 and word 2002
i need to be able to fix these problems as below or i cant use this.

what happens is that if i now open template.doc it says file in use etc. IT isnt in use so what do i do?

How do i do saveAs for this because i need to save the file under a different name than template .doc as this is a template?
        oApp.Documents.Save()
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TT Auto Dashboard 13 79
This code tracks birthdays 3 56
Definitions and default visual studio colors 5 58
Tool Box 2 31
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now