ASkolnick
11-27-2013, 07:45 AM
I am looking to add KeyBindings to a word document, however the document will not be attached to the normal template since it uses an attached template. The KeyBinding itself has to be on the document since I need to remove the attached template prior to the file saving.
I have tried the following with CustomizationContext=ActiveDocument, but the problem is then the attached template does not load.
The code looks as follows:
Sub AutoOpen() ' AutoOpen for Word forms
Dim NameofRep As String
#If ExcelForm Then
#Else
Dim sRepositoryFile, sServerFilePath, CompName
Dim sMsg
CreateWordFormDocument ' ***** do this first of all
' must unprotect to add AddIn
If WordFormDocument.ProtectionType <> wdNoProtection Then WordFormDocument.Unprotect
' Stop AutoOpen for debugging (this will only debug the form -- not the repository)
If (PropertyRead(CP_STOP) <> "") Then Stop
If PropertyRead(CP_DOCWASSAVED) = "" Then DocumentIsNew = True
If AutoOpenSkip Then Exit Sub ' v05 check for DesignMode etc
gbRunMode = True
On Error Resume Next
If Environ("clientname") = "" Then
CompName = Trim(Environ("computername"))
Else
CompName = Trim(Environ("clientname"))
End If
sLocalFolderPath = LOCAL_PATH & CompName & "\" & Environ("username") & "\" & REPOSITORY_SUBFOLDER
'KillRepositoryFiles sLocalFolderPath
If FileNotFound(sLocalFolderPath) Then MkDir sLocalFolderPath
zCheckErr "MkDir " & sLocalFolderPath
sRepositoryFile = PropertyRead("Repository") ' eg Repository.dot
sServerFilePath = GetFormsXMLPath & REPOSITORY_SUBFOLDER & sRepositoryFile ' eg ...\FormsXML\Repository\Repository.dot
sLocalFilePath = sLocalFolderPath & sRepositoryFile ' eg C:\SRS\Repository\Repository.dot
WordFormDocument.AttachedTemplate = ""
' If server copy is newer than local copy or if local copy is not found, attempt
' to copy from ...\FormsXML\Repository to C:\srs\Repository
' (FileGetDateTime returns 1/1/1900 if files don't exist eg when testing with local folders)
If Application.Documents.Count > 1 Then
OpenMultipleCode sServerFilePath, sRepositoryFile
Application.OnTime Now + TimeSerial(0, 0, 0.5), "FormRunRepositoryAutoopen"
Exit Sub
Else
If FileGetDateTime(sServerFilePath) > FileGetDateTime(sLocalFilePath) Then
' can give 'file in use' if repository is attached to an open doc or was not properly released
FileCopy sServerFilePath, sLocalFilePath
zCheckErr "FileCopy to " & sLocalFilePath
End If
If FileNotFound(sLocalFilePath) Then
sMsg = "Code Repository not Found" & vbCr & ExplainXMLPath & vbCr & "Press OK to exit"
MsgBox sMsg, , MsgBoxTitle
Else
' there is a local repository but we were unable to update it
' give a warning but allow the form to load
If FileGetDateTime(sServerFilePath) > FileGetDateTime(sLocalFilePath) Then
sMsg = "Warning -- failed to update local Repository " & vbCr & msErrLog
MsgBox sMsg, , MsgBoxTitle
End If
On Error GoTo errlab
Application.DisplayAlerts = wdAlertsNone
WordFormDocument.AttachedTemplate = sLocalFilePath
End If
Application.OnTime Now + TimeSerial(0, 0, 0.5), "FormRunRepositoryAutoopen" 'Delay seems to be needed
End If
CustomizationContext = WordFormDocument
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyShift, wdKeyA), _
KeyCategory:=wdKeyCategoryMacro, _
Command:="SaveHere"
Exit Sub
errlab:
sMsg = "Error loading code repository"
MsgBox sMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description, , MsgBoxTitle
#End If
End Sub
Any help is greatly appreciated.
I have tried the following with CustomizationContext=ActiveDocument, but the problem is then the attached template does not load.
The code looks as follows:
Sub AutoOpen() ' AutoOpen for Word forms
Dim NameofRep As String
#If ExcelForm Then
#Else
Dim sRepositoryFile, sServerFilePath, CompName
Dim sMsg
CreateWordFormDocument ' ***** do this first of all
' must unprotect to add AddIn
If WordFormDocument.ProtectionType <> wdNoProtection Then WordFormDocument.Unprotect
' Stop AutoOpen for debugging (this will only debug the form -- not the repository)
If (PropertyRead(CP_STOP) <> "") Then Stop
If PropertyRead(CP_DOCWASSAVED) = "" Then DocumentIsNew = True
If AutoOpenSkip Then Exit Sub ' v05 check for DesignMode etc
gbRunMode = True
On Error Resume Next
If Environ("clientname") = "" Then
CompName = Trim(Environ("computername"))
Else
CompName = Trim(Environ("clientname"))
End If
sLocalFolderPath = LOCAL_PATH & CompName & "\" & Environ("username") & "\" & REPOSITORY_SUBFOLDER
'KillRepositoryFiles sLocalFolderPath
If FileNotFound(sLocalFolderPath) Then MkDir sLocalFolderPath
zCheckErr "MkDir " & sLocalFolderPath
sRepositoryFile = PropertyRead("Repository") ' eg Repository.dot
sServerFilePath = GetFormsXMLPath & REPOSITORY_SUBFOLDER & sRepositoryFile ' eg ...\FormsXML\Repository\Repository.dot
sLocalFilePath = sLocalFolderPath & sRepositoryFile ' eg C:\SRS\Repository\Repository.dot
WordFormDocument.AttachedTemplate = ""
' If server copy is newer than local copy or if local copy is not found, attempt
' to copy from ...\FormsXML\Repository to C:\srs\Repository
' (FileGetDateTime returns 1/1/1900 if files don't exist eg when testing with local folders)
If Application.Documents.Count > 1 Then
OpenMultipleCode sServerFilePath, sRepositoryFile
Application.OnTime Now + TimeSerial(0, 0, 0.5), "FormRunRepositoryAutoopen"
Exit Sub
Else
If FileGetDateTime(sServerFilePath) > FileGetDateTime(sLocalFilePath) Then
' can give 'file in use' if repository is attached to an open doc or was not properly released
FileCopy sServerFilePath, sLocalFilePath
zCheckErr "FileCopy to " & sLocalFilePath
End If
If FileNotFound(sLocalFilePath) Then
sMsg = "Code Repository not Found" & vbCr & ExplainXMLPath & vbCr & "Press OK to exit"
MsgBox sMsg, , MsgBoxTitle
Else
' there is a local repository but we were unable to update it
' give a warning but allow the form to load
If FileGetDateTime(sServerFilePath) > FileGetDateTime(sLocalFilePath) Then
sMsg = "Warning -- failed to update local Repository " & vbCr & msErrLog
MsgBox sMsg, , MsgBoxTitle
End If
On Error GoTo errlab
Application.DisplayAlerts = wdAlertsNone
WordFormDocument.AttachedTemplate = sLocalFilePath
End If
Application.OnTime Now + TimeSerial(0, 0, 0.5), "FormRunRepositoryAutoopen" 'Delay seems to be needed
End If
CustomizationContext = WordFormDocument
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyShift, wdKeyA), _
KeyCategory:=wdKeyCategoryMacro, _
Command:="SaveHere"
Exit Sub
errlab:
sMsg = "Error loading code repository"
MsgBox sMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description, , MsgBoxTitle
#End If
End Sub
Any help is greatly appreciated.