Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word XP--Corrupting Names of CommandButtons / Won't exit design mode

Status
Not open for further replies.

Tranman

Programmer
Sep 25, 2001
695
US
Hi,
I have a Word document that is created by a VB program through automation. This document contains anywhere from 10 to 200 or so command buttons that I use to delete rows from an Oracle database and update rows in a SQL database (the same button performs both functions).

Randomly, for no reason that I can discern, Word corrupts the names of the CommandButtons during a file save, by appending a 1 onto the right side of the name--for example, CommandButton23 becomes CommandButton231. When this happens, the next time I try to open the file, I get an error message, "Can't exit design mode because control CommandButton23 can not be created".

This is a great app, but this problem is driving me nuts. Has anyone seen anything like this? Any help greatly appreciated.

Tranman
 
I think that the problem arises rather when creating document than when saving it. What is the code to add buttons?
The copy of CommandButton23 becomes CommandButton231. So, to control names, for instance:
[tt]Dim ctrl As InlineShape
Set ctrl = wdDoc.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
ctrl.OLEFormat.Object.Name = "Command12345"[/tt]

combo
 
combo,
Thanks for the response.

No, this corruption is definitely happening on save. When I create the document, it functions perfectly. The data entry clerk can use it all day with no problems. When it is saved and closed is when the names get clobbered or don't get clobbered. If they do get clobbered, then, every time you save it, it appends another 1 to the end of the already corrupted CommandButtons' names.

Here's the code:
'Add the delete button (must be done BEFORE the hyperlink)
strWord = Trim(appW.ActiveDocument.Sentences.Last.Words(7).Text)
Set rngBtn = appW.ActiveDocument.Sentences.Last.Words(7)
dblWordStart = rngBtn.Start + 10
rngBtn.Start = dblWordStart
rngBtn.End = rngBtn.Start + 1
Set shp1 = doc.Content.InlineShapes.AddOLEControl(ClassType:="forms.commandbutton.1", Range:=rngBtn)
shp1.OLEFormat.Object.Caption = "Delete"
shp1.Height = 7
shp1.Width = 47
shp1.AlternativeText = strWord

And here's the code where I add the event procedure for the control. You can see that I use the name of the CommandButton that I just created to create the event procedure, so there is *no* possibility that it's wrong (plus I went into the document and verified control names and verified that the name was definitely being corrupted on save(again and again after the first time).

'Add a procedure for the click event of the inlineshape for D1 record
'**Note: The click event resides in the ThisDocument module
strWord = Left(strWord, 7)
sCode1 = "Private Sub " & shp1.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" CONST strAccNo = """ & strWord & """" & vbCrLf & _
" CONST strYear = """ & strDBYear & """" & vbCrLf & _
" Call DeleteAcc(strYear, strAccNo)" & vbCrLf & _
"End Sub"
doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode1

Anybody else?

Tranman
 
Sorry for still being there, Tranman.
Your first post suggest that either the template or file, while opening, tries to create buttons. I would examine this and all event procedures (doc+templates), disable macros while opening, and without template auto procedures.

combo
 
Hi combo,
There is actually no template (other than Normal.doc) involved in this.

The only code is the event procedures associated with the buttons and a separate module that contains a sub to add a reference to the ActiveX Data Objects 2.7 Library, which is run by the VB program during document creation. None of the CommandButtons are created by the Word document itself(unless you consider the ole automation object in the VB program to be the document). No macros run at startup.

My understanding of how this works is that all of the objects in the document are instantiated (created) every time the document is opened (they only exist as references to the object type in the type library and arguments used for setting the properties of the instance of the object, when the document is closed). I think that, (the instantiation at startup) is what Word is talking about in the error message when it uses the (misspelled) phrase, "can not be created".

Anyway, I really do appreciate your help. Looks like it may be time for Plan B, or maybe C or D.

Tranman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top