Method 'Add' of object '_CommandBars' Failed RRS feed

  • Question

  • I've struggled to resolve this error. The code is mine, and I know it to work, but lately I've had people receive this error message. Sometimes recreating the resolves the problem, but not in all cases, and sometimes the problem returns.

    It's really hard to pin down as I cannot repeat the error myself, and not on everyone's computer. I work in a firm of around 150 people. Most people are fine, but of late this error has inexplicably began to crop up for seemingly random people.

    Things I've tried:

    Office Repair/Reinstall
    Restart of computer

    Any thoughts?

    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, August 12, 2011 8:55 PM

All replies

  • The code in question:

    Public Sub CreateCustomCommandBar(ByVal strCommandBarName As String, Optional strDocumentTitle As String)
    Dim cbCommandBar As CommandBar
    Set cbCommandBar = ActiveDocument.CommandBars.Add(Name:=strCommandBarName, Position:=msoBarTop, MenuBar:=False, Temporary:=True)
    cbCommandBar.Visible = True
    End Sub

    This might be related to a different issues I was having:

    MS Access 2010, Word 2010 Automation & Windows 7
    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Friday, August 19, 2011 8:24 PM
  • Hi Zamdrist

    That's an awfully long and meandering discussion. Could you please summarize what the problem was, in the end, and what action solved it (besides getting rid of New Document, which is the first thing that I noticed)?

    I thought I caught a glimpse of "network" and "template". There have been issues, related to Office 2010 handles security with macro-enabled templates stored in a network location. With certain security settings, such templates cause issues. A person came through the forum last week and posted that the following hotfix did solve those problems for him. I'm not able to test and confirm, but here's the info:

    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Calvin_GaoModerator Monday, August 22, 2011 12:47 PM
    • Unmarked as answer by ITMn0403 Monday, August 22, 2011 12:57 PM
    Saturday, August 20, 2011 11:41 AM
  • Essentially it came down to placing the line: objWord.Visible immediately after Set objWord = New Word.Application.

    It appeared that the code was ahead of the Word and the subsequent loading of the template.

    A snippet of the good code follows:

    Set objWord = New Word.Application
    objWord.Visible = True
    objWord.Documents.Add "C:\"
    objWord.Selection.GoTo wdGoToBookmark, , , "ApplicantName1"


    Facebook: Zamdrist
    Roleplaying: Elliquiy

    Monday, August 22, 2011 1:01 PM
  • Thank you for the additional detail. I can't see how the two things could be related, but perhaps they are...

    One thing that stands out for me in you CommandBars code is that you're not setting the CustomizationContext (WordApplication.CustomizationContext = ActiveDocument). This tells Word where to save the change. If you don't specify, things become a bit of a lottery on where the change is saved, where later changes might be saved, including anything that's deleted.

    That you mention renaming of makes me suspect specifying CustomizationContext could help. You need this in every procedure that initiates changes in the CommandBars.

    Note: Selection.GoTo is NOT the most efficient way to write information to bookmarks. Better:

    Dim wdDoc as Word.Document
    Set wdDoc = objWord.Documents.Add("C:\")
    wdDoc.Bookmarks("NameOfBookmark").Range.Text = "the text for the bookmark"

    Cindy Meister, VSTO/Word MVP
    Monday, August 22, 2011 1:27 PM
  • Thank you Cindy. Great info! I was never aware of the CustomizationContext reference. Unfortunately I'll have to wait until I have a user with the problem crop up again. It's not repeatable, not on demand at least. If history is any measure though I should have at least one user this week that has the problem however. Thanks.
    Facebook: Zamdrist
    Roleplaying: Elliquiy
    Monday, August 22, 2011 1:31 PM