Solved

vb.net and ms word

Posted on 2009-04-11
9
380 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

809 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