Consulting

Results 1 to 10 of 10

Thread: KeyBindings not working

  1. #1

    KeyBindings not working

    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.
    Last edited by Aussiebear; 11-28-2013 at 12:32 AM. Reason: Wrapped submitted code with vba tags

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You'll only be able to retain the keybindings in the document if: (a) it remains attached to the template; or (b) you add the code to the actual document, preferably before it is saved. For (b), that means all users who use this template will have to set Word to give trusted access to the VBA project model, something that severely compromises Word's security. Giving trusted access to the VBA project model is not something that can be done programmatically (with good reason) and a user changing it back to the default (no access) would break your process.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks for your information. We need to trust the VBA resource for various reasons anyway, so that is not a problem. What I am trying to do is from the attatched template, sendkeys to trigger something on the main template which runs a macro (SAVEHERE) on the main template because the attached template gets released prior to saving.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am not sure what you mean by "main" template, and what you mean by "released".

    There is ALWAYS an attached template for a document. You may unattach a template, but it is ALWAYS replaced by another, if not a specified template, then Normal.

    So is your "main" template actually a global template, that is, a code container? If it is a global template (and it is loaded) then you can fire directly.

  5. #5
    No, here is the layout.

    Main Form - That is where you see the code listed above. Also includes a procedure called SaveHere.
    Repository - This is the attached template which gets released.

    So the procedure is as follows:

    1) Opens Main template
    2) Attaches to Repository where most of the code is stored. There is a User Form part of the Repository which starts the save, but I need to release the Repository before actual saving.
    3) The way I was getting around this was by having the User Form activate a SendKeys of "Ctrl-Shift-A", which should activate the macro SaveHere on the form.

    This has worked in the past, but it is possible that there is a timing issue causing the repository to unattach and the sendkeys not to send.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I think there is some terminology confusion here.

    1) "Open Main template". I do not know what you mean by this. Do you mean you open an actual template file (.dotm, or .dot)?
    2) "Attaches to Repository". I do not know what you mean by this either. Again, I also do not know what you mean by "released". You can CHANGE an attached template, but the document still ALWAYS has some template attached to it. Do you mean you change the Repository template to another template (Main)?

    I am wondering with your 3) that "Main template" is not a template at all, but a USERFORM. But I have no idea what attached to Repository means.

    The bottom line is still what macropod states. SendKeys is a document level function, so it has to be in the document.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hmmm. I do notice that your code has AutoOpen, which implies that you are in fact opening a file. If this is a template file that you open only to get the code, this is a mistake. LOAD the template file as a global template (it is NOT opened; the code is loaded only). Once loaded all the code there is available.

  8. #8
    I am not obviously using the terminology right.

    Main Form - This is a .doc, not a template. The AutoOpen is in the doc.
    Repository - This is the Attached template (a .dot). This is not a global template because multiple documents may need to attach, and it needs to know which one since global variables for each file is set.

    Since this is an attached template, I want to detach the template prior to saving the file, but since most of the code is in the Repository (Attached Template) the way I had solved that problem is having the attached template use a Send Keys of (ctrl-Shift-A) to trigger the Subroutine SaveHere which has a keybinding on the Main Form.

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Well, there is a simple solution. Use a global template for the code you wish to use.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Well, there is a simple solution. Use a global template for the code you wish to use.
    Since this is an attached template, I want to detach the template prior to saving the file
    First of all, why? And replace it with what attach template? A different one? Normal?

    But anyway, let me see if I follow this.

    You open a document (Main form). The attached template for this document is Repository.dot.
    In the document (Main form) is a sub SaveHere which has a keybinding.

    If the DOCUMENT has a keybinding to a sub in the document, you do not actually need it. Code in the attached template can fire it directly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •