Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking VB with Word

Status
Not open for further replies.

huBBLe

Programmer
Joined
May 15, 2001
Messages
50
Location
SG
I have a standard report format in Word with empty fields. These fields will take the data from my VB application and fill it in accordingly. It's like filling in the blanks of the report.

I do not know how to go about doing this. I've read up on the Data Report Utility but it seems to be for connection between a database and the VB app.

The OLE looks like what I need but most of the examples I've seen are about embedding Word/ Excel in the application and not about saving the data to the Word document with the correct data going to their respective fields. I do not even need to open Word.

Any hints on how I can achieve this?

Thank You!
 
====================================================================
Check out this MSDN link. For all those exporting data to Office, it appears that new versions of Office can break your code if you use early binding.
Click on VB Frequently asked questions.
====================================================================
"If you are developing an Automation client for both Microsoft Access 97 and 2000, you should not use early binding: late binding is recommended. The Access 2000 object model was modified such that it breaks both binary (vtable) and dispid compatibility. Any client application that uses early or dispid binding to Access 97 might fail to work properly when run against Access 2000."
====================================================================

Below is what I use to get the benefits of IntelliSense without using early binding in production.
......
Code:
#If blnDesign then
    Dim objAppl as Word.Application ' Need reference to Type lib
    Dim objDoc  as word.document
#Else
    Dim objAppl as object 
    Dim objDoc  as object
#End if
You can set blnDesign in the Project Properties for the compile or define it in your code.
#Const blnDesign = True ' set to false when production compile


Do not use 2000 options not present in '97.

When you want to know how to do something. RECORD A MACRO of what you want to do and look at the generated code. The article says this but it needs to be reiterated.
'================================================================
 
Thanks for the direction John. At least now I know where to head.
 
Here is some code that I've used many times. In your Word document, you will need to create some Bookmarks, then you can tell VB where to place what text:

Dim WordObject As Word.Application
Set WordObject = CreateObject("Word.Application")

With WordObject
.Documents.Open ("c:\program files\qcap\transmove.doc")
.ActiveDocument.Bookmarks("transtype").Select
' reapply the boomark name to the selection
'.ActiveDocument.Bookmarks.Add Name:="First",Range:=Selection.Range
.ActiveDocument.Bookmarks("fromlocation").Select
.Selection.Text = (lblMoveFrom.Caption)
.ActiveDocument.Bookmarks("tolocation").Select
.Selection.Text = (lblMoveTo.Caption)
.ActiveDocument.Bookmarks("numbers").Select
.Selection.Text = (txtStatus.Text)
.ActiveDocument.Bookmarks("preparedby").Select
.Selection.Text = (UCase(UserAccount))
.ActiveDocument.Bookmarks("comments").Select
.Selection.Text = (txtComments.Text)
.ActiveDocument.Bookmarks("movedate").Select
.Selection.Text = (Today)
End With
WordObject.Visible = False
WordObject.ActiveDocument.PrintOut Background:=False
WordObject.ActiveDocument.SaveAs FileName:="x.doc"
' setting the printing to background will display the document
' on the screen while it is being printed
' WordObject.Documents.Close '("c:\program files\qcap\transmove.doc")
WordObject.Application.Documents.Close
Set WordObject = Nothing

fromlocation, tolocation, etc are all Bookmarks on my Word Template.

Hope this helps.
 
Hi there,
I'm trying to do the same thing as huBBLe;exporting data to specific fields in a word document. I want to ask, would you still use the OLE with the code that Hackster supplied or would you use the code in something else?
ie. a command button

Thanx
Sw0rdfish149
 
What I do is create a Word template containing the fields and put the table.fieldname containing the source data as the default text in each field.

Then it's simple to create a doc based on the template, iterate the FormFields collection, read the default text in each field and replace with data (via the Result property).

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top