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

Convert ASCII text by eliminating extra paragraph breaks
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
fredwang
General User
General User


Joined: 30 Jul 2008
Posts: 8

PostPosted: Mon Jan 19, 2009 5:32 am    Post subject: Remove Duplicate Spaces Reply with quote

hi,

i have tried to run this sub however it comes up as a series of errors


is there any way to make it stand alone??


regards

Fred




Sub DeleteExcessInteriorSpaces(FandR as Object)
SM = Chr(165) 'Space marker.
FandR.setSearchString("^ *") 'find any number of spaces at beginning of paragraph
Find = oDoc.findFirst(FandR)
While NOT isNull(Find)
Find.String = String(Len(Find.String),SM) 'replace with placeholders
Find = oDoc.findNext(Find.End,FandR)
Wend
FandR.setSearchString(" *") 'find any number of spaces
FandR.setReplaceString(" ") 'replace with one space
oDoc.ReplaceAll(FandR) 'do it
FandR.setSearchString("^" & SM & "*") 'find any number of placeholders at beginning of line
Find = oDoc.findFirst(FandR) 'turn them back into spaces
While NOT isNull(Find)
Find.String = String(Len(Find.String)," ")
Find = oDoc.findNext(Find.End,FandR)
Wend
End Sub
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


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

PostPosted: Mon Jan 19, 2009 9:20 am    Post subject: Reply with quote

fredwang,

Try this.
Code:
Sub DeleteExcessInteriorSpaces '(FandR as Object)
oDoc = ThisComponent
FandR = oDoc.createReplaceDescriptor
FandR.searchRegularExpression = true
SM = Chr(165) 'Space marker, i.e., ¥.
FandR.setSearchString("^ *") 'find any number of spaces at beginning of paragraph
Find = oDoc.findFirst(FandR)
While NOT isNull(Find)
Find.String = String(Len(Find.String),SM) 'replace with placeholders
Find = oDoc.findNext(Find.End,FandR)
Wend
FandR.setSearchString(" *") 'find any number of spaces
FandR.setReplaceString(" ") 'replace with one space
oDoc.ReplaceAll(FandR) 'do it
FandR.setSearchString("^" & SM & "*") 'find any number of placeholders at beginning of line
Find = oDoc.findFirst(FandR) 'turn them back into spaces
While NOT isNull(Find)
Find.String = String(Len(Find.String)," ")
Find = oDoc.findNext(Find.End,FandR)
Wend
End Sub
Back to top
View user's profile Send private message
fredwang
General User
General User


Joined: 30 Jul 2008
Posts: 8

PostPosted: Mon Jan 19, 2009 5:47 pm    Post subject: Convert ASCII text by eliminating extra paragraph breaks Reply with quote

hi,


thanks for that, it worked to a point, however i did a test with:

Fred
Bill

(ie) 4 spaces then Fred then 4 spaces etc

it did strip the 3 spaces after Fred but left the spaces before Fred

as i read the code the spaces before the paragraph should have been stripped or am i missing something

thanks for your quick reply i appreciate it


regards
fred
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


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

PostPosted: Tue Jan 20, 2009 5:44 am    Post subject: Reply with quote

fred,

The code is specifically designed to maintain spaces before a paragraph like an indent. If you just want to strip any space(s) at the beginning and multiple spaces within a paragraph try this.
Code:
Sub StripExtraSpaces
oDoc = ThisComponent
FandR = oDoc.createReplaceDescriptor
FandR.searchRegularExpression = true
FandR.setSearchString("^ *") 'Find one or more spaces at beginning of paragraph.
oDoc.ReplaceAll(FandR)  'Do it and replace with nothing.
FandR.setSearchString("  *") 'Find 2 or more spaces.
FandR.setReplaceString(" ") 'Replace with one space.
oDoc.ReplaceAll(FandR)  'Do it.
End Sub
Back to top
View user's profile Send private message
fredwang
General User
General User


Joined: 30 Jul 2008
Posts: 8

PostPosted: Tue Jan 20, 2009 9:33 am    Post subject: Convert ASCII text by eliminating extra paragraph breaks Reply with quote

hi again

excellent, this is what i need - i did re-read the macro and figured that it was meant to leave the spaces before.

during OCR, particularly with old books spaces are left between the quotation marks - here is a macro that again doesn't work ( i haven't written any code since '85 and it shows) could you again offer any suggestions?

regards


fred


Sub StripSpacesBetweenQuoteMarks
oDoc = ThisComponent
FandR = oDoc.createReplaceDescriptor
FandR.searchRegularExpression = true
FandR.setSearchString("" \<") 'Find one double quotation mark and space at beginning of word.
FandR.setReplaceString(""") 'Replace with one double quotation mark.
oDoc.ReplaceAll(FandR) 'Do it.
FandR.setSearchString("\> "") 'Find space and double quotation mark at end of word.
FandR.setReplaceString(""") 'Replace with one double quotation mark.
oDoc.ReplaceAll(FandR) 'Do it.
End Sub
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


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

PostPosted: Tue Jan 20, 2009 11:54 am    Post subject: Reply with quote

Don't ask me why but it takes 4 double quotes to represent 1 in this situation. Here's your code updated. In the first two modification I have also shown an example of setting up a reference in the search string and then using a back reference in the replace string. This only works in v2.4.1 and above.

Please ask in other such questions in a new thread as these are getting "off topic" for this one.
Code:
Sub StripSpacesBetweenQuoteMarks
oDoc = ThisComponent
FandR = oDoc.createReplaceDescriptor
FandR.searchRegularExpression = true
FandR.setSearchString("("") \<") 'Find one double quotation mark and space at beginning of word.
FandR.setReplaceString("$1") 'Replace with one double quotation mark.
oDoc.ReplaceAll(FandR) 'Do it.
FandR.setSearchString("\> """) 'Find space and double quotation mark at end of word.
FandR.setReplaceString("""") 'Replace with one double quotation mark.
oDoc.ReplaceAll(FandR) 'Do it.
End Sub
Back to top
View user's profile Send private message
ivo9
Newbie
Newbie


Joined: 07 Sep 2012
Posts: 1

PostPosted: Fri Sep 07, 2012 11:06 am    Post subject: Reply with quote

I unfortunately cannot run the macro (i.e. the orgininal long one). It obviously works for multiple people so i might be doing something wrong. I get several errors starting with "compile error: Sub or Function not defined" on the "createUnoService" line. Do you know what might be wrong?

I am running office 2003 if that is in any way relevant.

Thank you.

Ivo
Back to top
View user's profile Send private message
Corfy
Moderator
Moderator


Joined: 14 Jun 2005
Posts: 1117
Location: Near Indianapolis, IN, USA

PostPosted: Fri Sep 07, 2012 11:24 am    Post subject: Reply with quote

ivo9 wrote:
I am running office 2003 if that is in any way relevant.


I assume you mean Microsoft Office 2003? If so, that is your problem in getting the macro to work, as this forum is for users of OpenOffice.org. While OpenOffice.org (as well as its fork, LibreOffice) is a free and powerful office suite with a word processor, spreadsheet, database, graphics design, and presentations capabilities, and while it can open many Microsoft Word, Excel and PowerPoint documents, the macro language is not compatible between Microsoft Office and OpenOffice.org. So to get the macro to work, you will either need to get OpenOffice.org or LibreOffice, or rewrite the macro for Microsoft Office, or try a Microsoft Office forum to see if they have a similar macro.
_________________
Laugh at life or life will laugh at you.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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