Avatar billede lsskaarup Nybegynder
14. marts 2008 - 08:43 Der 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
Avatar billede supertekst Ekspert
16. marts 2008 - 17:45 #1
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
Avatar billede supertekst Ekspert
04. april 2008 - 09:25 #2
???
Avatar billede lsskaarup Nybegynder
04. april 2008 - 16:13 #3
Sorry jeg har simplethen ikke noget at kigge nærmere på det
Avatar billede supertekst Ekspert
09. april 2008 - 23:10 #4
ok...
Avatar billede lsskaarup Nybegynder
09. maj 2008 - 11:20 #5
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?
Avatar billede supertekst Ekspert
09. maj 2008 - 23:36 #6
Evt. lade de felter, hvor multiline er tilladt være anbragt i sektioner, der ikke er beskyttet.
Avatar billede lsskaarup Nybegynder
10. maj 2008 - 00:03 #7
Det kan ikke lade sig gøre, da felterne med multilinie skal ligge i samme linie, som nogle af de uden multilinie.

Kan man ikek på en eller anden måde, lave en if sætning, der tjekker på navnet af felterne?
Avatar billede lsskaarup Nybegynder
13. maj 2008 - 15:37 #8
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
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Tag et kursus i Word og øg effektiviteten

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester