gettextfields

Trying to attach the file again but it seems that the mailing list strips it out.

Thanks,

Vieri

I've created two "Text Boxes" in a form in a writer document.

I can iterate over them like this grabbing the text as I go

Sub EnumerateFields

  oDoc = ThisComponent
  oDrawPage = oDoc.DrawPage
  oForm = oDrawPage.Forms.GetByIndex(0)
    For i = 0 To oForm.getCount()-1
       x = oForm.getByIndex(i)
       Print x.getName() & " : " & x.Text
    Next
  
End Sub

Does this help in anyway?

Iain

Thanks!

Making progress... finally.

Your code actually retrieved the text within my first "text box" (which is at least something!) but then it fails with the error:

LibreOffice 4.0.2.2
BASIC runtime error.
'423'
Text

it points to line:
Print x.getName() & " : " & x.Text

It may be choking on a "formatted field" within the document.

Just out of curiosity, how did you know that you had to use ThisComponent.DrawPage.Forms? As a novice, I find the documentation to be quite confusing and a supposedly "simple" task such as getting text field values seems to be quite difficult. I mean, I found examples on the net and they all start with something like:

  vEnum = ThisComponent.getTextFields().createEnumeration()
  If Not IsNull(vEnum) Then
    Do While vEnum.hasMoreElements()
      vVal = vEnum.nextElement()

but then I either don't know how to continue or the examples I found on the net don't work (maybe outdated OO or LO versions).

BTW, is there an IDE that can auto-complete Basic code (eg. vVal.??? as in the example above).

Thanks,

Vieri

Vieri ,

pleasqe find some working code below

hope it helps:

so every field has a "Placeholder" property who correspondents with the data who commes here from a beamer

     Dim aTextFields as Object
         Dim i as integer
         Dim CurElement as Object
         'xray.xray oDocument
         aTextfields = oDocument.getTextfields.CreateEnumeration
         While aTextFields.hasmoreElements'
             CurElement = aTextField.NextElement

             If CurElement.PropertySetInfo.hasPropertybyName("PlaceHolder")Then
                 If (CurElement.PlaceHolder = "Lezers") Then
                     CurElement.getAnchor.setString(sLezers)
                 ElseIf (CurElement.PlaceHolder = "Mag") Then
                     CurElement.getAnchor.setString(sNaam)
                 ElseIf (CurElement.PlaceHolder = "Half") Then
                     CurElement.getAnchor.setString(sHalf)
                 Elseif (CurElement.PlaceHolder = "Voll") Then
                     CurElement.getAnchor.setString(sVoll)
                 Elseif (CurElement.PlaceHolder = "pr1") Then
                     CurElement.getAnchor.setString(sPr1)
                 Elseif (CurElement.PlaceHolder = "prH") Then
                     CurElement.getAnchor.setString(sPrH)
                 Elseif (CurElement.PlaceHolder = "prV") Then
                     CurElement.getAnchor.setString(sPrV)
                 Elseif (CurElement.PlaceHolder = "advprijs1") Then
                     CurElement.getAnchor.setString(sadvprijs1)
                 Elseif (CurElement.PlaceHolder = "ipvprijs1") Then
                     CurElement.getAnchor.setString(sipvprijs1)
                 Elseif (CurElement.PlaceHolder = "1jaargang") Then
                     CurElement.getAnchor.setString(s1jaargang)
                 Elseif (CurElement.PlaceHolder = "btw6_1") Then
                     CurElement.getAnchor.setString(sbtw6_1)
                 Elseif (CurElement.PlaceHolder = "totaal1") Then
                     CurElement.getAnchor.setString(stotaal1)
                 Elseif (CurElement.PlaceHolder = "advprijs2") Then
                     CurElement.getAnchor.setString(sadvprijs2)
                 Elseif (CurElement.PlaceHolder = "ipvprijs2") Then
                     CurElement.getAnchor.setString(sipvprijs2)
                 Elseif (CurElement.PlaceHolder = "2jaargang") Then
                     CurElement.getAnchor.setString(s2jaargang)
                 Elseif (CurElement.PlaceHolder = "btw6_2") Then
                     CurElement.getAnchor.setString(sbtw6_2)
                 Elseif (CurElement.PlaceHolder = "totaal2") Then
                     CurElement.getAnchor.setString(stotaal2)

                 Endif
             End if
         Wend
     Endif

Hi :slight_smile:
You might find it well worth buying the 3rd party guide produced by Andrew Pitonyak.  This link helps get you there
https://wiki.documentfoundation.org/Documentation/Other_Documentation_and_Resources#Programmers

Andrew works hard on this list and in the Docs Team to help people with bits&bobs of code as do other people on this list but the book brings it all together rather well.  It might be a good investment.  I'm not sure how badly it is affected by alleged changes in the newer version of LO Basic being used by 4.0.0 and beyond but i suspect the book gives such a good overview that changes in the details are easy to figure out.

Regards from
Tom )

Hi Vieri

The form you've created contains a set of controls of which one or more of
them are of the "Text box" variety.

You may also have other controls on the form such as a "Check Box", "Label" or
"Push Button" these latter controls don't have the ability to hold text or
edit text so don't have the .Text property. So for these controls the code
x.Text will fail.

Try this

Sub EnumerateFields

  oDoc = ThisComponent
  oDrawPage = oDoc.DrawPage
  oForm = oDrawPage.Forms.GetByIndex(0)
    For i = 0 To oForm.getCount()-1
       x = oForm.getByIndex(i)
       if x.supportsService("com.sun.star.form.component.TextField") then
           Print x.getName() & " : " & x.Text
         End If
    Next
  
