PDA

View Full Version : Maintaining AUtotext Entries from an external table.



kelzud
07-28-2009, 02:05 PM
Hi, I use office to manage a huge list of autotext entries, and I employ several templates to keep everything oranized. I move all of my commonly used autotexts from Normal.dot to Base.dot, and all the other autotexts I put in specialized templates (investigations, physexam, oncologymeds). Anyways, I have macros to turn add or remove these templates from the current document I am in. This is great.
The latest thing I have been trying to do is manage my autotext entries from an external file (set up for word, prefer excel). I have these two lists of VB code. Oh, I have no idea what any of this means, I just fiddle around and try to make it work!
I got these two pieces of code from gmayor website (regarding maintin autotext entries from an external table.

The first code looks at the external document (which I wish could be an excel file if anyone nows how to change it) and removes all entries from the normal.dot autotext list that are in that file. Its great!!! I can change where the file is and the name of it and everything. I can even change the template to delete in (in this case, normal.dot, which is listed in the code as NormalTemplate).

This is the delete code:

Sub AutoTextFromTableDelete()
Dim aTextDoc As Document
Dim cTable As Table
Dim rName As Range, rText As Range
Dim i As Long
Dim sFname As String

sFname = "D:\My Documents\Test\AutotextTable.doc"
Set aTextDoc = Documents.Open(sFname)
Set cTable = aTextDoc.Tables(1)

On Error Resume Next
For i = 1 To cTable.Rows.Count
Set rName = cTable.Cell(i, 1).Range
rName.End = rName.End - 1
NormalTemplate.AutoTextEntries(rName).Delete
Next i
aTextDoc.Close wdDoNotSaveChanges
End Sub

The next part of the code is set to populate the Normal.dot template (NormalTemplate) with the autotext entries in the external file. Here is where the problem comes in!!!! This works fine if I leave the template listed as NormalTemplate, but if I try to change it to the Investigations.dot by changing NormalTemplate to InvestigationsTemplate all hell breaks loose! I can still delete the entries in the Investigations.dot but when I click to upload the ones from the external file it will not do it. Here is the error followed by the code.

ERROR:
RUN-TIME ERROR '424:
OBJECT REQUIRED

When I click on Debugger it leads me to this portion of the code with a highlighted these two lines with an arrow pointing to the second line:

InvestigationsTemplate.AutoTextEntries.Add Name:=rName, _
Range:=rText

HERE IS THE CODE:

Sub AutoTextFromTableAdd()
Dim aTextDoc As Document
Dim cTable As Table
Dim rName As Range, rText As Range
Dim i As Long
Dim sFname As String

sFname = "D:\My Documents\Test\AutotextTable.doc"
Set aTextDoc = Documents.Open(sFname)
Set cTable = aTextDoc.Tables(1)

For i = 1 To cTable.Rows.Count
Set rName = cTable.Cell(i, 1).Range
rName.End = rName.End - 1
Set rText = cTable.Cell(i, 2).Range
rText.End = rText.End - 1

NormalTemplate.AutoTextEntries.Add name:=rName, _
Range:=rText
Next i
aTextDoc.Close wdDoNotSaveChanges
End Sub

Thanks for the time and the help guys; the only other info that I can give is that the template I am currently typing in is the Normal Template; all the other templates are just addons.
Have a great day!
Dave

fumei
07-31-2009, 12:09 PM
1. Your code does not have InvestigationsTemplate.AutoTextEntries.Add Name:=rName, _
Range:=rText

it has NormalTemplate

2. Name:=rName is an error. The Name parameter is a string, and rName is declared, and Set, as a Range.

3. I do not have time to actually check, but I am not sure you can even do this. The Add method works for either NormalTemplate or AttachedTemplate. It may be possible - again, I don't have time to check right now - to declare and Set a template object and do this...IF that template is a loaded global template.




Yes, you can.