View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Workbook_Open - AutoCorrect Routine Doesn't Work

Your code worked fine for me (also xl2003).

And since you saw the msgbox output, the code must be running. And since you
can run the procedure successfully by using F5, then you have permission to
update the ACL file.

I tried opening the file with the macro when there were no other visible
workbooks open, by double clicking on the filename in windows explorer, by
saving it as an addin and doing both those worked fine. I couldn't break it.

So my guess (and it's just a guess!) is that excel needs some time to play
catchup when it's opening your file.

I added a short delay and it still worked ok--but that's not much of a test for
me, since it worked ok before.

This was my code:

Option Explicit
Sub Auto_open()
Application.OnTime Now + TimeSerial(0, 0, 1), _
"'" & ThisWorkbook.Name & "'!dothework"
End Sub
Sub auto_close()
On Error Resume Next 'in case either isn't there.
Application.AutoCorrect.DeleteReplacement what:="TLA"
Application.AutoCorrect.DeleteReplacement what:="TLAO"
On Error GoTo 0
End Sub
Sub DoTheWork()
Application.AutoCorrect.AddReplacement _
what:="TLA", _
Replacement:="Top Level Action Owner"
Application.AutoCorrect.AddReplacement _
what:="TLAO", _
Replacement:="Top Level Action Owner"
End Sub

I've seen other posts similar to yours where a slight delay allows excel to
catch up and do things right. (I've never had to use this in real life,
though.)

And if you really want those entries in your ACL, then I wouldn't even check.
Just overwrite them <bg.

NickH wrote:

I'm using Excel 2003 - SP3

I've written a couple of routines to Add and subsequently Delete a
couple of AutoCorrect entries. The intention was to call the Add
routine from Workbook_Open and the Del routine from
Workbook_BeforeClose.

Both routines work fine when fired from the VBE using F5. The Del
routine also works fine when called from the Workbook_BeforeClose
routine. However, the Add routine is impotent when called from either
the Workbook_Open or Auto_Open routines.

It runs without error, as evidenced by msg boxes but it has no effect
on the ACL. Here's the code for the Add routine - apologies for any
wrap-around...

Public Sub AutoCorrectAdd()
Dim repl() As Variant
Dim x As Long
Dim TLA_used As Boolean
Dim TLAO_used As Boolean

MsgBox "Start of AutoCorrectAdd. Error = " & Err.Number

TLA_used = False
TLAO_used = False

repl = Application.AutoCorrect.ReplacementList

For x = 1 To UBound(repl)
If repl(x, 1) = "TLA" Then TLA_used = True
If repl(x, 1) = "TLAO" Then TLAO_used = True
Next

If Not TLA_used Then _
Application.AutoCorrect.AddReplacement _
What:="TLA", _
Replacement:="Top Level Action Owner"
If Not TLAO_used Then _
Application.AutoCorrect.AddReplacement _
What:="TLAO", _
Replacement:="Top Level Action Owner"
' Yes I do want both TLA and TLAO to mean the same

MsgBox "End of AutoCorrectAdd. Error = " & Err.Number

End Sub

There are a small number of earlier posts regarding this issue and
responses all seem to imply the developer is doing something wrong but
don't manage to pinpoint what. I'm wondering if this could be a
version/update specific problem. Is it possible that Microsoft have
blocked ACL changes by opening routines, to prevent a specific hack?

I guess what I'm asking is - Am I fighting a lost battle or is there a
way round this?


--

Dave Peterson