Dim msErrMsg As String
'--2007 attributes and namespace
Const ATT_TARGET_2007 As String = "customUI/customUI.xml"
Const ATT_TYPE_2007 As String = _
"http://schemas.microsoft.com/office/2006/relationships/ui/extensibility"
Const NS_2007 As String = "http://schemas.microsoft.com/office/2006/01/customui"
'--2010 attributes and namespace
Const ATT_TARGET_2010 As String = "customUI/customUI14.xml"
Const ATT_TYPE_2010 As String = _
"http://schemas.microsoft.com/office/2007/relationships/ui/extensibility"
Const NS_2010 As String = "http://schemas.microsoft.com/office/2009/07/customui"
Public Sub RestoreMyRibbon()
'--restores customUI ribbon to this workbook using ribbonX code stored in
' cell A1 of sheet with codename wksCustomRibbonBackup
'--process is to copy existing workbook to temp folder. workbook file is
' unzipped, xml file is written to customUI.xml file, relationships
' are updated, items are zipped back into workbook which is saved to
' same folder as source (thisworkbook) with a unique name (using time stamp)
Dim bSourcePathIsURL As Boolean
Dim oFSO As Object
Dim oFile As Object
Dim sCustomUI_Filename As String, sRibbonXML As String
Dim sTempFolderPath As String, sTempFilePath As String
Dim sTargetZipFilePath As String
Dim sNewWorkbookFolder As String, sNewWorkbookFilePath As String
'On Error GoTo ErrProc
Set oFSO = CreateObject("Scripting.FileSystemObject")
'--get and validate ribbon xml stored in this workbook
sRibbonXML = wksCustomRibbonBackup.Cells(1).Value
Select Case True
'***remove space after < in next line (needed for posting)
Case Left(sRibbonXML, 16) <> "< customUI xmlns="
msErrMsg = "Valid XML code for Custom Ribbon not found."
GoTo ExitProc
Case Mid(sRibbonXML, 18, Len(NS_2007)) = NS_2007
sCustomUI_Filename = "customUI.xml"
Case Mid(sRibbonXML, 18, Len(NS_2010)) = NS_2010
sCustomUI_Filename = "customUI14.xml"
Case Else
msErrMsg = "XML code for Custom Ribbon is unrecognized version."
GoTo ExitProc
End Select
'--copy workbook to temp dir
sTempFolderPath = sGetEmptyTempFolder(sTempFolderName:="RestoreMyRibbon")
If sTempFolderPath = vbNullString Then GoTo ExitProc
sTempFilePath = sTempFolderPath & "\" & ThisWorkbook.Name
ThisWorkbook.SaveCopyAs (sTempFilePath)
Set oFile = oFSO.GetFile(sTempFilePath)
'--assign variables for final destination of workbook
If LCase$(Left(ThisWorkbook.Path, 4)) = "http" Then
'--if thisworkbook at URL path then target is user's desktop folder
sNewWorkbookFolder = Environ$("USERPROFILE") & "\Desktop"
Else
'--else target is same folder as this workbook
sNewWorkbookFolder = ThisWorkbook.Path
End If
sNewWorkbookFilePath = sNewWorkbookFolder & "\" _
& oFSO.GetBaseName(oFile) _
& "(with Ribbon)" & Format(Now, " yyyy-mm-dd h-mm-ss") & "." _
& oFSO.GetExtensionName(oFile)
'--rename with .zip extension
oFSO.GetFile(sTempFilePath).Name = oFSO.GetBaseName(oFile) & ".zip"
'--unzip into \Items subfolder
oFSO.CreateFolder (sTempFolderPath & "\Items")
Call Unzip( _
sSourceFilePath:=sTempFolderPath & "\" & oFSO.GetBaseName(oFile) & ".zip", _
sTargetFolderPath:=sTempFolderPath & "\Items")
'--write custom ribbon xml to file
Call WriteCustomUI_XML_ToFile(sRibbonXML:=sRibbonXML, _
sCustomUI_FolderPath:=sTempFolderPath & "\Items\customUI", _
sCustomUI_Filename:=sCustomUI_Filename)
'--update rels file
Call UpdateRels(sTopFolderOfItems:=sTempFolderPath & "\Items", _
sCustomUI_Filename:=sCustomUI_Filename)
If Len(msErrMsg) Then GoTo ExitProc
'--rezip
sTargetZipFilePath = sTempFolderPath & "\RibbonRestored.zip"
Call Zip(sSourceFolderPath:=sTempFolderPath & "\Items", _
sTargetFilePath:=sTargetZipFilePath)
'--copy file unique name and Excel extension
oFSO.CopyFile sTargetZipFilePath, sNewWorkbookFilePath
MsgBox "A copy of this workbook with its custom ribbon restored was saved to: " _
& vbCr & vbCr & sNewWorkbookFilePath
ExitProc:
On Error Resume Next
'--delete temp files and folder
If oFSO.FolderExists(sTempFolderPath) Then
oFSO.DeleteFolder (sTempFolderPath)
End If
If Len(msErrMsg) Then
MsgBox msErrMsg, vbCritical
msErrMsg = vbNullString
End If
Exit Sub
ErrProc:
msErrMsg = Err.Number & "-" & Err.Description
Resume ExitProc
End Sub