14. marts 2008 - 08:43Der er
7 kommentarer og 1 løsning
Multilinie beskyttelse af formularfelter
Jeg har et dokument, der indeholder en del formularfelter, som ikke må være multiline. Det vil altså sige, at hvis brugeren trykker Enter, skal der ikke laves et linieskift i formularfeltet, men derimod skal fokus flyttes til næste formularfelt.
Hvordan gøres det lettest, og kræver det lidt VBA så er det også helt i orden, men så skal den være en generalmetode der modtager et felt som parameter, og dermed pricipielt kan bruges på alle formularfelter
Mød TrackMan og Veo på Computerworld Cloud & AI Festival og hør, hvordan tech ændrer måden, vi træner og udvikler talent – fra skolebold til The Masters.
Denne løsning er fundet hos Microsofts - indsæt koden i ThisDocument:
Sub AutoOpen() ' This macro will reassign the ENTER key when you open an existing ' Word form fields document. CustomizationContext = ActiveDocument.AttachedTemplate ' Bind the Enter key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" End Sub Sub EnterKeyMacro() ' Check whether the document is protected for forms ' and whether the protection is active. If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _ Selection.Sections(1).ProtectedForForms = True Then ' Retrieve the bookmark of the current selection. ' This is equivalent to the name of the form field. myformfield = Selection.Bookmarks(1).Name ' Go to the next form field if the current form field ' is not the last one in the document. If ActiveDocument.FormFields(myformfield).Name <> _ ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _ .Name Then ActiveDocument.FormFields(myformfield).Next.Select Else ' If the current form field is the last one, ' go to the first form field in the document. ActiveDocument.FormFields(1).Select End If Else ' If the document is not protected for forms, ' insert a tab stop character. Selection.TypeText Chr(13) End If End Sub Sub AutoClose() CustomizationContext = ActiveDocument.AttachedTemplate FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable ' Disables prompt to save template changes. Templates(1).Save End Sub
Ja, det virker, dejligt, dog lige et ekstra spørgsmål. Der er enkelte felter, som gerne må være multiline, hvordan søger jeg få at det er muligt på disse felter?
Fandt selv ud af hvordan det sidste skulle gøres, (det hjælper at kalde tingene korrekt :-) )
Så koden er nu:
Sub AutoOpen() ' This macro will reassign the ENTER key when you open an existing ' Word form fields document. CustomizationContext = ActiveDocument.AttachedTemplate ' Bind the Enter key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" End Sub Sub EnterKeyMacro()
' Check whether the document is protected for forms ' and whether the protection is active. If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _ Selection.Sections(1).ProtectedForForms = True Then ' Retrieve the bookmark of the current selection. ' This is equivalent to the name of the form field. myformfield = Selection.Bookmarks(1).Name
If ActiveDocument.FormFields(myformfield).Name <> "adresse" And _ ActiveDocument.FormFields(myformfield).Name <> "bemærkning" And _ ActiveDocument.FormFields(myformfield).Name <> "anvendelse" Then
' Go to the next form field if the current form field ' is not the last one in the document. If ActiveDocument.FormFields(myformfield).Name <> _ ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _ .Name Then ActiveDocument.FormFields(myformfield).Next.Select Else ' If the current form field is the last one, ' go to the first form field in the document. ActiveDocument.FormFields(1).Select
End If
Else FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable End If Else ' If the document is not protected for forms, ' insert a tab stop character. Selection.TypeText Chr(13) End If End Sub Sub AutoClose() CustomizationContext = ActiveDocument.AttachedTemplate FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable ' Disables prompt to save template changes. Templates(1).Save End Sub
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.