End Sub

Iain

Thanks Iain.
Your help is much appreciated. The macro works.
Now I'm trying to send HTTPS POST requests to my web server (not succeeding but that's another story).

Thanks again!

Vieri

Yes, that's the default behaviour for most mailing lists, I think. Not
that it matters anymore, since you seem to have your problem solved,
but next time you could upload the file somewhere and just give us the
link here.

Johnny Rosenberg

http://www.pitonyak.org/OOME_3_0.odt
http://www.pitonyak.org/AndrewMacro.odt

This document contains information on forms and such

http://www.pitonyak.org/database/AndrewBase.odt

Hi Vieri

The form you've created contains a set of controls of which
one or more of
them are of the "Text box" variety.

You may also have other controls on the form such as a
"Check Box", "Label" or
"Push Button" these latter controls don't have the ability
to hold text or
edit text so don't have the .Text property. So for these
controls the code
x.Text will fail.

Try this

Sub EnumerateFields

     oDoc = ThisComponent
     oDrawPage = oDoc.DrawPage
     oForm = oDrawPage.Forms.GetByIndex(0)
       For i = 0 To oForm.getCount()-1
          x =
oForm.getByIndex(i)
          if
x.supportsService("com.sun.star.form.component.TextField")
then
      
           Print x.getName() & " : " & x.Text
              End If
       Next
   End Sub

Thanks Iain.
Your help is much appreciated. The macro works.
Now I'm trying to send HTTPS POST requests to my web server (not succeeding but that's another story).

i use a "create a Windows" object to do the posting "WinHttp.WinHttpRequest.5.1"

hope it helps
greetz
Fernand

Function MicrosoftTranslate(sLanguageFrom As String, sLanguageTo As String, sText As String) As String

   ' On Error Goto err_catch0

     Dim ID As String
     Dim sURL As String
     Dim oH As object
     Dim sToken As String

     ID = ""
     sURL = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=&appId=" & ID & "&from=" & sLanguageFrom & "&to=" & sLanguageTo & "&text=" & JSencodeURLpart(sText)
     sToken = GetAccessToken()
     oH = CreateObject("WinHttp.WinHttpRequest.5.1")
     oH.Open "POST", sURL, False
     oH.setRequestHeader "Authorization", "Bearer " & sToken
     oH.send
     t = oH.ResponseText

     MicrosoftTranslate = mid(t,3,len(t)-3)

     Set oH = Nothing

exit_sub:
     Exit Function

err_catch0:
   msgbox("err_catch0 " & Err & Error & Erl,48)

     Resume exit_sub
End Function

Function GetAccessToken() As String

     On Error Goto err_catch2

     Dim mtToken As String
     webRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
     Dim URI As String
     Dim txtToken As String

     URI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"

     'Client ID from https://datamarket.azure.com/developer/applications
     Dim clientId As String
     clientId = "myclientid"

     'Client secret from https://datamarket.azure.com/developer/applications
     Dim clientSecret As String
     clientSecret = "JTEGDEb1OViegnPz0kzkvRWhOSeNRJpmPgjqauyeV8k="

     Dim sRequest As String
     sRequest = "grant_type=client_credentials" & _
     "&client_id=" & JSencodeURLpart(clientId, false) & _
     "&client_secret=" & JSencodeURLpart(clientSecret, False) & _
     "&scope=http://api.microsofttranslator.com"

     webRequest.Open("POST",URI, False)
     webRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
     webRequest.send (srequest)
     mttoken = WebRequest.ResponseText

    Dim arr As Variant, header As String

     header = """access_token"":""" '"&HMACSHA256="
     footer = """,""expires_in"":"""
   headerpos = instr(mttoken, header)+len(header)
   footerpos = instr(mttoken, footer)
   tokenl = footerpos-headerpos
     txtToken = mid(mttoken ,headerpos , tokenl)
' xray txttoken
' If txtToken = "_request" Then Resume err_catch:

     GetAccessToken = txtToken

exit_sub:
      Exit Function

err_catch2:
beep
     msgbox("err_catch2 " & Err & Error & Erl, 48)
     Resume exit_sub
End Function

Function URLEncode(StringToEncode As String, UsePlusRatherThanHexForSpace As Boolean ) As String

     On Error Goto err_catch3

     Dim TempAns As String
     Dim CurChr As Integer

     CurChr = 1
     Do Until CurChr - 1 = Len(StringToEncode)
          Select Case Asc(Mid(StringToEncode, CurChr, 1))
         Case 48 To 57, 65 To 90, 97 To 122
             TempAns = TempAns & Mid(StringToEncode, CurChr, 1)
         Case 32
             If UsePlusRatherThanHexForSpace = True Then
                 TempAns = TempAns & "+"
             Else
                 TempAns = TempAns & "%" & Hex(32)
             End If
         Case Else
             TempAns = TempAns & "%" & _
             Right("0" & Hex(Asc(Mid(StringToEncode, _
             CurChr, 1))), 2)
         End Select

         CurChr = CurChr + 1
     Loop
     URLEncode = TempAns

exit_sub:
     Exit Function

err_catch3:
beep
    msgbox("err_catch3 " & Err & Error & Erl ,48)
     Resume exit_sub

End Function

Thanks Fernand.

I'm currently using Java (HttpURLConnection) to do the HTTP stuff (easier).
For things that are simpler to do with Basic, I run other macros from within my Java macro (with XScript).

Thanks for your suggestion.

Vieri