subhashini
(Moderator): This is Subhashini Simha (Community Co-ordinator
, Microsoft India ) , the moderator for this chat .
subhashini (Moderator):
Please welcome Naresh Nichani Who is the Expert for today's chat. Naresh
Nichani manages a software consulting company ‘GL Infotech’
based in Chennai. He develops and manages projects in Visual Basic
(VB.Net and VB 6.0) and MS SQL Server. He also does VBA programming
in MS-Access, MS-Excel and MS Visio.
He has completed his MBA (Finance) from University of Massachusetts,
Lowell and had worked for Village Software (www.villagesoft.com)
for a year. Village Software is a key developer of spreadsheet applications
(the free templates in MS-Office 97 and MS-Office 2000 are from
Village Software).
He enjoys programming with Microsoft technologies as they are fairly
easy to use and developers can build fairly complex solutions for
customers with visually appealing interfaces quickly. He is currently
working on a DHTML Interface, which encapsulates the Web Browser
control to create a “web” like interface in a Window
subhashini (Moderator): This chat will last for
one hour. During this hour, our Expert will respond to as many questions
as they can. Please understand that there may be some questions
we cannot respond to due to lack of information or because the information
is not yet public. We encourage you to submit questions for our
Experts. We ask that you stay on topic for the duration of the chat.
This helps the Guests and Experts follow the conversation more easily.
We invite you to ask off topic questions after this chat is over,
but not during.
Please only submit your question once. Submitting your question
more than once is against the Chat Code of Conduct
subhashini (Moderator):
Hi everybody, the chat will start at 5.00 pm IST
subhashini (Moderator):
Today is a discussion on MS Word Object Model
subhashini (Moderator):
we discuss:
1. Key top level objects and their relationships - Application,
Documents, Document, Paragraphs and Range.
2. Automation from .NET and VB6?
3. Usage of Bookmarks to Insert Text from database at specific points
in Word Reports
Naresh_MVP (Expert):
We will start from basics and have a Q&A at end of session (try
and have 30 mins on Q&A)
subhashini (Moderator):
Welcme Naresh
Naresh_MVP (Expert):
Many of use MS-Word for creating documents - the idea in this chat
is to see how we can autoamte Word to do reporting from our business
apps
Naresh_MVP (Expert):
For example I have a SQL db or Access db with some data - I can
generate a lot of reports. Clients may like to see a report in Word
automatically generated from database
Naresh_MVP (Expert):
To do this we could use Word VBA, Visual Basic 6 or .Net langauages
Naresh_MVP (Expert):
We would use Word VBA if the whole solution is to be built in Word.
If our app is already in Vb6 or .Net we can automate Word from these
languages
Naresh_MVP (Expert):
Good question on no Word Installed
Naresh_MVP (Expert):
You can if you have Office 2003 and generate WordML (this is XML
version of Word)
Naresh_MVP (Expert):
Otherwise you need Word installed on machine where app is running
Naresh_MVP (Expert):
To automate Word we need to first instantiate the Word Application
Object from either Vb6 or .Net
Naresh_MVP (Expert):
TO do this you can make make a reference to "Microsoft Word
Object Library" in Vb6 or .Net (in .Net this is a COM Component)
Naresh_MVP (Expert):
Once you have a reference set to Word you can refer to Word objects
in your code.
Naresh_MVP (Expert):
In .Net after making the reference type this line at the top of
your class
Naresh_MVP (Expert):
Hello Everybody and welcome
Naresh_MVP (Expert):
You cannot use Word viewer to generate Word output using AUtomation
Naresh_MVP (Expert):
You need WOrd installed or generate WordML which will open only
in Word 2003
Naresh_MVP (Expert):
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Word
Naresh_MVP (Expert):
Try
wApp = GetObject(, "Word.Application")
Catch e2 As Exception
If wApp Is Nothing Then
wApp = CreateObject("Word.Application")
End If
If wApp Is Nothing Then
MsgBox("Word is not installed")
End If
End Try
Try
wApp.Visible = True
wdDoc = wApp.Documents.Add("C:\NN\Abc.Dot")
wdBook = wdDoc.Bookmarks("Abc")
wdBoo
Try
wApp = GetObject(, "Word.Application")
Catch e2 As Exception
If wApp Is Nothing Then
wApp = CreateObject("Word.Application")
End If
If wApp Is Nothing Then
MsgBox("Word is not installed")
End If
End Try
Try
wApp.Visible = True
wdDoc = wApp.Documents.Add("C:\NN\Abc.Dot")
wdBook = wdDoc.Bookmarks("Abc")
wdBook.Range.Text = "HaHa"
Catch e1 As Exception
MsgBox(e1.ToString)
End Try
Naresh_MVP (Expert):
The above code is sample .Net code to instatiate Word
Naresh_MVP (Expert): The
first step here is to check if Word is already running on user system
Naresh_MVP (Expert): To
do that use this line with a property Try Catch loop as it may fail
if Word is not running
Naresh_MVP (Expert): wApp
= GetObject(, "Word.Application")
Naresh_MVP (Expert): This
looks for a running Word Application and will fail if Word is not
running.
Naresh_MVP (Expert): If
Exception is raised then we must instantiate a new instance of Word
Naresh_MVP (Expert): Can
we wait for Questions till end of this - few minuites please
Naresh_MVP (Expert): This
creates a new instance of Word
Naresh_MVP (Expert): If
wApp Is Nothing Then
wApp = CreateObject("Word.Application")
End If
Naresh_MVP (Expert): THe
Word instance you create this way is invisible and the next line
should be
Naresh_MVP (Expert): wApp.Visible
= True
Naresh_MVP (Expert): Once
you have a Word Application Object open you can create a new document
(which may be based on a template) or open a existing document
Naresh_MVP (Expert): To
create a new Document first declare a Word Document Object like
this
Naresh_MVP (Expert): Dim
wdDoc as Document
Naresh_MVP (Expert): Then
to create a new open
Naresh_MVP (Expert): Set
wdDoc = wApp.Document.Add
Naresh_MVP (Expert): This
create a blank new Word document
Naresh_MVP (Expert): You
may want to open a new document based on a Word Template instead
which contains your report layout already and you can insert your
dat at specific points
Naresh_MVP (Expert): To
do this
Naresh_MVP (Expert): wdDoc
= wApp.Documents.Add("C:\NN\Abc.Dot")
Naresh_MVP (Expert): Here
"C:\NN\ABC.Dot" is the Word tempalte
Naresh_MVP (Expert): To
open a existing document write code like this
Naresh_MVP (Expert): wdDoc
= wApp.Documents.Open("C:\Abc.Doc")
Naresh_MVP (Expert): Once
we have a document open we can insert data at specific points in
the document. To do this in Word Template is is reccommended to
create Bookmarks (Use Insert | bookmark) for this
Naresh_MVP (Expert): You
can refer to a Bookmark in code like this
Naresh_MVP (Expert): Dim
wBook as Bookmark
Naresh_MVP (Expert): Set
wBook = wdDoc.Bookamrks("NN")
Naresh_MVP (Expert): Here
NN is a Bookmark in your document (you can could have made this
in Word template)
Naresh_MVP (Expert): To
write some text from a datbaase to a bookmark do this
Naresh_MVP (Expert): wBook.Range.Text
= "Some Text Here"
Naresh_MVP (Expert): If
you need to set Bookmark dynamically you will already know where
to insert the text and can do so directly
Naresh_MVP (Expert): However
you can insert bookmarks directly like this
Naresh_MVP (Expert): wBook
= wdDoc.Bookmarks.Add
Naresh_MVP (Expert): This
will add a bookmark at the selection point
Naresh_MVP (Expert):
In my projects where we do word reporting I normally have a Word
template with a lot of bookmarks - I then use code and take data
from db and add to these bookmarks
Naresh_MVP (Expert): All
formatting etc. is in template
Naresh_MVP (Expert): However
there are times when you need to add data in a table (many rows
of data)
Naresh_MVP (Expert): In
this case bookmark the first cell in table
Naresh_MVP (Expert): You
can select a bookmark like this
Naresh_MVP (Expert): wdDoc.Bookmarks("NN").Select
Naresh_MVP (Expert): Once
selected you can type text at Selection like this
Naresh_MVP (Expert): wApp.Selection.TypeText
"This is Some Text"
Naresh_MVP (Expert): To
move to next cell in table do this
Naresh_MVP (Expert): wApp.Selection.MoveRight
Naresh_MVP (Expert): This
way we can move from cell to cell and fill data in the cells in
table
Naresh_MVP (Expert): Yes
Word table
Naresh_MVP (Expert): This
way anything to do in Word can be automated with the Word Object
Model
Naresh_MVP (Expert): We
can do Mail Merge, Merge data from database to Template, Create
Good looking reports, Statements etc
Naresh_MVP (Expert): At
this point I will take some questions
Naresh_MVP (Expert): You
can create a header row. Then create a single blank row with a bookmark
at first cell of blank row. Activate bookmark and use MoveRight
to keep moving right - this will add new rows as you need
Naresh_MVP (Expert): wApp.Selection.MoveDown
Naresh_MVP (Expert): To
move to next cell do this
Naresh_MVP (Expert): Selection.MoveRight
Unit:=wdCell
Naresh_MVP (Expert): Do
not do "wApp.Selection.MoveRight" do "wApp.Selection.MoveRight
Unit:=wdCell" instead
Naresh_MVP (Expert): To
make work in all versions of Word use Late Binding - do not create
a Word reference.
Naresh_MVP (Expert): Instead
do like this
Naresh_MVP (Expert): Dim
wApp as Object 'this is Word.Application
Naresh_MVP (Expert): Do
not use constants like wdCell instead use their numerical value
Naresh_MVP (Expert): To
get the numerical value for a Word constant open the Word VBE and
type CTRL + G to get the Debug window. Here type ?wdCell and press
Enter to see the value of constant
Naresh_MVP (Expert): If
you use Late Binding code can be slower to execute - however it
will work with all versions. Also you will not get Intellisense
when typing code
Naresh_MVP (Expert): To
make a text bold etc. use the Range or Selection object like this
Naresh_MVP (Expert): wApp.Selection.Bold
= True
Naresh_MVP (Expert): This
would imply text is selected
Naresh_MVP (Expert): Otherwise
use a Range object
Naresh_MVP (Expert): A
Word Range object referes to a continuous set of text
Naresh_MVP (Expert): Dim
wRange as Range
Naresh_MVP (Expert): Set
wRange = wdDoc.Range(1,10)
Naresh_MVP (Expert): This
selects the first 10 characters in document
Naresh_MVP (Expert): Another
way
Naresh_MVP (Expert): Set
wRange = wdDoc.Paragraphs(1)
Naresh_MVP (Expert): This
range refers to first paragraph
Naresh_MVP (Expert): With
a range you can do this
Naresh_MVP (Expert): wRange.Font.Bold
= True
Naresh_MVP (Expert): To
make a report read only protect document with a password
Naresh_MVP (Expert): If
you use the Selection object Yes you need to reset it back. If using
Range you do not
Naresh_MVP (Expert): If
you use Selection and the selection is bold and you type next text
it will continue in Bold (kind of like Word)
Naresh_MVP (Expert): In
Word you have Tool | Protect Document with an option to make it
Read Only unless password is supplied
Naresh_MVP (Expert):
You can automate this as well like this
Naresh_MVP (Expert):
wdDoc.Protect wdAllowOnlyReading,,"Password"
Naresh_MVP (Expert): Here
"Password" is the Password to unlock for read write mode
Naresh_MVP (Expert):
To put text in the nth row
Naresh_MVP (Expert):
wdDoc.Tables(1).Rows(10).Select
Naresh_MVP (Expert):
This selects the 10th row in first table in Document
Naresh_MVP (Expert):
Bold and Italic are seperate properties of Range object and selection
object
Naresh_MVP (Expert):
Other option is to make a Style in Word which is Bold+Italic and
apply that style to Selection or Range
Naresh_MVP (Expert):
Selection.TypeText "Hello"
Naresh_MVP (Expert):
Selection.Font.Bold = True
Naresh_MVP (Expert):
Selection.TypeText " Naresh"
Naresh_MVP (Expert):
Selection.Font.Bold = False
Naresh_MVP (Expert):
To print quickly can use Draft Mode
Naresh_MVP (Expert):
You can only automate what is possible in Word
Naresh_MVP (Expert):
If Word had a fast printing option then you could automate that
as well
Naresh_MVP (Expert):
Idea would be to change Printer to a "fast" printer via
code and then print
Naresh_MVP (Expert): DOS
Printing is fast agreed - Windows printing is more formatted etc
- with a good laser printer it will print fast 
Naresh_MVP (Expert): Reference
links http://word.mvps.org/
and http://homepage.swissonline.ch/cindymeister/CMpriv.htm
Naresh_MVP (Expert):
Q: Can I do that without having installed MS Word
on the Machine?
A: No unless your client has Word 2003 - you can
generate WordML this is XML version of Word doc
Naresh_MVP (Expert):
Q: In Dot.Net of early binding and Late Binding
things are there?
A: Yes .Dot supports Late Binding but does not
recommend it - it works well though
Naresh_MVP (Expert):
Q: how do i set the book mark dynamically
A: wBook = wdDoc.Bookmarks.Add
Naresh_MVP (Expert):
Q: If I want to generate a report which is compatible
with all the earlier versions, Should I Reference to multiple Dlls
of one Single Dll?
A: Use Late Binding
Naresh_MVP (Expert):
Q: If I want to generate report which is compatible
to earlier version, should i make a reference to multiple Dlls or
Single Dlls
A: Use Late Bidning
Naresh_MVP (Expert):
Q: No, I want to show the report to the user, but
he should not alter the contents?
A: Protect the Word doc like this wd.Protect wdAllowOnlyReading,,"Password"
Naresh_MVP (Expert):
Q: You mean to say Word Table not the Database
Table
A: Yes
Naresh_MVP (Expert):
Q: do we need to define no rows and cols well in
advance
A: It is generatlly best to define cols as we may
know them - the rows may vary depending on data in db
Naresh_MVP (Expert):
Q: how do one move down in a column
A: Selection.MoveDown Unit:=wdCell
Naresh_MVP (Expert):
A: For i = 1 to 10
Selection.MoveDown Unit:=wdCell
Next
Naresh_MVP (Expert):
A: wdDoc.Tables(1).Cell(1,1).Range.Font.Bold =
true
Naresh_MVP (Expert):
Q: plz provide some working code samples and reference
links
A: http://word.mvps.org/
and http://homepage.swissonline.ch/cindymeister/CMpriv.htm
Naresh_MVP (Expert):
Q: how do i make the report as read only programmatically?
A: wdDoc.Protect wdAllowOnlyReading,,"Password"
Naresh_MVP (Expert):
Q: Is Word 2003 Object Model different from earlier
versions like (Word 98)Office 98?
A: Yes the Object Model is much richer (support
for XML), and all new Word features are part of Object Model
Naresh_MVP (Expert):
Q: After making the selecction as bold...do we
need to reset the bold option?
A: wApp.Selection.Font.Bold = False
Naresh_MVP (Expert):
Q: If am generating a report for a product like
tally, how do i control the end-user printer?
A: You can change the Default Printer like this
ActivePrinter = "Fax"
Here "Fax" is the printer to print on
Naresh_MVP (Expert):
Q: How should we dynamically add rows to a table
and populate our data.
A: When last cell is active do this
wApp.Selection.MoveRight Unit:=wdCell
Naresh_MVP (Expert):
Q: Is there any way to print the
report in dotmatrix type ( I do not know the exact word) which is
very fast. Otherwise This Windows Type report will be very slow
A: Change the printer like this
wApp.ActivePrinter = "Fax"
Naresh_MVP (Expert):
Q: Is there any Interface which
common to all the version of word object model so that we can write
common code?
A: Yes Application, Documents, Document, Rage objects
are all basic objects you can use in any Word version. If you use
a specific feature of a new version in a old version you will get
a error.If you test in Word 97 you are safe it will work in newer
versions
Naresh_MVP (Expert):
Q: How do i Embedd the word doc in .net Application
like OLE in vb.60
A: Use Visual Studio Tools for Office in Whidbley
subhashini (Moderator): We
have the last 10 mins
subhashini (Moderator):
SO suggest all of you to ask teh last few questions
Naresh_MVP (Expert):
Q: Can I make a text of a particular cell bold
or mark as a link
A: To mark as Link
wddDoc.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"http://www.d-tools.com",
SubAddress:="", ScreenTip:="", TextToDisplay:=
_
"http://www.d-tools.com"
Naresh_MVP (Expert): My
Email ID for any questions
nareshnichani@hotmail.com
subhashini (Moderator): You
can also mail Naresh at nareshnichani@hotmail.com
subhashini (Moderator): for
any additional questions
Naresh_MVP (Expert):
Q: Is that fast printing is called DOS Printing?
A: Change Priner again here and set Print Mode
to Draft
Naresh_MVP (Expert):
Q: Can I tranform rows into columns and vice versa
just like pivot table reporting?
A: Not sure - can be done via code for sure - but
not a one-liner
Naresh_MVP (Expert):
Q: If I am inserting Dynamically How do I put the
Text in the Nth row
A: For i = 1 to n
wApp.Selection.MoveDown Unit:=wdCell
Next
wApp.Selection.TypeText "Hi"
Naresh_MVP (Expert):
Q: how do i generate a report
in other languages like Hindi, Tamil, Kannada everything through
Code Only?
A: If you have a Unicode db you could read form
there and insert? Otherwise not sure on this
Naresh_MVP (Expert):
Q: Can we take it granted that all the code given
here will be work from ASP.NET (I mean in from web forms)
A: In Web Forms better not to automate Word per
MS. You could use WordML to generate WordXML and show in Word 2003.
Instantiating Word on server is not scalable and uses a lot of memory
Naresh_MVP (Expert):
Q: My Database will be in UNICODE, but how do i
put that into the report?
A: If you read the data in say a DataReader you
can insert like this
wApp.Selection.TypeText rd.GetString(0)
Naresh_MVP (Expert): MS
wants you to get a good laser printer nowadays I guess
Naresh_MVP (Expert): Thanks
all - email if if anything. Have fun with Word
Naresh_MVP (Expert): Sleep
more it may solve the problem sometimes 
subhashini (Moderator): Great
subhashini (Moderator): Thanks
Naresh
subhashini (Moderator): for
the informative chat
subhashini (Moderator): and
thanks to all of you
subhashini (Moderator): who
attended this chat taking time out from your busy schedule
subhashini (Moderator): The
next chat is scheduled on Dec 15'th
subhashini (Moderator): Curtain
Raiser: Visual Studio Team System
by Sudhakar Sadasivuni (MVP)
subhashini (Moderator): Have
a great evening
|