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

vb.net and ms word

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
jagguy
Asked:
jagguy
  • 5
  • 4
1 Solution
 
jagguyAuthor Commented:
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
 
GrahamSkanRetiredCommented:
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
 
jagguyAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
GrahamSkanRetiredCommented:
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
 
GrahamSkanRetiredCommented:
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
 
jagguyAuthor Commented:
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
 
GrahamSkanRetiredCommented:
That is probably because you have some invisible instances of Word open. Use Task Manger to kill them.
0
 
jagguyAuthor Commented:
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
 
jagguyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now