I
Iain Cowden
Hi all,
With a little help from some folks on another forum I have
a macro that I want to run automatically when I open a new
document based on a given template (FDA.dot).
The macro opens a text file generated by my database.
It then assigns each line of the text file to a document
variable. It then hopefully populates the document
variable fields in the document created from the template.
However, I cannot get it to work. My hunch is that this is
due to the Option Explicit command. This cannot be placed
inside the autonew subroutine, but doesn't appear to run
if left outside of it.
Can anyone help me re-write this macro so that it works
properly?
The code is as follows:-
Option Explicit
Private Type AddressData
forename As String
surname As String
adr1 As String
adr2 As String
adr3 As String
postcode As String
dob As String
End Type
Sub Autonew()
'
' Autonew Macro
' Macro created Tuesday, October 19, 1999 by snoop
'
Const PATH As String = "c:\"
Const DATAFILE As String = "appdata.txt"
Const INCLUDETEXT As String
= "INCLUDETEXT ""f:\\FDA.dot"""
Dim oTemp As AddressData 'temporary storage of data
from datafile
Dim oDoc As Document
Set oDoc = ThisDocument
Open PATH & DATAFILE For Input As #1
'read data from datafile
Input #1, oTemp.forename, oTemp.surname,
oTemp.adr1, oTemp.adr2, oTemp.adr3, oTemp.postcode,
oTemp.dob
'assign data to docvariables attached to main
document
oDoc.Variables("forename").Value = oTemp.forename
oDoc.Variables("surname").Value = oTemp.surname
oDoc.Variables("adr1").Value = oTemp.adr1
oDoc.Variables("adr2").Value = oTemp.adr2
oDoc.Variables("adr3").Value = oTemp.adr3
oDoc.Variables("postcode").Value = oTemp.postcode
oDoc.Variables("postcode").Value = oTemp.dob
'insert sub template into current document at the
bookmark "insertpoint"
'update fields (to show results, not field codes)
oDoc.Fields.Update
'unlink fields (to remove field codes in final
document, and leave results as text)
oDoc.Fields.Unlink
Close #1
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory
ActiveWindow.ActivePane.SmallScroll Down:=16
End Sub
Thanks for any help!!
Iain
With a little help from some folks on another forum I have
a macro that I want to run automatically when I open a new
document based on a given template (FDA.dot).
The macro opens a text file generated by my database.
It then assigns each line of the text file to a document
variable. It then hopefully populates the document
variable fields in the document created from the template.
However, I cannot get it to work. My hunch is that this is
due to the Option Explicit command. This cannot be placed
inside the autonew subroutine, but doesn't appear to run
if left outside of it.
Can anyone help me re-write this macro so that it works
properly?
The code is as follows:-
Option Explicit
Private Type AddressData
forename As String
surname As String
adr1 As String
adr2 As String
adr3 As String
postcode As String
dob As String
End Type
Sub Autonew()
'
' Autonew Macro
' Macro created Tuesday, October 19, 1999 by snoop
'
Const PATH As String = "c:\"
Const DATAFILE As String = "appdata.txt"
Const INCLUDETEXT As String
= "INCLUDETEXT ""f:\\FDA.dot"""
Dim oTemp As AddressData 'temporary storage of data
from datafile
Dim oDoc As Document
Set oDoc = ThisDocument
Open PATH & DATAFILE For Input As #1
'read data from datafile
Input #1, oTemp.forename, oTemp.surname,
oTemp.adr1, oTemp.adr2, oTemp.adr3, oTemp.postcode,
oTemp.dob
'assign data to docvariables attached to main
document
oDoc.Variables("forename").Value = oTemp.forename
oDoc.Variables("surname").Value = oTemp.surname
oDoc.Variables("adr1").Value = oTemp.adr1
oDoc.Variables("adr2").Value = oTemp.adr2
oDoc.Variables("adr3").Value = oTemp.adr3
oDoc.Variables("postcode").Value = oTemp.postcode
oDoc.Variables("postcode").Value = oTemp.dob
'insert sub template into current document at the
bookmark "insertpoint"
'update fields (to show results, not field codes)
oDoc.Fields.Update
'unlink fields (to remove field codes in final
document, and leave results as text)
oDoc.Fields.Unlink
Close #1
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory
ActiveWindow.ActivePane.SmallScroll Down:=16
End Sub
Thanks for any help!!
Iain