Consulting

Page 3 of 6 FirstFirst 1 2 3 4 5 ... LastLast
Results 41 to 60 of 117

Thread: Challenge!! Can you solve this problem?

  1. #41
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Dreamboat
    LOL, Howard. I hope Tony is in bed by now. He's in the UK.
    johnske is down under.
    Well, here's something they can read when they awaken.

    I added an additional .zip at the same URL.

    The following are my notes from the past 2 hours or so.
    I renamed things in the new .zip.

    1. Cleared read-only attribute for Normal.dot.
    2. Double-clicked on HowardKaikowDoubleVision.dot.
    3. Ran DeleteToolbars macro from Tools | Macros menu in Word GUI, not from VBIDE.
    4. Exited from Word.
    5. Started Word using Word icon on task bar.
    6. "String Names" toolbar was present.
    7. Exited Word.
    8. Double-clicked on HowardKaikowDoubleVision.dot.
    9. Ran DeleteToolbars macro from Tools | Macros menu in Word GUI, not from VBIDE.
    10. Exited from Word.
    11. Started Word using Word icon on task bar.
    12. An additional "String Names" toolbar was present.
    At this point, I noticed something I had forgotten about.
    Likely rather significant
    13. Exit from Word
    14. As Word was exiting, one of the toolbars (the leftmost) disappeared and the other remained.
    15. When I next opened Word using the icon on the task bar, the second "String Names" toolbar re-appeared AND,
    likely significant, both toolpars moved one position to the right.
    16. Each time steps 13-15 were repeated, both toolbars appeared AND were shifted one position to the right EACH time!
    17. Looking at the toolbars listed for the Normal template in Tools | Macros Organizer, TWO tooolbars are listed with the SAME name. Obviously the names cannot be the same, so something is amiss.

    18. I then added code to the DeleteToolbar macro to list the toolbars.
    19. I changed the file name to HowardKaikowDoubleVision-Revised.dot.
    20. Went back to a copy of the Normal template from before step 1 supra.
    21. Repeated steps 1-16 supra. Same behavior.

    Next
    21. Went back to a copy of the Normal template from before step 1 supra.
    22. Set read-only attribute for Normal.dot.
    23. Added code to work on MacroContainer, instead of Normal template and then
    double-clicked on HowardKaikowDoubleVision-Revised.dot.
    24. Ran WorkWithMacroContainer macro from Tools | Macros menu in Word GUI, not from VBIDE.
    25. Exited from Word.
    26. Double-clicked on HowardKaikowDoubleVision-Revised.dot.
    27. "String Names" toolbar was present BUT caption was empty!
    28. Exited Word.
    29. Double-clicked on HowardKaikowDoubleVision-Revised.dot.
    30. Ran WorkWithMacroContainer macro from Tools | Macros menu in Word GUI, not from VBIDE.
    31. Msgbox showed that there were two toolbars!
    32. Exited from Word.
    33. Double-clicked on HowardKaikowDoubleVision-Revised.dot.
    34. An additional "String Names" toolbar was present, both had empty captions.
    35. Exited from Word.
    36. Repeatedly, double-clicked on HowardKaikowDoubleVision-Revised.dot and exited Word.
    37. Each time, the two empty captioned toolbars were present, however, unlike the case with the Normal template, they did not shift position.

  2. #42
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    OK Howard, here's something else very strange for you to ponder on:- I ran the following modification of your code inserted into a module of a "normal" word doc 23 times (typing something and saving each time) without a single recurrence of 'multiple toolbars'.

    Then, I went into VB edit and changed a comment slightly, saved it, exited and then re-opened the doc. There were then two toolbars present...on deleting these through view/toolbars/delete there were no more problems.

    Next, I went back into VB edit and changed the comments again....SAME THING! Every time something is changed in the module the problem re-appears, but if nothing is changed in the module...NO problems

    John

    [VBA]
    Option Explicit
    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 '//all that's really needed
    CommandBars(strToolbar).Delete

    '//the rest of the error handling procedure is superfluous as the
    '//only error you should get is the one from attempting to delete
    '//a non-existent toolbar. The 1st "On Error Resume Next" then shifts
    '//the procedure to the code below - i.e. to creating a toolbar.

    Set cmdbar = CommandBars.Add(Name:=strToolbar, Position:=msoBarTop, Temporary:=False, MenuBar:=False)
    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
    [/VBA]

  3. #43
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Howard, (from my previous) here's what appears to be the sequence of events when any of the code is changed:

    (Change = any change made in the code/comments)

    i) Change/Save/Exit/Open always gives you ONE toolbar

    ii) Change/Save/RUN/Exit/Open gives you either one or two toolbars

    iii) Change/RUN/Save/Exit/Open always you TWO toolbars

    ??? It almost seems in the last case that there appears to be two versions of the doc co-existing momentarily in the memory if the macro is run before exiting the first time, the original and the changed one, so the toolbar is shown twice - one for each version ????

    Edit: PS ...I know you've stated the following "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."....

    But after all this I still fail to see the reason why one would want to delete & replace a toolbar that already exists...also, as others have already stated, the problem simply doesn't arise when you put Temporary = True ?

    Or, as MD suggested, wouldn't it be simpler to maybe put the "delete" and/or "create" as 'DocumentOpen' and 'DocumentClose' events or t'other way round?

  4. #44
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    OF COURSE! THAT'S IT...(reading my last 2 posts)...If we make the procedure an Open event, any changes to the code can't be run until the document is saved and re-opened.

    Copy and paste the following code into "ThisDocument", save it, exit and open...

    [VBA]
    Option Explicit
    Private Sub Document_Open()

    Dim intBeforeIndex As Integer
    Dim cmdbar As CommandBar
    Dim cmdbutton As CommandBarButton
    Const strToolbar As String = "Double Vision HK VBA"

    On Error Resume Next
    CommandBars(strToolbar).Delete

    Set cmdbar = CommandBars.Add(Name:=strToolbar, Position:=msoBarTop, Temporary:=False, MenuBar:=False)
    cmdbar.Visible = True
    Set cmdbar = CommandBars(strToolbar)

    intBeforeIndex = 1
    Set cmdbutton = cmdbar.Controls.Add(Type:=msoControlButton, Temporary:=False, Before:=intBeforeIndex)
    With cmdbutton
    .Style = msoButtonCaption
    .OnAction = "modReplaceStringNames.ReplaceStringNames"
    .Caption = "String Names"
    End With
    End Sub
    [/VBA]

    Hopefully we can mark this solvered now

  5. #45
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by johnske
    OF COURSE! THAT'S IT...(reading my last 2 posts)...If we make the procedure an Open event, any changes to the code can't be run until the document is saved and re-opened.

    Copy and paste the following code into "ThisDocument", save it, exit and open...

    [VBA]
    Option Explicit
    Private Sub Document_Open()

    Dim intBeforeIndex As Integer
    Dim cmdbar As CommandBar
    Dim cmdbutton As CommandBarButton
    Const strToolbar As String = "Double Vision HK VBA"

    On Error Resume Next
    CommandBars(strToolbar).Delete

    Set cmdbar = CommandBars.Add(Name:=strToolbar, Position:=msoBarTop, Temporary:=False, MenuBar:=False)
    cmdbar.Visible = True
    Set cmdbar = CommandBars(strToolbar)

    intBeforeIndex = 1
    Set cmdbutton = cmdbar.Controls.Add(Type:=msoControlButton, Temporary:=False, Before:=intBeforeIndex)
    With cmdbutton
    .Style = msoButtonCaption
    .OnAction = "modReplaceStringNames.ReplaceStringNames"
    .Caption = "String Names"
    End With
    End Sub
    [/VBA]

    Hopefully we can mark this solvered now
    Not yet. That is not the problem I posed.
    The example I posted is a simplified example of a more general case.

    The code cannot be in Document_Open because it is not known at that time that the toolbar will be modified or deleted. The code to replace the toolbar might be run at any time, or not at all.

    And CustomizationContext must be used.

    A few minutes ago, I posed a further simplified example in CombinedHowardKaikowDoubleVision.zip at the same link.

  6. #46
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    My apologies if I misled anyone with my post last night. At least some of the time, and probably all of the time, when nothing happened it was bacuase I had run the wrong code. I just got too excited :rofl

    Hopefully we can mark this solvered now
    I don't see how this is solved by showing that something different works. The question as to why the original fails remains open.

    I will play with this over the day to see if I can distill anything from it. Of course this is old and greater minds than mine have, presumably, been applied to it. but I'm not one to let something like that get in the way of making a fool of myself.

    One thing I did was add a check for the command bar's existence, by looping through the collection, at various points in the code and it was always shown as expected (i.e not there after a delete, there after a create). Despite that I still got two toolbars. I will try checking the collection for duplicates later.

    It seems as though it's something that happens when Normal is saved. I thought perhaps there was an extra copy of an object lying around so made sure all object variables were set to nothing but it made no difference. I removed the "Set cmdbar .." line because it seemed redundant but that made no difference either.

    I'll check out the latest code at Howards's site and I might try playing with the CommandBars Update Event although I've found that to be a pain in the past as it gets invioked so often.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #47
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    [QUOTE=Howard Kaikow]Not yet. That is not the problem I posed.....

    The code cannot be in Document_Open because it is not known at that time that the toolbar will be modified or deleted. The code to replace the toolbar might be run at any time, or not at all.

    And CustomizationContext must be used....
    QUOTE]

    i) Re-insert "CustomizationContext = NormalTemplate" if you like - you'll find that it still works...

    ii) Apart from that, I just trimmed it up for posting by removing comments and some extraneous error procedures - the code is exactly the same as in your original...

    ii) If you read carefully, the problem I found was that the double tool-bars appeared if ANY changes were made to the code, so you must not run the code before the book was saved, exited and re-opened. If you simply follow that simple constraint there is no problems. (The Open event was simply introduced to counter anyone's tendency to run the code before saving and exiting)

    I also saved this as a MS word template and ran it - no probs. there either

    John

  8. #48
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Howard,

    Firstly, open a word doc on your computer, goto view/toolbars and delete any existing references to Double Vision HK VBA.

    Now download the attached word template, open it, save it as a word doc, then open the doc and run the macro as many times as you like and try to get a duplicate toolbar - does this or does this not do exactly what you want it to (delete and re-make a singletoolbar when the macro is run)?

    AFTER you're satisfied, have a look at the code (it's locked for viewing but the password is "Howard") - just remember that if you change any of the code don't be tempted to run it until AFTER you have saved and exited the doc....

    John

  9. #49
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by TonyJollans
    My apologies if I misled anyone with my post last night. At least some of the time, and probably all of the time, when nothing happened it was bacuase I had run the wrong code. I just got too excited :rofl


    I don't see how this is solved by showing that something different works. The question as to why the original fails remains open.

    I will play with this over the day to see if I can distill anything from it. Of course this is old and greater minds than mine have, presumably, been applied to it. but I'm not one to let something like that get in the way of making a fool of myself.

    One thing I did was add a check for the command bar's existence, by looping through the collection, at various points in the code and it was always shown as expected (i.e not there after a delete, there after a create). Despite that I still got two toolbars. I will try checking the collection for duplicates later.

    It seems as though it's something that happens when Normal is saved. I thought perhaps there was an extra copy of an object lying around so made sure all object variables were set to nothing but it made no difference. I removed the "Set cmdbar .." line because it seemed redundant but that made no difference either.

    I'll check out the latest code at Howards's site and I might try playing with the CommandBars Update Event although I've found that to be a pain in the past as it gets invioked so often.
    Please put off further analysis of my updated posted code.

    I may have found a SOLUTION, at least for the case that puts the toolbar in the MacroContainer template. Not yet tested for the Normal template.

    If I indeed did find a solution, then I gotta figure out why.

    My schedule for today, as I just woke up about an hour ago:

    1. Eat (one of my all too favorite activities).
    2. Test some more.
    3. Exercise (have not done so since Sunday!).
    4. Eat again!
    5. If necessary, and time permitting, test/analyze some more.
    6. Watch presidential candidate debate.
    7. Watch silly news analysts discuss debate.
    8. If necessary, test/analyze some more.
    9. Post result.

    If I have found the solution, the cause, at this time, is not very obvious.
    I might test in Word 97, Word 2000, Word 2002 and Word 2003 BEFORE I might believe the solution.

  10. #50
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can I suggest

    5.1 Large glass of Lagavulin malt whisky

    6.1 Even larger glass of Lagavulin malt whisky

  11. #51
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    MD: Do you think Howard should INSERT those steps? Or REPLACE them?
    ~Anne Troy

  12. #52
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To carry out steps 6 and 7, I consider 5.1 and 6.1 a prerequisite!

  13. #53
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Oh!!

    I thought the 1 and the 1 were ONE. i.e.:

    5. One Large glass of Lagavulin malt whisky

    6. One Even larger glass of Lagavulin malt whisky

    and I wondered why you were limiting him... ROFL!!
    ~Anne Troy

  14. #54
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Maybe limits are required, after all, there may be some testing and analysing to be done. Wouldn't want him ROF

  15. #55
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by mdmackillop
    Can I suggest

    5.1 Large glass of Lagavulin malt whisky

    6.1 Even larger glass of Lagavulin malt whisky
    I am not allowed to drink alcoholic beverages.

  16. #56
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Dreamboat
    I've added a second link to a .zip file.
    That link will contain my latest testing version.

  17. #57
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I sympathise; I don't think politicians should be taken without one, but maybe a large pinch of salt would do.

  18. #58
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Howard Kaikow
    Please put off further analysis of my updated posted code.

    I may have found a SOLUTION, at least for the case that puts the toolbar in the MacroContainer template. Not yet tested for the Normal template.

    If I indeed did find a solution, then I gotta figure out why.

    My schedule for today, as I just woke up about an hour ago:

    1. Eat (one of my all too favorite activities).
    2. Test some more.
    3. Exercise (have not done so since Sunday!).
    4. Eat again!
    5. If necessary, and time permitting, test/analyze some more.
    6. Watch presidential candidate debate.
    7. Watch silly news analysts discuss debate.
    8. If necessary, test/analyze some more.
    9. Post result.

    If I have found the solution, the cause, at this time, is not very obvious.
    I might test in Word 97, Word 2000, Word 2002 and Word 2003 BEFORE I might believe the solution.
    I have done steps 1 and 2, and part of 3, i.e., I've put on sweatsocks and exercise shoes.

    I added a link at the original URL to a 2nd .zip file that greatly simplifies the code and helps isolate what may be the problem.

    A common sub is used for all processing. The only difference is whether it is passed MacroContainer or NormalTemplate.

    So far, the code seems to work as desired using MacroContainer, but not with NormalTemplate.

    I have not yet investigated whether it would make any difference if the code were put in the NormalTemplate, in which case, there should be no difference between using MacroContainer or NormalTemplate for those cases in which I want the toolbar in the Normal Template. I would rarely desire this as I run with Normal template as read-only except when I need to do some testing or modifications. Anyway, application specific stuff should be in app specific templates.

    Running in Word 2003, the current code seems to work for MacroContainer, but not for NormalTemplate.

    Ditto for Word 97 and Word 2000.

    For Word 97, I had to add error handling in the output sub because it didn't like the Parent property, I've not investigated why.

    For Word 2000, I had to run a few times before the case using NormalTemplate failed. Similar to what I found 3+ years ago.

    Did not bother to try in Word 2002.

    Further pounding will determine whether the MacroContainer case really works.

    Obviously, there's an oddity with NormalTemplate as the problem does not surface immediately in Word 2000, but does in other versions.

    I'm not sure what can be done to further simplify the code, remembering that CustomizationContext must be used.

    I started to fool around with using OpenAsDocument, instead of using CustomizationContext, but that seemed to always store the toolbar in the NormalTemplate.

  19. #59
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by mdmackillop
    I sympathise; I don't think politicians should be taken without one, but maybe a large pinch of salt would do.
    Up until about 2 weeks ago, I had not had ANY salt, other than the salt already in food, in my house for more than 10 years.

    On 13 September 2004, I had oral surgery and the dentist wanted me to use a rinse of salt water and hydrogen peroxide. I purchased salt, but instead decided to use Peroxyl from Colgate-Palmolive. Let's see how many years I go without opening the box of salt!

    If I can tolerate oral surgery, I sure can tolerate the debate.

    Oh well, 'nuff fer now, gotta stretch, then exercise, then ...

  20. #60
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    I note you have not bothered to download my last attachment

    If you had, you would find that it's YOUR OWN original post from your URL, it's just been re-named and locked for viewing...

    As I've stated, it runs perfectly IF you leave the code alone - you get two tool-bars IMMEDIATELY on re-open if you run any changes to the code or the comments before saving and quitting.

    Obviously this is a MS bug that needs to be looked at by microsoft

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •