Remove CustomUI from finished document with VBA

I have a document template that works perfectly in Word 2007. There is a custom UI that adds a tab with a few controls to the ribbon. The user uses these controls and any built-in ones necessary to assemble a document in a particular form, then saves it. The document is saved WITHOUT the custom UI, since it is not necessary. The custom code mostly invokes a few XSL transforms, so there is no functionality that I want to carry along with the completed document - once the transform runs, my code is finished - any further dolling up is done with standard controls, always available in any document.

I use the following code to intercept the Save and SaveAs... functions:

 

Public Sub FileSave()

ActiveDocument.AttachedTemplate = ""

Dialogs(wdDialogFileSaveAs).Show

End Sub

 

Public Sub FileSaveAs()

ActiveDocument.AttachedTemplate = ""

Dialogs(wdDialogFileSaveAs).Show

End Sub

 

In Word 2007, this works fine, the custom tab on the ribbon disappears and the document is saved without it. But in Word 2010, the tab stays, even though the underlying code disappears, resulting in load errors every time the finished document is opened, or when a (no longer functional) button is clicked on the custom tab.

The document is generated fine, but this inability to remove the custom ribbon tab results in a very tacky-looking finished product. How do I make this custom tab go away completely in 2010?

Pete

 

February 20th, 2011 9:38pm

Try attaching the Normal.dotm te

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2011 11:16pm

Thanks Doug, but no luck. When I execute

ActiveDocument.AttachedTemplate = "Normal.dotm"

the tab disappears from the document, but after saving and re-opening the document, the tab is back. Non functional, but still there. When I open the doc file with a Zip program, all the custom UI stuff is still in place - OnLoad call, tab definition, icon images...

Try attaching the Normal.dotm template

February 21st, 2011 2:42pm

Sorry, partial update here - this started as a 2003 project and I just realized that when I save my generated document as a .DOC file, all is well - no extra tab. My principal customer skipped from 2003 to 2010, whereas I have 2007 on my machine. When I save the generated doc as a .DOCX file, the tab stays, regardless of 2007 or 2010.

I guess I have a useable workaround for the moment - save in 2003 format. But it's still a pain in the fundament. It should be possible to remove this custom stuff, shouldn't it?

Free Windows Admin Tool Kit Click here and download it now
February 21st, 2011 2:57pm

How is the document being created from the Template?  By use File>New and selecting the template as the basis for the document of by using File>Open and actually opening the template.  The former is the proper way to

February 21st, 2011 4:02pm

The former, certainly. Also via shortcut method, by double-clicking directly on the template, whose default method is NEW, not OPEN.
Free Windows Admin Tool Kit Click here and download it now
February 21st, 2011 4:13pm

When you re-open the document after attaching the normal.dotm template, and then go to the Developer tab and Document Template, what template is attached to the doc

February 24th, 2011 1:10pm

Normal, just as I would expect from executing the line

ActiveDocument.AttachedTemplate = "Normal.dotm"

The code is gone too. But the custom tab with icons remains, as does the attempt to execute some start-up code. When I convert the resulting doc to a zip file and look inside, it still has the custom icon folder, with icons, and the customization file "customUI.xml", which contains this instruction:

<customUI onLoad="onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">

Naturally, the call to onLoad fails, since all the code is gone, giving me an "Unable to execute macro" error whenever I open the document.



Free Windows Admin Tool Kit Click here and download it now
February 24th, 2011 3:28pm

Normal, just as I would expect from executing the line

ActiveDocument.AttachedTemplate = "Normal.dotm"

The code is gone too. But the custom tab with icons remains, as does the attempt to execute some start-up code. When I convert the resulting doc to a zip file and look inside, it still has the custom icon folder, with icons, and the customization file "customUI.xml", which contains this instruction:

<customUI onLoad="onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">

Naturally, the call to onLoad fails, since all the code is gone, giving me an "Unable to execute macro" error whenever I open the document.


I experience a similar problem: I have a template which includes a ribbon with an additional custom group and some macros. When a docx file based on this template is generated, the customized ribbon is copied to the docx file -- which means that a customui.xml file is automatically included in the docx file. This has the effect that as long as the template is connected to the file, the additional custom group is shown twice (one comes from the customui.xml file included in the template, the other one from the customui.xml file included in the docx file).

The most weird thing is that it seems to depend on the user installation whether the customui.xml file is automatically included in the docx file. We tried it on several systems (all having 2007 installed), and on some, the saved docx file included the customui.xml file, on others it didn't. We already checked all Word Options, but didn't find any relevant differences.

It would be great to find out how  this behavior can be influenced ...!

Thank you!
Katja

 

April 6th, 2011 8:13pm

I discovered that the CustomUI-files are copied from the template to the document when the template is not in a trusted location. When I moved the template to a trusted location this behavior stopped!

/Åsa Holmgren

Free Windows Admin Tool Kit Click here and download it now
June 14th, 2011 11:13am

I too have recently experienced a similar problem. Documents created from the template on my PC work fine but on my customer's PC, when they save, close and then open the document again they have 2 sets of ribbon modifications because customui14.xml has been copied to the document. I've tried to eliminate what can be causing the problem and deduced it was something to do with their security settings.
September 23rd, 2011 7:57pm

experiencing same issue here, whereby one template includes the customui in documents created from it, but only on a couple of machines.

have read the suggestions and none either provide a solution, nor a reason why this is happening.

did this get resolved?

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2013 5:38pm

having experienced this same issue cindy meister pointed me to using the openxml sdk

After some dirt on my hands i managed to produce a tool to remove the ribbon using .Net4 and the OpenXML SDK 2.5.

The code posted below is very crude and a couple of points to note are that a 2007 document contains a customisation part of type RibbonExtensibilityPart, whereas a 2010 document contains a part of type RibbonAndBackstageCustomizationsPart.  To produce a proper working tool the ApplicationVersion document property of the ExtendedFilePropertiesPart should be checked first to determine which customisaton part to try and remove.

You need to add a reference to the DocumentFormat.OpenXML and WindowsBase assemblies in your project.

code below______________________________________________________________

using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;

public static class CustomUI
    {

        public static void Delete(string filename)
        {
            try
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))
                {
                    //  delete any 2010 customisations
                    if (myDoc.GetPartsCountOfType<RibbonAndBackstageCustomizationsPart>() > 0)
                    {
                        Console.WriteLine("Deleting 2010 customUI from {0}.", filename);
                        myDoc.DeletePart(myDoc.GetPartsOfType<RibbonAndBackstageCustomizationsPart>().First());
                    }
                    //  delete any 2007 customisations
                    else if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                    {
                        Console.WriteLine("Deleting 2007 customUI from {0}.", filename);
                        myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());
                    }
                    //  no customisations
                    else
                    {
                        Console.WriteLine("{0} does not contain any customui.", filename);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} contains invalid openxml.\n{1}", filename, ex.ToString());
            }
        }
    }

July 25th, 2013 5:53am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics