Putting long strings into AutoCorrect and Run-time error ‘-2147467259 (80004005)

About four years ago I posted a query on EE to ask how, using Word with VBA, I could load strings longer than 255 characters into AutoCorrect. Essentially the solution was convoluted but it worked. For this example, the string to change to the long text is called strShortcut and the text is called strMsg.
    (a)          Dimension a variable to hold the long message, say “TextToAdd” as a Range
    (b)          Insert a new table with only one cell at the end of the document; say it is “Tbl3”
    (c)          Go through a loop to insert the shortcuts and the long texts into AutoCorrect
        i.          Copy the long string, strMsg, into the table: as in Tbl3.Columns(1).Cells(1).Range = strMsg
        ii.          Set TextToAdd = Tbl3.Columns(1).Cells(1).Range
        iii.          Chop off the end of TextToAdd: TextToAdd.End = TextToAdd.End - 1
        iv.          Add the message into AutoCorrect: AutoCorrect.Entries.AddRichText Name:=strShortcut, Range:=TextToAdd
    (d)          When the loop is done, delete the table inserted in step (b) above.
   
Except for one thing, the above works. The problem is that when the computer reaches step (iv) above, it displays a message: “Run-time error ‘-2147467259 (80004005)”. If I ignore that warning, the long message still gets loaded and when the routine is finished, typing the strShortcut string generates the desired message.
    Is that error message a time bomb that is going to give me problems later, or can I insert an “On Error Resume Next” to avoid stopping the code?
    --JRA in Priddis, Alberta
JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Please see attached code.
' ****************************************************************************************************************
Sub addShortcutsToWord()
'
'   Strings
Dim strMsg As String
Dim strCode As String
Dim ThisDoc As String
Dim TempDoc As String
Dim x As String

'   Ranges
Dim Tbl3 As Table
Dim TextToAdd As Range

      Application.ScreenUpdating = False      '   Freeze the screen to let us work faster
      Selection.NoProofing = False
      Application.CheckLanguage = True
      
      '     Make Tbl3 for holding msgs
      ThisDoc = ActiveDocument.Name
      Documents.Add DocumentType:=wdNewBlankDocument
      ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, _
            AutoFitBehavior:=wdAutoFitContent
      TempDoc = ActiveDocument.Name
      Set Tbl3 = ActiveDocument.Tables(1)
      Documents(ThisDoc).Activate
      '     ===================================================================================================================
      Selection.NoProofing = False
      Application.CheckLanguage = True
      strCode = ",a"
      x = "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a "
      GoSub AddItem
      strCode = ",b"
      x = "b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b "
      GoSub AddItem
      strCode = ",c"
      x = "c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c "
      GoSub AddItem
      strCode = ",d"
      x = "d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d "
      GoSub AddItem
      Documents(TempDoc).Close SaveChanges:=False
      AutoCorrect.ReplaceText = True
      Application.ScreenUpdating = True   '   Show screen again
      MsgBox "Done!"
      
      Exit Sub
AddItem:
            strMsg = x & x & x & x & x    '     Generate a long string to put into AutoCorrect
            Tbl3.Columns(1).Cells(1).Range = strMsg
            Set TextToAdd = Tbl3.Columns(1).Cells(1).Range
            TextToAdd.End = TextToAdd.End - 1  'Chop off the the cell marker at the end
            AutoCorrect.Entries.AddRichText Name:=strCode, Range:=TextToAdd
      Return
