ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Controlling Word template from Excel (https://www.excelbanter.com/excel-programming/281945-controlling-word-template-excel.html)

Lee

Controlling Word template from Excel
 
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
Whe=Selection.Range, _
RichText:=True
End With

What am I doing wrong?

GB[_3_]

Controlling Word template from Excel
 

"Lee" wrote in message
...
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

Whe=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 Whe=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








All times are GMT +1. The time now is 06:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com