| View previous topic :: View next topic |
| Author |
Message |
jelaih OOo Enthusiast


Joined: 23 Feb 2005 Posts: 152
|
Posted: Tue Mar 07, 2006 7:16 pm Post subject: all about formatting |
|
|
How do i preserve and retain the format/emphasis of a certain word if i use the search and replace descriptors?
I have found the following code from http://homepages.paradise.net.nz/hillview/OOo/, but the formatting disappears after the replace. The first column is the word to be searched and the 2 column is the word to be replaced. It is not working for formatted text.
| Code: |
sub FindReplaceOccurences
Dim i as long
dim oDoc as object
dim oReplace as object
dim sFilename as string
dim oFolderPickerDlg as object
dim oUcbObject as object
dim Dummy()
'Load mFinds and mReplaces arrays with strings from table
oDoc = thisComponent 'StarDesktop.CurrentComponent
oVCurs = ThisComponent.getCurrentController().getViewCursor()
oTable = oDoc.getTextTables.getByIndex(0)
nReplacements = oTable.getRows.count -1
dim mFinds(nReplacements), mReplaces(nReplacements)
for i = 0 to nReplacements
mFinds(i) = oTable.getCellByPosition(0,i).getstring()
mReplaces(i) = oTable.getCellByPosition(1,i).getstring()
next
'Select folder
oFolderPickerDlg = createUnoService("com.sun.star.ui.dialogs.FolderPicker")
oFolderPickerDlg.setTitle("Replace occurences folder")
oFolderPickerDlg.setDescription("Select folder of files to perform replace operations on:")
oFolderPickerDlg.setDisplayDirectory(ConvertToURL(CurDir))
oFolderPickerDlg.execute()
sPickedFolder = oFolderPickerDlg.getDirectory()'.getDisplayDirectory()
'chdir sPickedFolder
oUcbObject = createUnoService("com.sun.star.ucb.SimpleFileAccess")
'Loop through files in folder performing the replacements
If oUcbObject.Exists(sPickedFolder) Then
mDirContent() = oUcbObject.GetFolderContents(sPickedFolder,True)
for i = 0 to ubound(mDirContent)
sFileName = mDirContent(i)
If not oUcbObject.IsFolder(sFileName) Then
oDoc = StarDesktop.loadComponentFromURL(convertToUrl(sFileName), "_blank", 0, Dummy())
oReplace = oDoc.createReplaceDescriptor
oReplace.SearchCaseSensitive = true
For j = 1 To nReplacements
oReplace.SearchString = mFinds(j)
oReplace.ReplaceString = mReplaces(j)
oDoc.replaceAll(oReplace)
Next
oDoc.store
oDoc.close(false)
end if
next
end if
end sub
|
_________________ codebits...  |
|
| Back to top |
|
 |
probe1 Moderator


Joined: 18 Aug 2004 Posts: 2465 Location: Chonburi Thailand Asia
|
Posted: Mon Mar 20, 2006 11:13 am Post subject: |
|
|
Sorry - did test this code without having a user-defined (hard-formatted) changed.
OOo 2.0.2-5 on Linux 9.3, *.odt test file
Which version/platform you are on?
Is your table text formatted differently to the text to be replaces?
Did you apply a separate character style on the replace parts? _________________ Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs |
|
| Back to top |
|
 |
jelaih OOo Enthusiast


Joined: 23 Feb 2005 Posts: 152
|
Posted: Mon Mar 20, 2006 7:34 pm Post subject: |
|
|
OOo 2.0/ WinXP
| Quote: |
Is your table text formatted differently to the text to be replaces?
|
Yes, sometimes same format. What matters is that the 2nd column must retain its format when replacing its countepart on column 1.
| Quote: |
Did you apply a separate character style on the replace parts?
|
Yes, It's like I want to find all occurrence of the word "bon jour" on a certain doucment with/without emphasis, Then replace it with italic "bon jour" or whatever emphasis I have placed on the 2nd column. _________________ codebits...  |
|
| Back to top |
|
 |
probe1 Moderator


Joined: 18 Aug 2004 Posts: 2465 Location: Chonburi Thailand Asia
|
Posted: Tue Mar 21, 2006 4:44 am Post subject: |
|
|
OK, now I do understand your question.
This should be possible - but I have no idea where to start programming.
I've tried recording a replace (with setting a different format on the places found), but this didn't help me to get a hint which properties to set.
Maybe you can record a find/replace+format set on your own and have a look at the generated code....
Good luck _________________ Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs |
|
| Back to top |
|
 |
|