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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Word Document Template From VB Program

Status
Not open for further replies.

jtrapat1

Programmer
Jan 14, 2001
137
US
I'm using VB6 against an SQL Server database.
Our VB app currently uses a MS Word Document as a template for the many documents we have stored on our server.
We do a FileCopy "\\SERVER\FOLDER\orig.doc" FileName
and then set up the fields.

I'm trying to add a macro to this word document but I'm getting very confused.
Now,this document is not saved as a template;it's a word document with labels for the field names.
When I try to add my macro, I open orig.doc,go to Tools>Macro>Visual Basic Editor and then paste the macro in a module.
But these changes get saved back to my template (normal.dot) back in the default directory at C:\Program Files\Microsoft Office\Office\Templates.

Can I add a macro to this word document that we're using as a template;or,do I have to save it as a template?
If so, can I still manipulate this template from VB?
I mean,we're using commands like:
dim wrd as new word.application
set tmp = word.application
application.documents.open filename,,readonly
etc.

Will I still be able to control this template thru VB code?
Thanks in advance.
John


 
Go to and search on "Office Automation". I do not use templates. I use a document, fill it in, Print it in Word then Quit with no save.
USE LATE BINDING. Early binding will break your code eventually.
BUT I NEED INTELLISENSE.
Fine. Use the Type Library for compiling, testing and providing constants but not for a final compile. The constants probably will not change but the early binding vTables will.
Use conditional compilation to get the best of both worlds.
Code:
#Const blnProduction = false
#If blnProduction then
    Dim objAppl as Word.Application
    Dim objAppl as Word.Document
#Else
    Dim objAppl as object 'Word.Application
    Dim objAppl as object 'Word.Document
#End if
 
Thanks for the tips.

Can you take a look at my code and give me an opinion on whether I should stick with a .doc as a template or switch to a .dot.
I have a project which opens bill files (word documents) in an instance of word when an ole object is double-clicked.

Here's the routine that opens the file in a word instance:

Private Sub BamMSWord_Click()
Set MyWord = New Application
MyWord.Documents.Open BamFileName, , BamReadOnly
MyWord.Visible = True
MyWord.Width = 600
MyWord.Height = 440
MyWord.Left = 0
MyWord.Top = 0
End Sub

The .doc template is used when a new bill file gets created:
I would need the macro to be inside the word document when it is opened.
The macro is a simple FileSaveAs() procedure that helps the users to save to their own D:\drives and not on the server (or current directory)

Here:
'where orig.doc is the template with some labels on it-
and the fields are setup with bookmarks

FileCopy "\\POOH\piglet\bams\orig.doc", BamFileName
Set MyWord = New Word.Application
BamMSWord.CreateLink BamFileName
MyWord.Documents.Open BamFileName, , BamReadOnly
MyWord.Visible = True
MyWord.ActiveDocument.Bookmarks("BillNum").Select
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Bookmarks("Sponsor").Select
If RsBill!rules = "1" Then
MyWord.Selection.Text = "RULES(At the Request of M. of A. " & Trim(txtSponsor) & ")"
Else
MyWord.Selection.Text = "M. of A. " & Trim(txtSponsor)
End If
MyWord.ActiveDocument.Bookmarks("Title").Select
MyWord.Selection.Text = Trim(txtTitle) & vbCr & vbLf
MyWord.ActiveDocument.Bookmarks("Analyst").Select
MyWord.Selection.Text = Initials
MyWord.ActiveDocument.Bookmarks("BillNum2").Select
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Bookmarks("Analyst2").Select
MyWord.Selection.Text = Trim(AnalystArray(1, 2))
MyWord.ActiveWindow.ActivePane.View.Type = wdPageView
MyWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
MyWord.ActiveDocument.Bookmarks("BillNum3").Select
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Bookmarks("SenateStat").Select
If RsSenateActions.BOF And RsSenateActions.EOF Then
MyWord.Selection.Text = ""
Else
MyWord.Selection.Text = RsSenateActions!Action
End If
MyWord.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
MyWord.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
MyWord.Selection.Find.ClearFormatting
MyWord.ActiveDocument.Save
MyWord.Width = 600
MyWord.Height = 440
MyWord.Left = 0
MyWord.Top = 0
End If

Thaks
John
 
I can't tell you what is better. I use DocVariables rather than Bookmarks and .Doc rather than .Dot only because that is what I stumbled accross first. I keep meaning to look up Bookmarks.

I also put all my logic into VBA modules inside the document so that I could pass an "unloaded" dictionary object and tell the documents to print themselves. I don't want to know in my program anything but what data ALL the documents need, even though I endede up doing all the coding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top