Word 2003 code not working in Word 2007

S

slickdock

I have an MSAccess application that exports some data to word, then formats
the word screen. This code executes from MSAccess after Word has opened a
document and Word is in the footer of that document. This code has always
worked since Word 97, but now Word 2007 dies at this point:

wd.NormalTemplate.AutoTextEntries("Filename and path").Insert _
wd.Selection.Range

I'd like to find code that would work for either version if possible, rather
than having to have 2 different bits of code depending on the version of Word
that is in use on the machine.

Thank you.
 
G

Graham Mayor

This is attributable to the different ways Word handles autotext in the two
versions.

You should be able to get around it with something like

If Application.version < 12 Then
wd.NormalTemplate.AutoTextEntries("Filename and path").Insert _
wd.Selection.Range
Else
wd.NormalTemplate.BuildingBlockEntries("Filename and path").Insert _
wdSelection.Range
End If

Ensure that the building block entry actually exists in Word 2007's normal
template.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

slickdock

Thank you Graham. You have been a big help, but I can't get it to insert the
filename and path. I tried 2 techniques:

1. First I tried what you said. I added it to the building block entries,
then when your exact code didn't insert the field, I manually made a word
macro that inserted the building block entry, which looked like this:

wd.ActiveDocument.AttachedTemplate.BuildingBlockEntries("Filename and
path").Insert _
Where:=wd.Selection.Range, RichText:=True

Then I inserted that code in my Access module. But it still just sat there
and didn't insert the filename and path.

2. I manually made a word macro that just inserted filename and path,
instead of using building blocks. It looked like this, so I put that in my
code instead:

wd.Selection.Fields.Add Range:=wd.Selection.Range, Type:=wd.FieldEmpty,
Text:= _
"FILENAME \* Lower \p ", PreserveFormatting:=True

Again, it just sits there and doesn't insert the filename and path.

Any thoughts?
 
G

Graham Mayor

First of all may I say I don't know much about programming Word from Access
vba and the syntax is slightly different, however the following would work
in Word vba

Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldFileName, _
Text:="\* Lower \p ", _
PreserveFormatting:=False

My guess is that the building block did not exist in the active template so

ActiveDocument.AttachedTemplate.BuildingBlockEntries("Filename and
path").Insert _
Where:=Selection.Range, _
RichText:=True

Incidentally if the active template is a Word 2003 dot file and contains the
autotext entry then

ActiveDocument.AttachedTemplate.AutoTextEntries("Filename and path").Insert
_
Where:=Selection.Range, _
RichText:=True

should work for either Word version.

If the autotext is stored in the normal template (I don't think this entry
exists by default in Word 2007) then

NormalTemplate.AutoTextEntries("Filename and path").Insert _
Where:=Selection.Range, _
RichText:=True

should also work for either version. Because of the extra functionality
through building blocks, Word 2007 is a little more precise about addressing
autotext entries.

Hopefully this will help you on your way from Access.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

slickdock

I like your first solution, because I don't have to be sure anything has been
set up in the user's normal.dot, or BuildingBlockEntries or anything at all.
So I tried it, inserting this into my code:

wd.Selection.Fields.Add _
Range:=wd.Selection.Range, _
Type:=wd.FieldFileName, _
Text:="\* Lower \p ", _
PreserveFormatting:=False

Notice I just had to add the wd. to your code in a few places. It compiled,
then I tried it, and got this error message when it gets to the point of
inserting the filename:

"Object doesn't support this property or method."
 
G

Graham Mayor

I regret my lack of knowledge of Access vba gets in the way here - my gut
feeling suggests that Type:=wd.FieldFileName is wrong, but you might get
more pertinent advice in an Access vba forum and to that end I have cross
posted this reply to that group.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins

I am not sure of all of your code, but use something like

Dim doc as document
set doc = wd.ActiveDocument
doc.Fields.Add Selection.Range, wdFieldFileName

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Douglas J. Steele

When using Automation, you're using the code from the target, not the
source. In other words, you'd use pretty much standard Word VBA, not Access
VBA. The difference, of course, is that you have to be explicit about
including the Word application variables in the statement.

Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldFileName, _
Text:="\* Lower \p ", _
PreserveFormatting:=False

won't work, because Access has no idea what Selection is. Since Selection is
a property of the Application, assuming wd is instantiated as
Word.Application, all references to Selection must be prefixed with wd.
wd.FieldFileName definitely does look incorrect. Assuming you're using Early
Binding (i.e.: a reference has been added to Word under Tools | Reference),
wdFieldFileName should be sufficient. If you're using Late Binding (i.e.: no
reference has been set), you'll have to define the value of wdFieldFileName
to Access (it's equal to 29)

wd.Selection.Fields.Add _
Range:=wd.Selection.Range, _
Type:=wdFieldFileName, _
Text:="\* Lower \p ", _
PreserveFormatting:=False

or

wd.Selection.Fields.Add _
Range:=wd.Selection.Range, _
Type:=29, _
Text:="\* Lower \p ", _
PreserveFormatting:=False
 
S

slickdock

Thank you Doug. The Type 29 did the trick. (I had already added the wd.
prefixes without success.)
And thank you Graham for cross posting. Wish I knew how to do that.
 
S

slickdock

Thank you Douglas. The Type 29 did the trick.
And thank you Graham for cross posting. I wish I knew how to do that.
 
S

slickdock

Thank you Graham for cross posting. I would have done that if I had known how.
You were right that the Type := was wrong. See below from Douglas for
solution.
 
D

Douglas J. Steele

You appear to be using Microsoft's abyssmal web interface to these
newsgroups.

To cross-post, you should see an "Advanced Options" link at the bottom of
the page. You can type the names of the various groups into the Newsgroup
box, separating each newsgroup name with a semicolon.
 

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