Как передать выделенный текст документа в строковую переменную?

Автор tvitaly1, 11 марта 2012, 14:55

0 Пользователи и 1 гость просматривают эту тему.

tvitaly1

Microsoft Word VBA делаю так:
Dim s as string

s:=selection.text

В результате в переменную s заносится текст выделенного фрагмента.

А как это сделать  Basic Write?

Рыбка Рио

Можно так:

REM  *****  BASIC  *****

Sub Main
Dim s As String
Dim selection As Object
selection = ThisComponent.CurrentSelection
if  selection.count = 1 then
   s = selection.getByIndex(0).String
   msgbox s
else
   for i=1 to selection.count - 1
      s = s + selection.getByIndex(i).String + chr(13)
   next
   msgbox s
endif
End Sub
ubuntu 12.04 + LibO3.6.0

tvitaly1

Цитата: Клио от 11 марта 2012, 18:01
Можно так:

REM  *****  BASIC  *****

Sub Main
Dim s As String
Dim selection As Object
selection = ThisComponent.CurrentSelection
if  selection.count = 1 then
   s = selection.getByIndex(0).String
   msgbox s
else
   for i=1 to selection.count - 1
      s = s + selection.getByIndex(i).String + chr(13)
   next
   msgbox s
endif
End Sub


Спасибо, Клио!
Вот функция, для модуля Selection :)

Function Text As String
Dim selection As Object
selection = ThisComponent.CurrentSelection
if  selection.count = 1 then
  Text = selection.getByIndex(0).String
else
   Text=""
  for i=1 to selection.count - 1
     Text = Text + selection.getByIndex(i).String + chr(13)
  next
end if

end Function






tvitaly1

К стати о птичках, конец абзаца в Write кодируется двумя символами - chr(13)+chr(10)!

Рыбка Рио

Цитата: tvitaly1 от 17 марта 2012, 09:11конец абзаца в Write кодируется двумя символами - chr(13)+chr(10)!
На Windows. На Linux только chr(13).
ubuntu 12.04 + LibO3.6.0

tvitaly1

Цитата: Клио от 17 марта 2012, 11:32
На Windows. На Linux только chr(13).
А почему так?
Я решил программно устранить эту вещь. :) Вот код программы удаляющий повторяющиеся пробелы в выделенном текстею

ЦитироватьSub Main
dim s as string, s1 as string, i as long
  s1=TextCopy
  'Удалим повторяющиеся пробелы в выделеных фрагментах
  s=""
  s1="@"+s1
  For i=2 to len(s1)
   if not( mid(s1,i-1,1)=" " and mid(s1,i,1)=" " ) then
    s = s+mid(s1,i,1)
   end if
  next i   
   TextPaste s 

End Sub

Function TextCopy As string
dim i as long
    TextCopy=""
  with ThisComponent.CurrentSelection 
   for i=0 to .Count - 1
      if i>0 then
      TextCopy = TextCopy + chr(13)+"&"
      end if
      TextCopy = TextCopy + .getByIndex(i).String
   next
  end with
end Function

sub TextPaste(s as string) As string
dim i as long, j as long
   j = 0   
   with ThisComponent.CurrentSelection 
   s = s+" "
   s1=""
   for i=1 to len(s)-1
    if mid(s,i,1)<>chr(13) then
      s1=s1+mid(s,i,1)
    elseIf mid(s,i+1,1)=chr(10) then
      s1=s1+mid(s,i,1)
      i=i+1
    'новый фрагмент
    elseIf mid(s,i+1,1)="&" then
      .getByIndex(j).String=s1
      s1=""
      j=j+1
      i=i+1
    end if     
   next
     .getByIndex(j).String=s1
   
  end with
 
end su