Solved

vb.net and ms word

Posted on 2009-04-11
9
372 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

12 Experts available now in Live!

Get 1:1 Help Now