MS Office Forum / Word / Programming / October 2005
Method "Insert" of AutoTextEntry Failed.
|
|
Thread rating:  |
Greg - 11 Oct 2005 21:11 GMT Hi,
I am getting an Error in some code that I can't understand. The error is as shown in the subject and when I press "Help" in the message box a screen reports information ofn Automation Error (440) which I don't understand.
Here is the scenario. I was goofying around attempting to make mutually exclusive checkboxes in a simple unprotected form. I was bored so please bear with me ;-).
I inserted five AutoText entries similiar to the ones available in the Fax Templates (Checked and Unchecked box symbols nested in a Macrobutton field)in columns 1 through five across the top of table. My thinking being that I would use the checkit (I called it enable) macro to determine column number of the box I checked, toggle the box to checked, and replace the AutoText entry in the other four columns to the unchecked box symbol.
In practice, it works pretty good and a working macro is provided below. The part I can't understand is why I can't use a predefined range to insert the AutoText in the four other columns. For some reason I first have to delete the existing AutoText Entry (using a Range.Delete), then select insertion point (using a Range.Select) and finally insert the AutoText entry using Where:=Selection.Range
Here is a short snipet of what I tried to do when the box in column 1 was checked: Dim oCol1 As Range 'Repeat for oCol2-5 Set oCol1 = ActiveDocument.Tables(1).Cell(1,1).range 'Repeat for oCol2-5 Select Case j 'Column index Case Is = 1 'Column 1 checkbox checked oTemp.AutoTextEntries("Disabled").Insert Where:=oCol2 oTemp.AutoTextEntries("Disabled").Insert Where:=oCol3 'repeat for columns 4 and 5 'ERROR is generating on the first oTemp line. Why?
and here is what I had to do: Select Case j Case Is = 1 oCol2.Delete oCol2.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol3.Delete oCol3.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range 'repeat for columns 4 and 5
Option Explicit Private j As Long Dim oTemp As Template Sub Enable() Dim oCol1 As Range Dim oCol2 As Range Dim oCol3 As Range Dim oCol4 As Range Dim oCol5 As Range Dim oTable As Table Set oTable = ActiveDocument.Tables(1) Set oTemp = ActiveDocument.AttachedTemplate Set oCol1 = oTable.Cell(1, 1).Range Set oCol2 = oTable.Cell(1, 2).Range Set oCol3 = oTable.Cell(1, 3).Range Set oCol4 = oTable.Cell(1, 4).Range Set oCol5 = oTable.Cell(1, 5).Range j = Selection.Information(wdStartOfRangeColumnNumber) oTemp.AutoTextEntries("Enabled").Insert Where:=Selection.Range Select Case j Case Is = 1 oCol2.Delete oCol2.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol3.Delete oCol3.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol4.Delete oCol4.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol5.Delete oCol5.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range Case Is = 2 oCol1.Delete oCol1.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol3.Delete oCol3.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol4.Delete oCol4.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol5.Delete oCol5.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range Case Is = 3 oCol1.Delete oCol1.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol2.Delete oCol2.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol4.Delete oCol4.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol5.Delete oCol5.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range Case Is = 4 oCol1.Delete oCol1.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol2.Delete oCol2.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol3.Delete oCol3.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol5.Delete oCol5.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range Case Is = 5 oCol1.Delete oCol1.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol2.Delete oCol2.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol3.Delete oCol3.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range oCol4.Delete oCol4.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range Case Else 'Do Nothing End Select End Sub Sub Disable() Set oTemp = ActiveDocument.AttachedTemplate j = Selection.Information(wdStartOfRangeColumnNumber) oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range End Sub
Thanks
Anne Troy - 12 Oct 2005 03:55 GMT Hi, Greg!! Doesn't that mean the object doesn't exist? For instance, I remember getting that error when trying to insert at a bookmark name that didn't exist. And I think I get it too when trying to insert an autotext entry that doesn't exist IN THIS TEMPLATE. Sorry if it's not helpful! ************ Anne Troy www.OfficeArticles.com
> Hi, > [quoted text clipped - 143 lines] > > Thanks Greg Maxey - 12 Oct 2005 04:16 GMT Anne,
I don't know what it means. I know that there is a cell(1,1), cell(1,2), etc.
I am setting a oCol1 to the cell(1,1) I can delete the oCol1.Range I can select the oCol1.Range
I just can't use oCol1 to tell "where" to insert the AutoText entry: oTemp.AutoTextEntries("Disabled").Insert Where:=oCol1
I have to do it this way: oCol1.Delete oCol1.Select oTemp.AutoTextEntries("Disabled").Insert Where:=Selection.Range
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Hi, Greg!! Doesn't that mean the object doesn't exist? For instance, I > remember getting that error when trying to insert at a bookmark name that [quoted text clipped - 151 lines] >> >> Thanks Anne Troy - 12 Oct 2005 07:40 GMT Have you tried using bookmarks? http://vbaexpress.com/kb/getarticle.php?kb_id=126 Also...if you give a search, there's a cool utility there, too, that helps manage bookmarks. I don't code, but I use it a lot. :) ************ Anne Troy www.OfficeArticles.com
> Anne, > [quoted text clipped - 167 lines] >>> >>> Thanks Greg - 12 Oct 2005 16:29 GMT Anne,
Using a bookmark doesn't really answer the question "Why can't I use a defined range in the "where:= " section of the oTemp.AutoTextEntries("Disabled").Insert Where:=____ statement.
I have learned that it has something to due with the defined range be a cell range. Consider this experiment and code:
Open a new blank document. Type a few lines of gibberish, enter a table, enter a few more lines of gibberish and a bookmark named "Test"
Run this code:
Sub InsertAutoText() Dim myRng1 As Range Dim myRng2 As Range Dim myRng3 As Range Dim myRng4 As Range Set myRng1 = Selection.Range Set myRng2 = ActiveDocument.Bookmarks("Test").Range Set myRng3 = ActiveDocument.Range myRng3.Start = 100 'or any arbitrary point Set myRng4 = ActiveDocument.Tables(1).Cell(1, 1).Range ActiveDocument.AttachedTemplate.AutoTextEntries("ATTN:").Insert Where:=myRng1 ActiveDocument.AttachedTemplate.AutoTextEntries("ATTN:").Insert Where:=myRng2 ActiveDocument.AttachedTemplate.AutoTextEntries("ATTN:").Insert Where:=myRng3 On Error GoTo Handler 'Next line fails. I don't know why but I suspect it is due to the defined range 'encompassing a cell range. ActiveDocument.AttachedTemplate.AutoTextEntries("ATTN:").Insert Where:=myRng4 'With this change it works!! myRng4.Delete 'clears the cell contents myRng4.Collapse ActiveDocument.AttachedTemplate.AutoTextEntries("ATTN:").Insert Where:=myRng4 Exit Sub Handler: MsgBox Err.Number & Err.Description Resume Next End Sub
The question now is why must the defined range first be collapsed before is will work in the .Insert statement?
Helmut Weber - 12 Oct 2005 17:00 GMT Hi Submariner,
could it be, that it is the chr(102) & some letters & chr(103) end-of-cell mark again?
Dim rTmp As Range Set rTmp = ActiveDocument.Tables(1).Cell(1, 1).Range rTmp.End = rTmp.End - 1 NormalTemplate.AutoTextEntries("q").Insert Where:=rTmp
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Greg - 12 Oct 2005 17:21 GMT How shocking!! My tender eyes and ears are burning ;-)
It must be!!
Greg - 12 Oct 2005 17:13 GMT Anne,
BTW, a few months ago I modified a simliar writebook mark routine to meet my need. There could be extraneous bells and whistles for some users, but you might like to give it a try:
Sub myBookMarker() Dim oBkMark As String Dim oTxtInput As String oBkMark = InputBox("Enter the bookmark name: ", "Bookmark") If Not ActiveDocument.Bookmarks.Exists(oBkMark) Then If Len(Selection.Range) = 0 Then If MsgBox("Do you want to enter text at the insertion point?", _ vbYesNo, "Text") = vbYes Then oTxtInput = InputBox("Enter the text to insert: ", "Text") Else oTxtInput = "" End If Else oTxtInput = Selection.Range.Text End If Else If MsgBox("Bookmark " & oBkMark & " already exists." _ & " Do you want to redefine the contents?", vbYesNo, "Warning") = vbYes Then oTxtInput = InputBox("Enter the new bookmark tex: ", "Text") Else Exit Sub End If End If WriteToBookmark oBkMark, oTxtInput End Sub Public Sub WriteToBookmark(ByVal bkMarkName As String, ByVal textIn As String) Dim oRng As Range If ActiveDocument.Bookmarks.Exists(bkMarkName) Then Set oRng = ActiveDocument.Bookmarks(bkMarkName).Range oRng.Text = textIn ActiveDocument.Bookmarks.Add bkMarkName, oRng Else ActiveDocument.Bookmarks.Add bkMarkName, Selection.Range Set oRng = ActiveDocument.Bookmarks(bkMarkName).Range oRng.Text = textIn ActiveDocument.Bookmarks.Add bkMarkName, oRng End If End Sub
Greg - 12 Oct 2005 17:48 GMT Once again with the gracious assistance of Helmut Weber I passed a stumbling block and have managed to clean this code up a bit. Thanks Helmut.
Option Explicit Private j As Long Sub Enable() Dim oTable As Table Dim oAutoTxt As AutoTextEntry Dim oCol1 As Range Dim oCol2 As Range Dim oCol3 As Range Dim oCol4 As Range Dim oCol5 As Range
Set oTable = ActiveDocument.Tables(1) Set oAutoTxt = ActiveDocument.AttachedTemplate.AutoTextEntries("Disabled") 'Define ranges and strip off end of cell markers. Set oCol1 = oTable.Cell(1, 1).Range oCol1.End = oCol1.End - 1 Set oCol2 = oTable.Cell(1, 2).Range oCol2.End = oCol2.End - 1 Set oCol3 = oTable.Cell(1, 3).Range oCol3.End = oCol3.End - 1 Set oCol4 = oTable.Cell(1, 4).Range oCol4.End = oCol4.End - 1 Set oCol5 = oTable.Cell(1, 5).Range oCol5.End = oCol5.End - 1 'Determine column of oject clicked j = Selection.Information(wdStartOfRangeColumnNumber) 'Toggle on checkboxes (or other interactive object) as selection. ActiveDocument.AttachedTemplate.AutoTextEntries("Enabled").Insert _ Where:=Selection.Range 'Toggle off checkboxes at all other locations. Select Case j Case Is = 1 oAutoTxt.Insert Where:=oCol2 oAutoTxt.Insert Where:=oCol3 oAutoTxt.Insert Where:=oCol4 oAutoTxt.Insert Where:=oCol5 Case Is = 2 oAutoTxt.Insert Where:=oCol1 oAutoTxt.Insert Where:=oCol3 oAutoTxt.Insert Where:=oCol4 oAutoTxt.Insert Where:=oCol5 Case Is = 3 oAutoTxt.Insert Where:=oCol1 oAutoTxt.Insert Where:=oCol2 oAutoTxt.Insert Where:=oCol4 oAutoTxt.Insert Where:=oCol5 Case Is = 4 oAutoTxt.Insert Where:=oCol1 oAutoTxt.Insert Where:=oCol2 oAutoTxt.Insert Where:=oCol3 oAutoTxt.Insert Where:=oCol5 Case Is = 5 oAutoTxt.Insert Where:=oCol1 oAutoTxt.Insert Where:=oCol2 oAutoTxt.Insert Where:=oCol3 oAutoTxt.Insert Where:=oCol4 Case Else 'Do Nothing End Select End Sub Sub Disable() j = Selection.Information(wdStartOfRangeColumnNumber) ActiveDocument.AttachedTemplate.AutoTextEntries("Disabled").Insert _ Where:=Selection.Range End Sub
Anne Troy - 12 Oct 2005 21:37 GMT Cool beans! :) ************ Anne Troy www.OfficeArticles.com
> Once again with the gracious assistance of Helmut Weber I passed a > stumbling block and have managed to clean this code up a bit. Thanks [quoted text clipped - 66 lines] > Where:=Selection.Range > End Sub
|
|
|