How To Automate Microsoft Word From Visual Basic

admin  13.04.2020  No Commentson How To Automate Microsoft Word From Visual Basic

Feb 05, 2011  This tutorial shows you how to automate Microsoft Office Word using a simple C# application and Windows Forms. Language:Visual C# Created. Jul 25, 2015  How to create ms word templates This video shows how to create a template in MS Word using Quick parts, Fields, Bookmarks and VBA macros. Templates are useful for automating word.

Hi guys,
I need to put the results of a query to an acces database onto a word document. now the query is driven by user choices so it might return one record or it might return over 300 records or then again, nothing at all. but if there is at least one record returned, this is needed to be desplayed on a word document that is going to have to be designed on the fly in code. since I am new to programming (not a student, working!!) i would like some help on this please. i have got as far as getting the info out of the database, just need help on putting it into a word document.

  • 2 Contributors
  • forum1 Reply
  • 335 Views
  • 3 Days Discussion Span
  • commentLatest Postby choudhuryshouviLatest Post

choudhuryshouvi33

Hi guys,
I need to put the results of a query to an acces database onto a word document. now the query is driven by user choices so it might return one record or it might return over 300 records or then again, nothing at all. but if there is at least one record returned, this is needed to be desplayed on a word document that is going to have to be designed on the fly in code. since I am new to programming (not a student, working!!) i would like some help on this please. i have got as far as getting the info out of the database, just need help on putting it into a word document.

I've a code that acts like the similar way u want.Try this code.I think it will give u an idea how to do word writing from vb programs.If u still have problems then mail me.My email id is :
choudhuryshouvik@yahoo.com

Insert the following references into your project before trying to run this code :
Microsoft Word <version no> Object Library
from Project->References

Here is the code:-
'add a textbox(text1).also add two command buttons and name 'them cmdclose & cmdword respectively.

Option Explicit
Dim wrdapp As Word.Application

Private Sub cmdclose_Click()
On Error GoTo cerror

wrdapp.ActiveDocument.Close
wrdapp.Quit

cerror:
If Err.Number = 4198 Then
Dim ex As Integer
ex = MsgBox('Do you want to save this file or quit ?', vbYesNo + vbQuestion, 'Microsoft Word with VB')
If ex = vbNo Then
'Close the current document
wrdapp.ActiveDocument.Close
'Close Word
wrdapp.Quit
ElseIf ex = vbYes Then
Exit Sub
End If
'Exit Sub
End If
End Sub

Private Sub cmdword_Click()
Set wrdapp = New Word.Application
With wrdapp
'Show Word
.Visible = True
'Create New Document
.Documents.Add
'Add text to the document
.ActiveDocument.Content.Text = 'Hi, ' & Trim(Text1.Text)
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set wrdapp = Nothing
End Sub

Using COM to automate Word from your Visual Basic application isn’t very hard. Just did a sample for a friend and decided to share it. Not that the sample is very complicated or anything, quite the opposite in fact. But it does show how easy it is to get started [:)]

The sample code below shows how to construct a minimal document, insert a bookmark and, at a later moment, replace the empty bookmark with some text.

Free version microsoft word. Imports Microsoft.Office.Interop.Word

Module Module1


Sub Main()


Dim word As
New Application

word.Visible = True


Dim doc As Document = word.Documents.Add()


Dim range As Range = doc.Range


‘ Insert some text

range.InsertAfter(“Range1” + vbCrLf)


‘ Insert a bookmark, make sure the range is 0 characters long so we can insert text

range.Collapse(WdCollapseDirection.wdCollapseEnd)

doc.Bookmarks.Add(“MijnBookmark”, range)


‘ Add some more text

range.InsertAfter(“Range2” + vbCrLf)


‘ Get a reference to the bookmark


Dim bookmark As Bookmark = doc.Bookmarks(1)

range = bookmark.Range


‘ Insert the text

range.Text = “Bookmark” + vbCrLf


‘ Change the font for the bookmark

range.Font.Color = WdColor.wdColorRed

range.Font.Italic = 1


End
Sub

WordWord visual basic examples

End
Module

One thing to keep in mind when automating Word is that, in general, it is better to use Range objects instead of the Selection. One reason is that there is only one selection while you can have multiple ranges but there is also a performance penalty to using the selection. And don’t forget to install the Office PIA’s when using Office through COM from .NET.

Another useful thing is the Word Macro Recorder. It is still there in Word 2007 but hidden away in the Developer Ribbon bar. Kind of strange as the macro recorder was always the simplest way of automating Word for the average user and they typically don’t have the developer ribbon visible.

Enjoy!