geekgirlau
02-05-2008, 12:33 AM
Hi guys,
I'd like to clarify that I understand correctly how Word works with globally-loaded code templates. Forgive me if there's a little too much detail here, but I want to make sure I'm not barking up the wrong tree!
Background:
I've inherited an application written in Word VBA that consists of 5 globally-loaded code templates. Attempting to sort out the spaghetti in the back end and create processes for testing, installation, upgrades etc.
Code Templates:
1 of these templates is a repository of functions and procedures that are called by each of the other 4 templates.
Questions:
Is it necessary to each of the other 4 templates to have a hard-coded reference to this "library" template?
Is there another way to do this?
Is it possible to call functions and procedures from the library template without having the reference?This situation causes me issues due to the hard-coded reference. For example, a site may have X:\MyApp\System and X:\MyApp TEST\System. On completing a test process, it is not possible to just copy the code templates to the production folder, as the references have to be updated first to point to the new file location.
I have attempted an "update references" procedure, but it's a little hit and miss to say the least. The issue appears to be due to the fact that the code templates have already been globally loaded. It is also unsuccessful if I have users on Word 2002.
Future Direction:
This application is going to be completely redeveloped on a non-VBA platform in future, but given that this may still be some time away, I want to make sure that the current product is made as stable, secure and reliable as I can in its current format.
My understanding is that to redevelop this as a COM addin is a fair amount of work (still lots of code here even after slashing through the spaghetti with a chainsaw :devil2: ). This would certainly resolve the security issue, but is it worth the effort given the future redevelopment plans?
Fix Reference code:
Private Sub FixReferences()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
' Purpose: The system templates contain a reference to the Library
' template. Unfortunately this means that it often
' hangs onto the wrong reference. This procedure:
'
' * Prompts for the location of the current system files
' * Opens all system templates
' * Removes all existing references to the Library template
' * Adds a reference to the current version of the Library
' template
'
' Restrictions: * Requires a reference to Microsoft Visual Basic for
' Application Extensibility
' * Must have "Trust access to Visual Basic project" selected in
' Macro Security
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
Dim dlgOpen As Object
Dim ref As Reference
Dim strTemplate(0 To 3) As String
Dim strFolder As String
Dim i As Integer
strTemplate(0) = "Code1"
strTemplate(1) = "Code2"
strTemplate(2) = "Code3"
strTemplate(3) = "Code4"
If vbNo = MsgBox("Have you ticked" & vbCrLf & vbCrLf & _
" 'Trust access to Visual Basic project'" & vbCrLf & _
" (Tools | Macro | Security | Trusted Publishers)", _
vbQuestion + vbYesNo + vbDefaultButton2, "Trusted access") Then
MsgBox "You MUST tick this option before you can continue:" & vbCrLf & vbCrLf & _
vbTab & "Tools" & vbCrLf & _
vbTab & "Macro" & vbCrLf & _
vbTab & "Security" & vbCrLf & _
vbTab & "Trusted Publishers" & vbCrLf & _
vbTab & "Trust access to Visual Basic project", vbCritical, _
"Update Cancelled"
Else
FindLibrary:
MsgBox "Please navigate to the current LIBRARY system template," & vbCrLf & _
"and double-click on the file", vbInformation, "Find Library Template"
Set dlgOpen = Dialogs(wdDialogFileOpen)
With dlgOpen
If .Display = -1 Then
If LCase(.Name) <> "library.dot" Then
GoTo FindLibrary
End If
strFolder = Options.DefaultFilePath(wdCurrentFolderPath)
For i = 0 To UBound(strTemplate)
Documents.Open strFolder & "\" & strTemplate(i) & ".dot", , , False
For Each ref In ActiveDocument.VBProject.References
If LCase(ref.Name) = "library" Then
ActiveDocument.VBProject.References.Remove ref
End If
If InStr(1, ref.Name, "~") > 0 Then
ActiveDocument.VBProject.References.Remove ref
End If
Next ref
ActiveDocument.VBProject.References.AddFromFile _
strFolder & "\Library.dot"
ActiveDocument.Close True
Next i
MsgBox "Make sure you remove the " & vbCrLf & vbCrLf & _
" 'Trust access to Visual Basic project'" & vbCrLf & _
" (Tools | Macro | Security | Trusted Publishers)" & _
vbCrLf & vbCrLf & "now that the references are done.", _
vbInformation, "Trusted access"
End If
End With
End If
On Error Resume Next
ActiveDocument.Close False
End Sub
I'd like to clarify that I understand correctly how Word works with globally-loaded code templates. Forgive me if there's a little too much detail here, but I want to make sure I'm not barking up the wrong tree!
Background:
I've inherited an application written in Word VBA that consists of 5 globally-loaded code templates. Attempting to sort out the spaghetti in the back end and create processes for testing, installation, upgrades etc.
Code Templates:
1 of these templates is a repository of functions and procedures that are called by each of the other 4 templates.
Questions:
Is it necessary to each of the other 4 templates to have a hard-coded reference to this "library" template?
Is there another way to do this?
Is it possible to call functions and procedures from the library template without having the reference?This situation causes me issues due to the hard-coded reference. For example, a site may have X:\MyApp\System and X:\MyApp TEST\System. On completing a test process, it is not possible to just copy the code templates to the production folder, as the references have to be updated first to point to the new file location.
I have attempted an "update references" procedure, but it's a little hit and miss to say the least. The issue appears to be due to the fact that the code templates have already been globally loaded. It is also unsuccessful if I have users on Word 2002.
Future Direction:
This application is going to be completely redeveloped on a non-VBA platform in future, but given that this may still be some time away, I want to make sure that the current product is made as stable, secure and reliable as I can in its current format.
My understanding is that to redevelop this as a COM addin is a fair amount of work (still lots of code here even after slashing through the spaghetti with a chainsaw :devil2: ). This would certainly resolve the security issue, but is it worth the effort given the future redevelopment plans?
Fix Reference code:
Private Sub FixReferences()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
' Purpose: The system templates contain a reference to the Library
' template. Unfortunately this means that it often
' hangs onto the wrong reference. This procedure:
'
' * Prompts for the location of the current system files
' * Opens all system templates
' * Removes all existing references to the Library template
' * Adds a reference to the current version of the Library
' template
'
' Restrictions: * Requires a reference to Microsoft Visual Basic for
' Application Extensibility
' * Must have "Trust access to Visual Basic project" selected in
' Macro Security
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
Dim dlgOpen As Object
Dim ref As Reference
Dim strTemplate(0 To 3) As String
Dim strFolder As String
Dim i As Integer
strTemplate(0) = "Code1"
strTemplate(1) = "Code2"
strTemplate(2) = "Code3"
strTemplate(3) = "Code4"
If vbNo = MsgBox("Have you ticked" & vbCrLf & vbCrLf & _
" 'Trust access to Visual Basic project'" & vbCrLf & _
" (Tools | Macro | Security | Trusted Publishers)", _
vbQuestion + vbYesNo + vbDefaultButton2, "Trusted access") Then
MsgBox "You MUST tick this option before you can continue:" & vbCrLf & vbCrLf & _
vbTab & "Tools" & vbCrLf & _
vbTab & "Macro" & vbCrLf & _
vbTab & "Security" & vbCrLf & _
vbTab & "Trusted Publishers" & vbCrLf & _
vbTab & "Trust access to Visual Basic project", vbCritical, _
"Update Cancelled"
Else
FindLibrary:
MsgBox "Please navigate to the current LIBRARY system template," & vbCrLf & _
"and double-click on the file", vbInformation, "Find Library Template"
Set dlgOpen = Dialogs(wdDialogFileOpen)
With dlgOpen
If .Display = -1 Then
If LCase(.Name) <> "library.dot" Then
GoTo FindLibrary
End If
strFolder = Options.DefaultFilePath(wdCurrentFolderPath)
For i = 0 To UBound(strTemplate)
Documents.Open strFolder & "\" & strTemplate(i) & ".dot", , , False
For Each ref In ActiveDocument.VBProject.References
If LCase(ref.Name) = "library" Then
ActiveDocument.VBProject.References.Remove ref
End If
If InStr(1, ref.Name, "~") > 0 Then
ActiveDocument.VBProject.References.Remove ref
End If
Next ref
ActiveDocument.VBProject.References.AddFromFile _
strFolder & "\Library.dot"
ActiveDocument.Close True
Next i
MsgBox "Make sure you remove the " & vbCrLf & vbCrLf & _
" 'Trust access to Visual Basic project'" & vbCrLf & _
" (Tools | Macro | Security | Trusted Publishers)" & _
vbCrLf & vbCrLf & "now that the references are done.", _
vbInformation, "Trusted access"
End If
End With
End If
On Error Resume Next
ActiveDocument.Close False
End Sub