Controlling Word template from Excel

L

Lee

I have an Excel add-in from which I can open a Word
template and insert info from the Excel workbook. I am
trying to insert AutoText that is saved in the template,
but I can't find the right syntax. This is what I have
written:

Dim WordApp As Word.Application
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
With WordApp
Documents.Add Template:= _
"C:\Documents and Settings\all
users\Templates\rxtemptest.dot" _
, NewTemplate:=False, DocumentType:=0
WordApp.Selection.Goto what:=wdGoToBookmark,
Name:="PlaceMark"
WordApp.Selection.Insert("temp1").InsertAutoText
Where:=Selection.Range, _
RichText:=True
End With

What am I doing wrong?
 
G

GB

Lee said:
I have an Excel add-in from which I can open a Word
template and insert info from the Excel workbook. I am
trying to insert AutoText that is saved in the template,
but I can't find the right syntax. This is what I have
written:

Dim WordApp As Word.Application
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
With WordApp
Documents.Add Template:= "C:\Documents and Settings\all
users\Templates\rxtemptest.dot" , NewTemplate:=False, DocumentType:=0
WordApp.Selection.Goto what:=wdGoToBookmark, Name:="PlaceMark"
WordApp.Selection.Insert("temp1").InsertAutoText
Where:=Selection.Range, RichText:=True
End With

What am I doing wrong?

I don't know, but I have two suggestions that may be helpful and one that is
probably wrong:

1. Your query is really to do with Word VBA code - why not try one of the
more relevant NGs? You will be more likely to get a helpful response.

2. We have an office app that does something similar. It actually runs a
mailmerge and then edits the resultant document within Word. We found the
best way was to set up a separate macro within word to do the editing. The
Excel code can trigger the word macro and then leave it to do its own work.
I seem to recall that it is far easier to debug the Word VBA code as a
separate macro within Word.

3. My Word Macro recorder recorded this, and you may like to use it in place
of the last line of your code:
NormalTemplate.AutoTextEntries("temp1").Insert Where:=Selection.Range

Incidentally, if you are using With WordApp ... End With, is it at all
helpful to start two of the lines WordApp.?

HTH a bit

Geoff
 

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