View Full Version : Help getting dynamic menu of files to work

09-16-2015, 06:35 AM
Hi there.

Can someone help me please. I have some code that loads all the files in a directory to a dynamic menu on the ribbon. On the first press it works fine - loads about 10 files. But when I press the refresh button it only loads one file (when there are about 10). Is it something to do with clearing my variables? Help!

Private Sub GetContent(control As IRibbonControl, ByRef returnedVal)
'Populate a menu item
Dim sXML As String
Dim lFiles As Long
Dim lFileCount As Long
Dim objFile As Object
Dim objFiles As Object
Dim fso As Object
Dim strUserTemplatesPath As String
strUserTemplatesPath = Options.DefaultFilePath(wdUserTemplatesPath)

'Set error handling
On Error GoTo CloseTags
'Open the XML string
sXML = "<menu xmlns=""XXXXXX"">" 'XXXX denotes html code that I needed to take out for this post
'Check for files in the directory
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strUserTemplatesPath).Files
'Cycle through files, adding Word template files to the menu
For Each objFile In objFiles
If LCase(Left(objFile.Name, 3)) = "sfs" And InStr(objFile.Name, "&") < 1 Then
lFiles = lFiles + 1
sXML = AddButtonXML(sXML, "Button" & lFiles, False, objFile.Path, _
False, objFile.Name, False, True, "FileNew", "btnCentral")
Debug.Print vbCrLf & sXML
'Add the file path to a collection of objects for later retrieval
Dim sFileTip As New clsFilePaths
sFileTip.sFilePath = objFile.Path
FilePaths.Add Item:=sFileTip, Key:=CStr(lFiles)
Set sFileTip = Nothing
End If
Next objFile
'Add refresh button and close the menu tag
sXML = sXML & "<button id=""RefreshTemplateList"" label=""Refresh List"" imageMso=""RecurrenceEdit"" onAction=""DynamicMenu_onAction""/> "
'Close the menu string
sXML = sXML & "</menu>"
'Return the completed XML to the RibbonUI
returnedVal = sXML
End Sub