Item #1 at:
http://www.standards.com/OhMyWord/VB...#DeleteToolbar
:)
Printable View
Item #1 at:
http://www.standards.com/OhMyWord/VB...#DeleteToolbar
:)
I tried with Word 2002 and could not duplicate the issue.
Hi Anne,
I couldn't duplicate the exact problem as described either, but I did get a duplicate once....See if this fixes it on yours (it appears it did on mine) :dunno
John :bink:
Edit: The problem appears to be that the single procedure was deleting the toolbar and then immediately adding the toolbar back in, and there was also a duplication of one piece of code. I deleted the duplication and split delete and add into two separate procedures (done on Office 2000 Premium edition)
Hi,
I couldn't replicate it either (Word 2000). I agree the delete and remake are too close together and this may cause the problem. Things don't always happen in "real life" as they do in stepping through a macro. I would allocate Delete Tooolbar to a DocumentOpen event in the template
MD
Yep, that's definitely it! I went back in and ran the delete/remake as a single procedure again, followed instructions, and ended up with THREE toolbars. It seems that there is simply not always enough TIME for the toolbar to be successfully deleted before the code creating a new one is activated.Quote:
Originally Posted by mdmackillop
Even slowing it down by simply changing "debug.print" to "MsgBox" slows everything down enough for it to run correctly (without duplicated toolbars) - a simple pause or some other operation should also do the trick :bink:
I can replicate this (2000 w/o SP1 or SP2 at the moment).
I am not comfortable with the explanation given though. It is perfectly reasonable (and not uncommon) to delete an object (for safety's sake) before (re-)creating it and I've never heard of this happening before.
I think this is the problem.
tempORary:=False
When the tool bar is created it sets the temporary flag to false. I don't know this but Word may place the information (the toolbar) in a seperate area for permanent tollbars vs temp toolbars.
Hi Tony,
I had a similar problem here.
Solved: Find text in Excel workbook
My code created added a worksheet and then tried to add code to that sheet. Although it worked fine in "step through", in practice, the sheet was not added until the macro had completed, and the AddCode was "missed"
MD
Changing the timing, by splitting the code, does not always work.
Tried that many times, not always successful.
In the real applications in which I have encountered the problem, the code was not always so close together, so there's no simple timing solution.
I constructed the example to exacerbate the timing aspect.
The problem was not discovered by moi alone, a number of others also reported/confirmed the problem.
It's clearly a timing issue, Word has this worse than the other Office apps, so a "general" solution ain't so easy. Users would not be happy of macros included long wait loops.
I wonder if the problem is going to get worse when Office is .NET-ized as garbage collection is very different in .NET. Wow, think of all the new bugs we will have to investigate! Something to look forward too!
In the general case that won't work as one does not always easily know WHEN the toolbar will need to be deleted.Quote:
Originally Posted by mdmackillop
The posted example was constructed to exacerbate the problem.
May not be relevant but, in playing with this, I ended up at one point with three copies of the toolbar - all invisible. I deleted them all through Tools > Customize and closed Word and restarted and they were all still there.
Sorry, should have said DocumentClose event.
MD
Ayup, that is also how I encountered the problem several years ago.Quote:
Originally Posted by TonyJollans
My recollection is that I had tested that hypothesis several years ago.Quote:
Originally Posted by Tommy
Temporary = False is the default so explicitly specifying it shouldn't make any difference. If memory serves, there used to be a problem with Temporary=True not working properly, but I think that's now fixed.
I tested and got 5+ commandbars. I saved changes. I opened Normal.dot and deleted the commandbars. I then changed the Temporary = False to Temporary = True. I now do not get duplicates of the commandbars I just get 1. What am I missing?
Hey, Tommy. Just to be clear, in which version are you testing?
I sometimes specify certain defaults as that makes the code clearer.Quote:
Originally Posted by TonyJollans
In addition, specifying the defaults eliminates bugs due to MSFT changing defaults from version to version, rarely done, but it'd cheap protection.
Howard,
MS Bugs - surely not!!
Tommy,
Temporary=True, if it works, means that the toolbars never get saved, no matter how many there are
Had the same thing...I still think the problem is timing, but exacerbated by the fact that there is actually no macro/module in the workbook being used. The macro is in the template and is being referenced by the workbook and what happens in one is not neccessarily being duplicated in the other at the same time (this is instanced by the fact that you can rename, change, and save, the workbook but on exiting the book you will then be asked if you want to save the template) :bink:Quote:
Originally Posted by TonyJollans
The behavior you see is going to vary from version to version.Quote:
Originally Posted by Tommy
And, as I point out in the code comments, the problem does not occur every time the code is run.
Note that I've NEVER seen the problem with Word 97, so sumptin changed in Word 2000 and beyond.
Of course, until about a year ago, I was running Office 97 in Win 98, the other versions of Office in Win 2000, so the OS could also be a factor. Maybe the problem will now be seen in Word 97 under Win 2000? Next time I boot to the system with Word 97, I'll try to remember to test, but that may not occur for a looong time.
In the original app in which I found the problem I was indeed using Temporary=False The example uses Temporary=False to emphasize the problem. After all, a tempory toolbar should vanish on its own anyway.
The one at the site that was posted above. I did not download the one John posted.
I am using Word 2000 9.0.6926 with SP 3. Win98, IE 6 SP1 with all updates installed.
Thing is I did change the temp back to false and was able to reduplicate it. On the commandbar I noticed a funny thing when I looked at the code I saw this tempORary:=False I changed it to true and it went to this Temporary:=True :confused: go figure.Code:Public Sub DeleteToolbars()
Dim intBeforeIndex As Integer
Dim cmdbar As CommandBar
Dim cmdbutton As CommandBarButton
Const strToolbar As String = "Double Vision HK VBA"
' CustomizationContext = MacroContainer
CustomizationContext = NormalTemplate
On Error Resume Next
CommandBars(strToolbar).Delete
If Err.Number = 0 Then
Debug.Print "Toolbar deleted"
Else
Debug.Print "Toolbar NOT deleted"
End If
On Error GoTo 0
On Error Resume Next
CommandBars(strToolbar).Delete
On Error GoTo 0
Set cmdbar = CommandBars.Add(Name:=strToolbar, Position:=msoBarTop, Temporary:=True, MenuBar:=False) '<-- the line I changed
cmdbar.Visible = True
Set cmdbar = CommandBars(strToolbar)
intBeforeIndex = 1
Set cmdbutton = cmdbar.Controls.Add(Type:=msoControlButton, Temporary:=False, Before:=intBeforeIndex)
With cmdbutton
.Style = msoButtonCaption
' Use whatever you need in next line to avoid compile errors.
.OnAction = "modReplaceStringNames.ReplaceStringNames"
.Caption = "String Names"
End With
End Sub
We need to stick to Word in this thread, Excel is usually not as misbehaving as Word.Quote:
Originally Posted by johnske
Sorry - I meant Doc of course
Another funny, while testing I had a commandbar set to false and a command button set to true. I changed the commandbar to true, reran the code and when I reopened Word it had 2 buttons on the commandbar.
To simplify discussionm do not use Temporary=True.
Temporary=False must be used as the intent of the original app was to replace an extant toolbar.
The easiest way was to delete and recreate the toolbar.
That's when I encountered the problem.
I've also seen that behavior, but I didn't want to complicate the example.Quote:
Originally Posted by Tommy
Hey, it's almost 16:00 and I've had no food or drink since about 03:00.
Is VBAExpress hazardous to my health?
If so, Howard, you need only refresh yourself in the arcade.
Re: My last post (quoted above) anyone else wanna try this?Quote:
Originally Posted by johnske
1) export the module from the template
2) move the template to the recycle bin
3) now import the module into the document
4) goto tools/references and untick any reference to the template
5) disable CustomizationContext = NormalTemplate by putting an apostrophe in front of it
6) save...
Now run the macro (delete & instant remake) and see if you get any duplications
I did and couldn't get any....:dunno :bink:
This is very interesting.
My suspicion is that it is somehow related to the customization context - customizing one template from another seems to be flawed,
1. Make sure Word is closed
2. Double click on the DoubleVision template to create a new document based on it
3. Run the macro - from Word, not the VBE
4. The toolbar is NOT added to Normal
I have done this several times and never get the toolbar
5. Now, click on the New icon open a new document based on Normal
6. Go back to the document based on doubleVison
7. Run the macro again, from Word
8. This time I get a toolbar
I have repeated this several times, again with consistent results.
(edited to add this comment - and the related amendments above)
A bit more testing shows this behaviour only to happen when the macro is run via Alt+F8 from Word - it does not happen when run from within the VBE
Is not your code using Temporary=True?Quote:
Originally Posted by johnske
If so, that ignores the problem, and is not a solution.
CustimizationContext is precisely intended for this purpose.Quote:
Originally Posted by TonyJollans
However, would not astonish me if there were a "bug"/"feature" depending on HOW the code was run.
I'll try your experiment a bit later.
Sorry, nope...I'm using Temporary=False :bink:Quote:
Originally Posted by Howard Kaikow
I have not played a computer game in over a year.Quote:
Originally Posted by Dreamboat
I've overcome the addiction!
This just gets more and more interesting.
The peanut gallery is *in attendance*!
:bore
Best to ignore my last post until I am sure I report what I actually did. It's late here is the only excuse I have. :dunno
It is still interesting.
Quote:
Originally Posted by TonyJollans
I;ve got about a 2 hour window in which to play.
I'm gonna start over and document EVERY step.
Let's see what happens.
LOL, Howard. I hope Tony is in bed by now. He's in the UK. :)
johnske is down under.
Hi Howard,
Inconsistent little beggar aint it?...Went back to your original code and converted it from "Template" to "Normal" and it came back!!!
Have a look at this attachment, it's in "Normal" with some of your error-handling procedure disabled being the only difference and - for whatever reason - it gives the right results every time.
But remember that the toolbar(s) are added to ALL your word docs when you add it to one doc, so goto view/toolbars and delete all occurrences of the toolbar before running this...You then only get one toolbar every time :bink: