Problem with automating a macro...

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
 
P

Peter Hewett

Hi Iain

You don't actually state what the problem is. What error message are you
getting? Or is it not doing what you are expecting??

I've taken a cursory look at the code, but because of the way text is
wrapped I can't see anything that prevents it from working.

The Option Explicit statement is normally the first statement (non comment
line) in a module. What it's actually doing is forcing you to declare all
variables before you use them. You can't place this statement inside a
macro (procedure).

There is a problem in that the "Postcode" DocumentVariable is assigned two
different values, obviously only the last value will be used! But this wont
stop the code from running.

Post again stating what error you're receiving.

HTH + Cheers - Peter
 
I

Iain Cowden

I feel so dim <blush>.

Basically, when the macro text below is pasted into the
FDA.dot template, and that is saved in my templates
directory. If a new document is generated from the
template (i.e. file/new fda.dot) the macro fails to
populate the document variables. The select-all part at
the end of the macro works, but nothing else.

The only time the macro works is if the fda.dot template
itself is opened and the macro run manually.

Iain
-----Original Message-----
Hi Iain

You don't actually state what the problem is. What error message are you
getting? Or is it not doing what you are expecting??

I've taken a cursory look at the code, but because of the way text is
wrapped I can't see anything that prevents it from working.

The Option Explicit statement is normally the first statement (non comment
line) in a module. What it's actually doing is forcing you to declare all
variables before you use them. You can't place this statement inside a
macro (procedure).

There is a problem in that the "Postcode"
DocumentVariable is assigned two
 
P

Peter Hewett

Hi Iain

No problem! OK, chage the line:

Set oDoc = ThisDocument
to:
Set oDoc = ActiveDocument

What's happening is ThisDocument ALWAYS refers to the template/document
where the code is executing. In this case since the codes in your template,
DocumentVariables in your template are being updated! But not in your
document.

HTH + Cheers - Peter
 
G

Guest

Superb!!

Thank you very very much. Now, I'm rocking...

I wish all answers were that simple!

Iain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top