Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net and ms word

Posted on 2009-04-11
9
Medium Priority
?
391 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
[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
  • 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

705 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