Forum at OOoForum.orgThe Forum
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Paragraph Position Count - VBA into OO?

Post new topic   Reply to topic Forum Index -> Macros and API
View previous topic :: View next topic  
Author Message

Joined: 21 May 2012
Posts: 1

PostPosted: Mon May 21, 2012 2:58 pm    Post subject: Paragraph Position Count - VBA into OO? Reply with quote

I hope someone can help me here Smile I'm working with OO Writer and i need to find a specific Paragraph in a .odt file

For Example i have a text with 608 Paragraphs in total (File - Properties - Statistics) and i want to find the 258th Paragraph in this text.

I found a Makro for Word which will do that (not exactly but good enought). It tells me the position i have choosen in the document. But this Makro (VBA) does not work with OO and the OO.odt i somehow cannot export or save as .docx.

Maybe someone knows how i can find a specific paragraph number or can help me with edditing the existing vba-makro ... that it will work in OO

Sub WhereAmI()
    MsgBox "Paragraph number: " & GetParNum(Selection.Range) & vbCrLf & _
    "Absolute line number: " & GetAbsoluteLineNum(Selection.Range) & vbCrLf & _
    "Relative line number: " & GetLineNum(Selection.Range)
End Sub
Function GetParNum(r As Range) As Integer
    Dim rParagraphs As Range
    Dim CurPos As Long
    CurPos = ActiveDocument.Bookmarks("\startOfSel").Start
    Set rParagraphs = ActiveDocument.Range(Start:=0, End:=CurPos)
    GetParNum = rParagraphs.Paragraphs.Count
End Function
Function GetLineNum(r As Range) As Integer
     'relative to current page
    GetLineNum = r.Information(wdFirstCharacterLineNumber)
End Function
Function GetAbsoluteLineNum(r As Range) As Integer
    Dim i1 As Integer, i2 As Integer, Count As Integer, rTemp As Range
        i1 = Selection.Information(wdFirstCharacterLineNumber)
        Selection.GoTo what:=wdGoToLine, which:=wdGoToPrevious, Count:=1, Name:=""
        Count = Count + 1
        i2 = Selection.Information(wdFirstCharacterLineNumber)
    Loop Until i1 = i2
    GetAbsoluteLineNum = Count
End Function

I am a noob at this, and i was really proud that i could get the word-makro started.. xD so i would be very thankful if someone could help me with that.. It will save me a lot of counting (paragraphs xD)

Thank you already!
Back to top
View user's profile Send private message

Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Tue May 22, 2012 8:01 am    Post subject: Reply with quote

Remember that blank paragraphs count.
Sub FindParagraphNumber
n = InputBox ("Enter desired paragraph number.")
If n = "" then End
n = CInt(n)
oDoc = ThisComponent
oVC = oDoc.CurrentController.getViewCursor
oTC = oDoc.Text.createTextCursor 'At beginning by default
pn = 1
Do While pn < n
 If oTC.gotoNextParagraph(false) then
   pn = pn + 1
  Else Exit Do
If pn < n then MsgBox("There are only " & pn & " paragraphs." : End
If oTC.isEndOfParagraph then
 MsgBox("Pargraph # " & pn & " is blank.")
REM Highlight the paragraph.
REM Show the paragraph.
MsgBox(oTC.String,0,"Paragraph # " & pn)
End Sub
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Macros and API All times are GMT - 8 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group