End Sub
' ****************************************************************************************************************
Sub DeleteAutoCorrect()
'     Function written January 28, 2010 by John Robin Allen, jrapriddis@gmail.com
'    Deletes all AutoCorrect entries that begin with a specified character (see below). Currently it is a comma.
Dim acEntry As AutoCorrectEntry
Dim Msg As String
Dim Msg2 As String
Dim i As Integer
Dim InitChar As String
      InitChar = ","
      For Each acEntry In AutoCorrect.Entries
            If Left(acEntry.Name, 1) = InitChar Then
                  i = i + 1
            End If
      Next acEntry
      If i = 0 Then
            Msg = "Ordinarily, this command deletes all AutoCorrect entries in Word that begin with a """ & InitChar & """ character. "
            Msg = Msg & "However, you have no such entries, so I am stopping this now." & MontSig
            MsgBox Msg, vbOKOnly
            End
      Else
            Msg = "This will delete all " & Str(i) & " AutoCorrect entries in Word that begin with a """ & InitChar & """ character. "
            Msg = Msg & "Do you want to do that?" & vbCrLf & vbCrLf & "You can always restore them back again "
            Msg = Msg & "in the same way you loaded them. " & vbCrLf & vbCrLf
            Msg = Msg & "     To delete the shortcuts, click on the OK button. " & vbCrLf
            Msg = Msg & "     Otherwise, press Enter or Escape."
            If MsgBox(Msg, vbOKCancel + vbDefaultButton2 + vbCritical, JRA & Msg2) = vbOK Then
                  For Each acEntry In AutoCorrect.Entries
                        If Left(acEntry.Name, 1) = InitChar Then
                              acEntry.Delete
                        End If
                  Next acEntry
                  If i > 0 Then
                        Msg = "We deleted " & i & " shortcuts. "
                        MsgBox Msg, vbOKOnly, JRA & Msg2
                  Else
                        Msg = "There were no shortcuts beginning with a comma." & MontSig
                        MsgBox Msg, vbOKOnly, JRA & Msg2
                  End If
            Else
                  Msg = "OK. Nothing changed. " & MontSig
                  MsgBox Msg, vbOKOnly, JRA & Msg2
            End If
      End If
End Sub

Open in new window

0
 
Helen FeddemaCommented:
I see AutoCorrectEntries (one word) in the Object Browser.  This may be a recent change.  Try removing the period and see if step iv works.
0
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Thanks Ms Feddema, for the suggestion. However, when I removed the period as you suggested, it produced an error message saying that VBA could not recognize "AutoCorrectEntries".
     A further development: When I tried running the routine again for 106 long entries, no error message appeared. Perhaps just rebooting the system removed the problem.
     Another puzzle arises that may require a new query with EE: Not all of the shortcuts produce the AutoCorrect messages. I suspect the problem may be with the language used (French, US English, Canadian English). I understand that AutoCorrectEntries are language-dependent. I'll work on that a bit and if I cannot solve it, I'll post another query with EE.
     Since one should not pose a new question in an existing query, if anyone has a possible solution to the new problem, let me know that here (without giving the suggestion) and I will post a new query so that others may see it in a different post.
    --John Robin (Allen)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Helen FeddemaCommented:
This is what I see in the Object Browser for Word 2010 (U.S. edition):

AutoCorrectEntries in Object Browser
What version of Word are you running?
0
 
Helen FeddemaCommented:
I also checked Work 2007 (running in a VM), and it also has AutoCorrectEntries in the Object Browser.
0
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I too am running Word 2010, and I have the same image as you show, with AutoCorrectEntries having an AddRichText as a member. It still does not work, and I do not know what I am doing wrong.
    j.r.a
0
 
Helen FeddemaCommented:
Perhaps the text stored in the table is not considered Rich Text.  Is the table field that stores the text a Memo field, set for Rich Text?  For more information on this, see my Access Archon article on this topic:

http://www.helenfeddema.com/Files/accarch157.zip

Rich Text form and table
0
 
Helen FeddemaCommented:
I was thinking that the table was an Access table, but on rereading your question I think it is a Word table.  You are setting a Range variable to the text, so that is not the problem.  Maybe the next line, where you trim off the last character?  Try commenting out that line, and see what happens.  Or try trimming off the last character before setting the Range variable.
0
 
Helen FeddemaCommented:
If you can post the actual VBA procedure and some test data, I will see what I can find out.
0
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I am somewhat new to working with posting code, so my previous message was a bit brief, except for the 100 lines of code. If you copy and paste the code into a module of a blank document, you can then run either two routines in the VBA code. The first creates four Autocorrect messages and loads them into Word. After they are loaded, you can type ",a" through ",d" that AutoCorrect will, in theory, convert to some longish strings of the letter plus a space. Each of those trigger words starts with a comma, followed by a letter of the alphabet.
     To get rid of those useless Autocorrect entries, you can delete them by hand or run the second routine, DeleteAutoCorrect that will delete any Autocorrect entry that begins with a comma.
     Comments and suggestions are most welcome.
     --John Robin Allen; Priddis, Alberta, Canada
0
 
Helen FeddemaCommented:
I made a new module and put the procedures into it.  The addShortcutsToWord procedure created the AutoText entries (I saw them in the AutoCorrect Options dialog).  The DeleteAutoCorrect procedure initially errored on the MontSig and JRA variables, which must have been declared elsewhere, so I declared them as text and gave them arbitrary values, and then this procedure also ran without errors.

So perhaps the problem is with the localization, or with some difference in setup between your computer and mine.  

Another possible issue:  The sample text is not actually Rich Text -- just plain text.  Do you have some Rich Text examples to test with?
0
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Thanks for looking at the code. I'll try to get rid of the MontSig and JRA variables. One tries to eliminate such goofs in code one shares, but this time I goofed with panache!
     I do not need RTF text except to be able to load strings longer than 255 characters. At some point in the future it would be wonderful if I could also make the routine work with formatted text, but for the moment I just want it to load long plain text strings into AutoCorrect.
     My basic query is whether others can make the routine work to load long strings into AutoCorrect.
     I would there therefore appreciate any comments or suggestions on the code I posted.

     --JRA
0
 
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Since the problem raised in my original query seems to have solved itself, I would like to close this question but still leave the code above for others to use in order to make AutoCorrect deal with strings longer than 255 characters.
    --John Robin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.