On 05/06/2013 05:21 AM, Vieri wrote:
Hi,

I have an odt writer document with 2 input text fields (added with the form 
designer toolbar). I manually type text into one of the fields. I copy/pasted a 
macro to see if I could grab the text within the input field but haven't had 
any luck. This is the test macro in Basic:

Sub EnumerateFields
   vEnum = ThisComponent.getTextFields().createEnumeration()
   If Not IsNull(vEnum) Then
     If vEnum.hasMoreElements() Then
       Print "OK"
     Else
       Print "NOT OK"
     End If
   End If
End Sub

If I manually run this macro I get the "NOT OK" message.

What am I doing wrong?
What is the correct way of getting the text field elements?

Thanks

Vieri



I grabbed these two routines from my test modules without running them first.... so I have no idea what state they are in, but without taking the time to create a test document and such..... Perhaps these will help you. I don't even know why I have them. Probably have better coverage in OOME_3.odt or AndrewMacro.odt (but I did not check).

REM Author: Andrew Pitonyak
Sub EnumerateTextFields(Optional oUseDoc)
  Dim oDoc      'Document to use.
  Dim oEnum     'Enumeration of the text fields.
  Dim oField    'Enumerated text field.
  Dim s$        'Generic string variable.
  Dim n%        'Count the number of text fields.
  Dim nUnknown% 'Count the unexpected field types.
  Dim bDisplayExpressions As Boolean
  bDisplayExpressions = True


  If IsMissing(oUseDoc) Then
    oDoc = ThisComponent
  Else
    oDoc = oUseDoc
  End If

  oEnum = oDoc.getTextFields().createEnumeration()
  If IsNull(oEnum) Then
    Print "getTextFields().createEnumeration() returns NULL"
    Exit Sub
  End If

  Do While oEnum.hasMoreElements()
    oField = oEnum.nextElement()
    If oField.supportsService("com.sun.star.text.TextField.Input") Then
      REM If you update Content, use oDoc.TextFields.refresh() afterwards.
      n = n + 1
      s = s & "Input (" & oField.getPropertyValue("Hint") & ", " & _
              oField.getPropertyValue("Content") & ")" & CHR$(10)
    ElseIf oField.supportsService("com.sun.star.text.TextField.User") Then
      REM You can update the text master field, but be certain to use
      REM oDoc.TextFields.refresh() afterwards.
      n = n + 1
      s = s & "User (" & oField.TextFieldMaster.Name & ", " & _
              oField.TextFieldMaster.Value & ", " & _
              oField.TextFieldMaster.InstanceName & ")" & CHR$(10)
ElseIf oField.supportsService("com.sun.star.text.TextField.PageNumber") Then
      REM Ignore page number fields
ElseIf oField.supportsService("com.sun.star.text.TextField.GetReference") Then REM Ignore references to Listings, Tables, and Figures because I want to. If oField.SourceName <> "Listing" AND oField.SourceName <> "Table" AND oField.SourceName <> "Figure" Then
        n = n + 1
s = s & "Reference (" & oField.SourceName & ", " & oField.getPresentation(False) & ")" & CHR$(10)
      End If
ElseIf oField.supportsService("com.sun.star.text.TextField.SetExpression") Then
      REM This includes things such as Tables, Listing, and Figures
      REM The values will be similar to "SetExpression (Table, Table + 1)"
      If bDisplayExpressions Then
        n = n + 1
s = s & "SetExpression (" & oField.VariableName & ", " & oField.Content & ")" & CHR$(10)
        Inspect oField
      End If
ElseIf oField.supportsService("com.sun.star.text.TextField.Chapter") OR _
oField.supportsService("com.sun.star.text.TextField.DocInfo.Title") OR _
oField.supportsService("com.sun.star.text.TextField.DocInfo.ChangeDateTime") OR _
oField.supportsService("com.sun.star.text.TextField.Bibliography") OR _
oField.supportsService("com.sun.star.text.TextField.Annotation") Then
      n = n + 1
s = s & oField.getPresentation(True) & " : " & oField.getPresentation(False) & CHR$(10)
    Else
      nUnknown = nUnknown + 1
      n = n + 1
      If nUnknown = 1 Then inspect(oField)
s = s & "Unknown : " & oField.getPresentation(True) & " : " & oField.getPresentation(False) & CHR$(10)
    End If
    If n > 50 Then
      MsgBox s, 0, "Text Fields"
      s = ""
      n = 0
    End If
  Loop
  If n > 0 Then
    MsgBox s, 0, "Text Fields"
    s = ""
    n = 0
  End If
If nUnknown > 0 Then Print "Found " & nUnknown & " unexpected field types"
  Print "Finished"
End Sub



Sub DisplayMasterFields(Optional oUseDoc)
  Dim oMasters     'Text field masters from the document.
  Dim sNames()     'Text field master names.
  Dim i%           'Generic loop variable.
  Dim sVal$
  Dim s$           'Generic string variable.
  Dim oMasterField 'A particular master field.
  Dim sUserType$   'Holds the name for a User defined text field master

  sUserType = "com.sun.star.text.FieldMaster.User"

  If IsMissing(oUseDoc) Then
    oMasters = ThisComponent.getTextFieldMasters()
  Else
    oMasters = oUseDoc.getTextFieldMasters()
  End If
  sNames() = oMasters.getElementNames()
  s = "===Text Field Masters==="
  For i = LBound(sNames()) to UBound(sNames())
    s = s & Chr$(13) & "(" & sNames(i)
    oMasterField = oMasters.getByName(sNames(i))
    If Not IsNull(oMasterField) Then
      s = s & "," & oMasterField.Name
      If (Left$(sNames(i),Len(sUserType)) = sUserType) Then
        REM If the user type is an expression, then you can access
        REM the Value property, which is a double.
        s = s & "," & oMasterField.Content

        REM In general, you will not get here You will
        REM have entries with names such as:
        REM com.sun.star.text.FieldMaster.SetExpression.Illustration
        REM com.sun.star.text.FieldMaster.SetExpression.Table
        REM com.sun.star.text.FieldMaster.SetExpression.Text
        REM com.sun.star.text.FieldMaster.SetExpression.Drawing
        REM com.sun.star.text.FieldMaster.User
      End If
    End If
    s = s & ")"
  Next i
  MsgBox s, 0, "Text Field Masters"
End Sub


--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php


--
To unsubscribe e-mail to: users+unsubscr...@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Reply via email to