jtrapat1
asked on
Call Word Document Template From VB Program
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
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"
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
etc.
Will I still be able to control this template thru VB code?
Thanks in advance.
John
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.d oc", BamFileName
Set MyWord = New Word.Application
BamMSWord.CreateLink BamFileName
MyWord.Documents.Open BamFileName, , BamReadOnly
MyWord.Visible = True
MyWord.ActiveDocument.Book marks("Bil lNum").Sel ect
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book marks("Spo nsor").Sel ect
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.Book marks("Tit le").Selec t
MyWord.Selection.Text = Trim(txtTitle) & vbCr & vbLf
MyWord.ActiveDocument.Book marks("Ana lyst").Sel ect
MyWord.Selection.Text = Initials
MyWord.ActiveDocument.Book marks("Bil lNum2").Se lect
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book marks("Ana lyst2").Se lect
MyWord.Selection.Text = Trim(AnalystArray(1, 2))
MyWord.ActiveWindow.Active Pane.View. Type = wdPageView
MyWord.ActiveWindow.Active Pane.View. SeekView = wdSeekCurrentPageFooter
MyWord.ActiveDocument.Book marks("Bil lNum3").Se lect
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book marks("Sen ateStat"). Select
If RsSenateActions.BOF And RsSenateActions.EOF Then
MyWord.Selection.Text = ""
Else
MyWord.Selection.Text = RsSenateActions!Action
End If
MyWord.ActiveWindow.Active Pane.View. SeekView = wdSeekMainDocument
MyWord.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
MyWord.Selection.Find.Clea rFormattin g
MyWord.ActiveDocument.Save
MyWord.Width = 600
MyWord.Height = 440
MyWord.Left = 0
MyWord.Top = 0
End If
Thaks
John
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.d
Set MyWord = New Word.Application
BamMSWord.CreateLink BamFileName
MyWord.Documents.Open BamFileName, , BamReadOnly
MyWord.Visible = True
MyWord.ActiveDocument.Book
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book
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.Book
MyWord.Selection.Text = Trim(txtTitle) & vbCr & vbLf
MyWord.ActiveDocument.Book
MyWord.Selection.Text = Initials
MyWord.ActiveDocument.Book
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book
MyWord.Selection.Text = Trim(AnalystArray(1, 2))
MyWord.ActiveWindow.Active
MyWord.ActiveWindow.Active
MyWord.ActiveDocument.Book
MyWord.Selection.Text = txtBillNum
MyWord.ActiveDocument.Book
If RsSenateActions.BOF And RsSenateActions.EOF Then
MyWord.Selection.Text = ""
Else
MyWord.Selection.Text = RsSenateActions!Action
End If
MyWord.ActiveWindow.Active
MyWord.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
MyWord.Selection.Find.Clea
MyWord.ActiveDocument.Save
MyWord.Width = 600
MyWord.Height = 440
MyWord.Left = 0
MyWord.Top = 0
End If
Thaks
John
nzjonboy