Ask Your Question
0

Remove all blank text boxes from a page in Draw

asked 2019-01-08 00:21:51 +0100

starryknight64 gravatar image

I have a LO Draw document with lots of blank text boxes on it. Rather than SHIFT+Click each one (problematic) and delete, I'd like to write a macro to do it.

I've tried the following and it error'd out:

vEnum = ThisComponent.getTextFields().createEnumeration()
REM ERROR: BASIC runtime error. Property or method not found: getTextFields.
edit retag flag offensive close merge delete

Comments

TextField is a kind of object occurring inserted as TextPortion inside a text (date field, link with URL, ...). No match here.
As far as I know Draw never creates blank TextBox. It is even complicated to get one at all. It seems you are talking of TextBox containg a string of spaces only. Of course, there are additional characters leaving the shape visually "blank".

Lupp gravatar imageLupp ( 2019-01-09 21:21:41 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-01-09 18:28:16 +0100

starryknight64 gravatar image

updated 2019-01-09 18:29:51 +0100

Ended up doing quite a bit of analysis on what makes a Text Field different from anything else and came up with this. The key here is a lot of these fields were really small which explains the check for its height.

Sub doIt()
  oPages = thisComponent.DrawPages
  For n = 0 to oPages.Count-1
    oPage = oPages.GetbyIndex(n)
    If oPage.HasElements then
      do
        removedBlankField = False
        For m = 0 to oPage.Count-1
          oPageElement = oPage.GetByIndex(m)
          if oPageElement.UINamePlural = "Text Frame" and oPageElement.Size.Height >160 then
            if Trim(oPageElement.String) = "" then
              oPageElement.Parent.remove(oPageElement)
              removedBlankField = True
              GoTo removed
            end if
          End If
        Next
        removed:
      loop until removedBlankField = False
    End If
  Next
End Sub
edit flag offensive delete link more

Comments

A few remarks:
Your code processes all the slides (DrawPage), not just one.
I suppose you first tried to do it without the "removedBlankField trick" and got errors because the indexes of oPage changed if a shape got removed. You can avoid this by looping through the shapes in reverse order. (Untested: You might also avoid the error by an immediate m = m - 1 in Basic where loop controlling variables accept assignments.)
Did you consider grouped shapes? What about entering them?
The UIName properties are a bad choice when trying to select TextShape. Use the .ShapeType property instead

Lupp gravatar imageLupp ( 2019-01-09 21:37:22 +0100 )edit

This was tested:

Sub removeAllTexBoxesContainingOnlySpace()
doc0 = ThisComponent
dPgs = doc0.DrawPages
u1   = dPgs.Count - 1
For k = 0 To u
  dPg = dPgs(k)
  u2 = dPg.Count - 1
  For j = u2 To 0 Step -1 
    sh = dPg(j)
    If sh.ShapeType = "com.sun.star.drawing.GroupShape" Then
      REM Nothing implemented, but...
      REM you might want once to resolve the group...
    Else
      If sh.ShapeType = "com.sun.star.drawing.TextShape" Then 
        t = Trim(sh.String)
        If t="" Then dPg.remove(sh)
      Else
        REM Nothing implemented, but...
      End If
    End If
  Next j
Next k
End Sub
Lupp gravatar imageLupp ( 2019-01-09 21:39:48 +0100 )edit

I just also took the time, to test the "untested" variant.
It also works. Of course you need to use a named variable (say u) initalised with oPage.Count-1 for the loop control's upper limit, and it must be decremented at the same time as m.
m = m - 1 : u = u - 1.

Lupp gravatar imageLupp ( 2019-01-09 21:47:39 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-08 00:21:51 +0100

Seen: 23 times

Last updated: Jan 09