Attribute VB_Name = "modInit" Option Explicit Function Init() As Boolean ' Notify user to save all projects. If AnyUnsavedProjects Then MsgBox "Before using this tool, please save any " _ & "unsaved VBA projects." Init = False GoTo Init_End End If ' Verify that the user has no more than one project named ' "VBAProject" or "Project" (in Word). If EnsureNamedProjects = False Then Init = False GoTo Init_End End If Init = True Init_End: Exit Function End Function Function AnyUnsavedProjects() As Boolean ' Determine whether user has any unsaved projects. Dim strFileName As String Dim vbpProject As VBIDE.VBProject AnyUnsavedProjects = False On Error Resume Next For Each vbpProject In gobjVBE.VBProjects ' Check whether project has an associated file name. ' If not, it has not been saved. strFileName = vbpProject.FileName If Err <> 0 Then AnyUnsavedProjects = True Exit For End If Next End Function Function MoreThanOneUnnamedProject() As Boolean ' Check whether there is more than one project named ' "VBAProject" or "Project" open in the Visual Basic Editor. Dim vbpProject As VBIDE.VBProject Dim lngCount As Long Dim lngCountWord As Long MoreThanOneUnnamedProject = False On Error Resume Next For Each vbpProject In gobjVBE.VBProjects ' Default project name for Excel and PowerPoint ' is "VBAProject." If StrComp(vbpProject.Name, "VBAProject", _ vbTextCompare) = 0 Then lngCount = lngCount + 1 If lngCount > 1 Then MoreThanOneUnnamedProject = True Exit For End If End If ' Handle Visual Basic Editor in Word separately. If StrComp(vbpProject.Name, "Project", _ vbTextCompare) = 0 Then lngCountWord = lngCountWord + 1 If lngCountWord > 1 Then MoreThanOneUnnamedProject = True Exit For End If End If Next End Function Function EnsureNamedProjects() As Boolean ' Prompt user to rename projects that still have the default ' name. Dim vbpProject As VBIDE.VBProject Dim strFileName As String If MoreThanOneUnnamedProject Then ' Prompt user to rename projects. If MsgBox("Before using this tool, you should rename " _ & "any VBA projects that still " _ & "have the default name ""VBAProject.""" & vbCrLf _ & "Do you want to automatically rename your VBA " _ & "projects now?", vbYesNo + vbQuestion) = vbYes Then ' Rename projects using file name without spaces and ' without the file extension. For Each vbpProject In gobjVBE.VBProjects If vbpProject.Name = "VBAProject" Or vbpProject.Name = "Project" Then ' Trim extra white space from file name. strFileName = TrimSpace(vbpProject.FileName) ' Remove single spaces from file name. strFileName = Replace(strFileName, " ", "") ' Rename project, ignoring errors. An error ' may occur if you name a project with a ' VBA keyword. On Error Resume Next vbpProject.Name = ParsePath(strFileName, _ FILE_ONLY_NO_EXT) On Error GoTo 0 End If Next EnsureNamedProjects = True Else MsgBox "Please rename your VBA projects and run " _ & "this add-in again." EnsureNamedProjects = False End If Else EnsureNamedProjects = True End If End Function