AutoCorrect import macro? 2008 version

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
rmdemi
Posts: 10
Joined: Mon Jun 30, 2008 9:45 pm

AutoCorrect import macro? 2008 version

Post by rmdemi »

Orijinal topic is here; http://www.oooforum.org/forum/viewtopic ... highlight= code created by patb and Zarius Thanks :)

At this point we need, adress http://word.mvps.org/FAQs/Customization ... orrect.htm instructions from this site download Autocorrect.zip, with autocorrect.dot create word backup copy autocorrect list document.

autocorr.odt
writer auto correct import macro document
(18.89 KiB) Downloaded 1353 times

I tested in Pardus(Linux), WindowsXP OOo2.4.X
With this file click the import button with macro opens, file open dialog select word backup document . Done. File has using instructions in English and Turkish.

I made some code modifications;
* Calc section, cut, copy, paste sections canceled. No more file operations.
* Now detectlocale function converts two byte locale strings to five bytes correct forms. Such as fr, tr to fr-FR, tr-TR
* File picker dialog added for selection backup word file
* If user hasn't acor_xx-xx.dat in user path macro copies it from OOo share path.
* İf writer has same autocorrect entries like word These entries doesn't insert to writer.

Here is the whole code;

Code: Select all

REM  *****  BASIC  *****
global gWriterDoc as object
global autoCorrDbDatFileWithPath as string
global autoCorrDbDatFile
global autoCorrFileXmlWithPath as string
global ilke as variant

sub ImportACLFromWord
GlobalScope.BasicLibraries.LoadLibrary("Tools")
   ' ImportACLfromWord - 23/06/2006 by Pat B
   ' Modifications 25/06/2006 by Zarius (refactored & generalised)
   ' Modifications 29/06/2008 by Ramdem
     'I made some code modifications;
     '* Calc section, cut, copy, paste sections canceled. No more file operations.
     '* Now detectlocale function converts two byte locale strings to five bytes correct forms. Such as fr, tr to fr-FR, tr-TR 
     '* File picker dialog added for selection backup word file
     '* If user hasn't acor_xx-xx.dat in user path macro copies it from OOo share path.

' ** Back up your //.openoffice.org2/user/autocorr/acor_xx-xx.dat before you start. 

   ' Use MS Word to back up your Word autocorrect list using the macro at
   '   http://word.mvps.org/FAQs/Customization/ExportAutocorrect.htm
   ' Then open that document in Writer, run this macro then restart OpenOffice.

   ' Before running, uncomment the appropriate version below and
   ' replace the locale (OO1.x: 1033, OO2.x: en-AU) with your locale.
   ' New: the macro should automatically detect the locale on OO2.x

   ' **********************************************************************
   ' ******************** EDIT ME *****************************************
   ' SO7/OO1.x
   'locale = "1033" ' SO7, OO1.x - other locales have different numbers (eg 1031)
   'autoCorrDbDatFile = "acor"+locale+".dat" ' this probably wont change   

   ' OO2.x
   'locale = "tr-TR"    en-AU = Australian English (change to your own locale)
    locale = detectLocale()
    autoCorrDbDatFile = "acor_"+locale+".dat" ' this probably wont change
   ' ******************** END: EDIT ME ************************************
   ' **********************************************************************

   ' The macro selects the unformatted entries (ie RTF='False' elements) in MS
   '   Word's AutoCorrect Backup Document and transposes these to a text document
   '   called DocumentList.xml which is then zipped into
   '   //.openoffice.org2/user/autocorr/acor_en-AU.dat.

   ' It does not transpose formatted entries.  I suggest you do this manually.

   ' Good luck!  No Warranty!

   dim msg as string

   sotoduzeltyolu = ConvertToURL(PickFileName())
   gWriterDoc = StarDesktop.loadComponentFromURL( sotoduzeltyolu , "_blank", 0, Array() )  
   autoCorrFileXml = "DocumentList.xml"     ' this probably wont change

   ' setup the path and filenames for the AutoCorrect database and the xml file
   oPaths = CreateUnoService( "com.sun.star.util.PathSettings" )
   toplamstring = oPaths.AutoCorrect 
   ilke() = split(toplamstring,";") 'find autocorrect paths first share and second user

   sofficeConfigPath = ConvertFromURL(ilke(1)) ' strip the file:/// from the front
   autoCorrDbDatFileWithPath = ilke(1) + "/" + autoCorrDbDatFile
   autoCorrFileXmlWithPath   = ilke(1) + "/" + autoCorrFileXml

   ' ---------------- writer section
   ' get the rows and columns of the table, 
   oTables = gWriterDoc.getTextTables()
   oTable = oTables.getByIndex(0) 'Select first table(named Table1,Tabelle1 etc)
   oRows = oTable.getRows()
   oColumns = oTable.getColumns()
   if  not (oColumns.Count= 3) then
      msg = "You don't seem to be in a table.                    " & _
         chr(10) & "The open document should be the AutoCorrect Backup Document " & _
         chr(10) & "      created by MS Word" & _
         chr(10) & "The macro may need tweaking." & _
         chr(10) & "Aborting......"
      msgbox ( msg, 0, "ERROR!")
      exit sub
   endif
' replace special characters with their appropriate xml representations 
   searchArray =  array(chr(38),  chr(34), chr(60), chr(62), chr(39), "==>")
   replaceArray = array("&", """, "<" , ">" , "&apos;", "'==>")
   searchAndReplaceArrays(gWriterDoc, searchArray(), replaceArray())
' if not exists acor_xx-xx.dat copy it from share path to user path
   oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
   if (not(oUcb.Exists(autoCorrDbDatFileWithPath))) then
      kaynak=ilke(0)+"/"+ autoCorrDbDatFile
      hedef=ilke(1) +"/"+ autoCorrDbDatFile
      oUcb.copy( kaynak , hedef )
   endif
' unzip the document.xml out of the auto correct db file 
   unzipAutoCorrDB()
' open DocumentList.xml with writer
   Dim oMediaDescriptor(1) as new com.sun.star.beans.PropertyValue
	oMediaDescriptor(0).name = "FilterName"
	oMediaDescriptor(0).value = "Text (encoded)"
	oMediaDescriptor(1).name = "FilterOptions"
	oMediaDescriptor(1).value = "UTF8"

   oOoAcWriterDoc = StarDesktop.loadComponentFromURL( autoCorrFileXmlWithPath, "_blank", 0, oMediaDescriptor() )
' sdesc for discover writer has same autocorrect entry with word
sdesc=oOoAcWriterDoc.createSearchDescriptor
' rdesc for replacing writer entries with word entries
rdesc=oOoAcWriterDoc.createReplaceDescriptor
rdesc.SearchRegularExpression=False
s1=" <block-list:block block-list:abbreviated-name="""
s2=""" block-list:name="""
s3="""/>"
s4="</block-list:block-list>"
' if gui is windows add chr$(10) to string
if getguitype() = 1 then s4=s4+CHR$(10)

   ' loop through all rows and writer has no entries like word add this entriesr
   ' (note: a for loop seems easier, but didn't work well due to a shrinking row count)
   rowCount = oRows.Count
   p = 1
   while (p < rowCount)
      cellName = "C"+trim(str(p+1))
      cellLeftVal = oTable.getCellByName("A"+trim(str(p+1))).String
      if(oTable.getCellByName(cellName).String = "True") then goto nothing 'ignore row
sdesc.Searchstring=cellLeftVal
if Not IsNull (oOoAcWriterDoc.FindFirst(sdesc))then goto nothing ' same entry ignore row
' yes we can insert this entry row
cellLeftVal2 = oTable.getCellByName("B"+trim(str(p+1))).String
rdesc.Searchstring=s4
rdesc.Replacestring=s1++cellLeftVal+s2+cellLeftVal2+s3+CHR$(10)+s4
oOoAcWriterDoc.ReplaceAll(rdesc)

nothing:
      p = p+1
   wend

   oOoAcWriterDoc.Store()      ' save documentlist.xml
   oOoAcWriterDoc.close(true)  'close it
   zipAutoCorrDB()   'Save Documentlist.xml to acor_XX-xx.dat.
   gWriterDoc.close(true)
if locale="tr-TR" then
   msgbox "Otodüzeltme kayıtlarınız Openoffice'e başarıyla alındı.  Değişiklikleri görmek için OOo'yu yeniden başlatın.", 0, "Başarıldı."
else
   msgbox "Successfully merged the word ac list with OpenOffice.  You will need to restart OpenOffice to see the changes.", 0, "Success."
End if
end sub

' This function will search through the document and replace anything in the
'   document that matches entries in the searchArray with the relevant
'   entry in the replaceArray.
function searchAndReplaceArrays(oDocument, sArray, rArray)
   dim oReplace as object
   oReplace = oDocument.createReplaceDescriptor()
   oReplace.SearchCaseSensitive = True
   For p = LBound(sArray()) To UBound(sArray())
      oReplace.SearchString = sArray(p)
      oReplace.ReplaceString = rArray(p)
      oDocument.ReplaceAll(oReplace)
   Next p
end function

' thanks to al_andreas for the original zip and unzip autocorrdb functions
sub zipAutoCorrDB()
   dim zipService as variant
   dim filestreamService as variant
   dim inputStream as variant
   dim theZipper as variant
   dim outputStream as variant
   
   Dim args1(0)

   args1(0) = autoCorrDbDatFileWithPath

   filestreamService = createUnoService("com.sun.star.ucb.SimpleFileAccess")
   inputStream = FilestreamService.OpenFileRead(autoCorrFileXmlWithPath)

   zipService = createUnoService("com.sun.star.packages.Package")
   zipService.initialize(args1())

   theZipper=zipService.createInstance()
   theZipper.SetInputStream(inputStream)

   autoCorrFileXml = FileNameoutofPath(autoCorrFileXmlWithPath)
   outputStream=zipService.getByHierarchicalName("")

   outputStream.replaceByName(autoCorrFileXml, theZipper)

   zipService.commitChanges()

   kill autoCorrFileXmlWithPath
end sub

' thanks to al_andreas for the original zip and unzip autocorrdb functions
sub unzipAutoCorrDB()   
   dim zipService as variant
   dim filestreamService as variant
   dim inputStream as variant
   dim theZipper as variant
   dim outputStream as variant
   dim autoCorrFileXml as string
   dim args1(0)

   args1(0) = autoCorrDbDatFileWithPath
   
   zipService = createUnoService("com.sun.star.packages.Package")
   zipService.initialize(args1())
   
   autoCorrFileXml = FileNameoutofPath(autoCorrFileXmlWithPath)
   theZipper = ZipService.getByHierarchicalName(autoCorrFileXml)
   inputStream = TheZipper.getInputStream()
   
   outputStream = createUnoService("com.sun.star.ucb.SimpleFileAccess")
   outputStream.WriteFile(autoCorrFileXmlWithPath, inputStream)
End Sub

function detectLocale() as string 
Dim OOLangue as string
Dim aSettings, aConfigProvider
Dim aParams2(0) As new com.sun.star.beans.PropertyValue
aConfigProvider = createUnoService( _
"com.sun.star.configuration.ConfigurationProvider" )
aParams2(0).Name = "nodepath"
aParams2(0).Value = "/org.openoffice.Setup/L10N"
aSettings = _
aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aParams2() )
OOLangue= aSettings.getbyname("ooLocale")
' if only two bytes long convert to xx-XX form such tr-TR, fr-FR
if len (OOLangue) = 2 then OOlangue = OOlangue +"-"+Ucase(OOlangue)
   detectLocale() = OOLangue   
end function 

Function PickFileName()
   oFilePickerDlg = createUnoService( "com.sun.star.ui.dialogs.FilePicker" )
   oOfficeFilePickerDlg = createUnoService( "com.sun.star.ui.dialogs.OfficeFilePicker" )
   ' Unnecessary, the dialog defaults to having multi-select turned off.   
'   oFilePickerDlg.setMultiSelectionMode( False )
 if not isnull ( oFilePickerDlg) and  getguitype() = 1 then
   oFilePickerDlg.execute()
   oFiles = oFilePickerDlg.getFiles()
   else
   oOfficeFilePickerDlg.execute()
   oFiles = oOfficeFilePickerDlg.getFiles()
End if      
   ' Get an array of the files that the user picked.
   ' There is only one item in the array since we did not
   '  turn on the Multi-Selection feature of the dialog box.
   If UBound( oFiles ) - LBound( oFiles ) + 1 > 0 Then
      PickFileName() = ConvertFromURL( oFiles(0) )
   Else
      PickFileName() = ""
   EndIf   
End Function 
Sincerely, :)
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

It doesn't work for me. It goes through all the steps, and reports success, but there is no change to my list of autocorrect entries.

Thank you, nonetheless, for the effort. I feel we are 90% of the way there. Possibly I am just missing a step. But it looks like it is all working as intended.
rmdemi
Posts: 10
Joined: Mon Jun 30, 2008 9:45 pm

Re: AutoCorrect import macro? 2008 version

Post by rmdemi »

Hello Raywood,

In linux cr and cr-lf is a problem for this macro, now I modified macro try this version please. I tested it with kubuntu livecd KDE 4.0 and OOo 2.3 .
autocorr v1.odt
newer version
(17.7 KiB) Downloaded 1420 times
If you have problem with this file, also you can mail to me. :D

Note: İn linux macro must create /home/user name/openoffice.org2/user/autocorr/acor_xx-XX.dat file. If you open documentlist.xml you can see chances.
OOo 2.4.X on Ms Windows XP + linux-other
march
Posts: 1
Joined: Sat Aug 02, 2008 4:10 pm

Re: AutoCorrect import macro? 2008 version

Post by march »

Re: AutoCorrect import macro? 2008 version
and
autocorr v1.odt
(newer version)
Post by rmdemi on Wed Jul 30, 2008 11:43 am

I tried both of these macros & neither one worked for me operating Vista (ughh!). As in the above post, the macro ran fine & said my autocorrect file had been imported but when I restarted OO the autocorrect files had not been changed. At least this macro runs on my machine though. Is there a similar macro for importing the MS Word custom dictionary into OO?

Thanks
OOo 2.4.X on MS Windows Vista + None
rmdemi
Posts: 10
Joined: Mon Jun 30, 2008 9:45 pm

Re: AutoCorrect import macro? 2008 version

Post by rmdemi »

Hello March,

I think vista and OOo are buggy.

Please let me know:

In OOo Tools>Options menu way in Paths --Autocorrect tree way.. What is the your user path? ( It is per user operation )
after macro process there do you have acorr_xx-XX.dat. ?
is Installed Openoffice local country same your Vista local? and what is your local country?

If you know about macro ide:
Plase open module1 insert a code breakpoint to the line 134 after then run the macro.
When macro stopped at this line, you can see documentlist.xml in writer. Check file inserted lines from word"s autocorrect backup list.

Cheers..
OOo 2.4.X on Ms Windows XP + linux-other
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

I'm using WinXP and again I got the same result as March. The macro seems to be going through all the steps, but my autocorrect entries are not added to those in OO Writer.

The path in OO Writer is /home/ray/.openoffice.org2/user/autocorr. In that folder, I have a ZIP file named acor_en-US.dat.

In OOW > Tools > Macros > Organize Macros > OpenOffice.org Basic > Module1 > Edit, I have just this:

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

Sub Main

End Sub

That's all. Instead of OOo Basic, I also have Python, BeanShell, and JavaScript, but those all seem to require a Java runtime environment that apparently I have not installed.
OOo 2.4.X on Ubuntu 8.x
User avatar
Terrae
Posts: 9
Joined: Fri Aug 15, 2008 9:53 pm
Location: United States

Re: AutoCorrect import macro? 2008 version

Post by Terrae »

I posted this under another topic, after I was directed to this one about the AutoCorrect import macro. I used the macro and below is what happened after I used it. Villeroy asked that I repost my comment with regard to the macro here.

-------------------- Original Post ---------------
Interesting... it said it worked, but I did not see any of my own custom entries, my Language is now greyed out, and the word completion tab is now empty... ?? What happened? Any idea? :?:
Love all, trust few, do harm to none.
OOo 2.4.X on Ms Windows XP
rmdemi
Posts: 10
Joined: Mon Jun 30, 2008 9:45 pm

Re: AutoCorrect import macro? 2008 version

Post by rmdemi »

Hello terrae,

I'm so sorry to hear that.. :oops: You can directly delete C:\Documents and Settings\Yourname\Application Data\OpenOffice.org2\user\autocorr\acor_xx-XX.dat . So OOo continues using acor_xx-XX.dat in share directory. If your user path different from this way. You can see your user autotext path İn OOo Tools>Options>Openoffice.org Autotext view.

For debugging purpose send to me word auto correct file and your user autocorrect dat file. I can create imported autcorrect dat file for you.

Dear Raywood Is acor_en-US.dat file is correct for you? ooLocale means Openoffice local. not user locale. Check Openoffice installation path/share autocorrect/acor_en-US.dat file size with your user dat's file size. Size is bigger I think. Original file size is 15588 bytes
Basic editor menu
Basic editor menu
You select Autocor v1 module for edit. Line 134 is oOoAcWriterDoc.Store() ' save documentlist.xml.

If you open acor_en-Us.dat with a zip editor. In documentList.xml You can see entries.
OOo 2.4.X on Ms Windows XP + linux-other
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: AutoCorrect import macro? 2008 version

Post by Tommy »

Hi Guys,
are you aware of the "OOo autocorrect entries limit bug"?

http://www.openoffice.org/issues/show_bug.cgi?id=87672

if you add more than 65535 entries in a single acor.dat file, OOo crashes and looses allthe stored autocorrect entries.

be aware not to pass that limit when importing long list of autocorrection...
despite being a P2 issue from 1 year and an half ago it hasn't been fixed yet
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

Hi, all -- another year goes by -- any progress on this? I'd really like to start using OO Writer, but I still don't know how to import my list of word definitions. I don't have 65,000 entries, so that limit is not a problem, but unfortunately the previous instructions didn't work for me.
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

Bump.
OOo 2.4.X on Ubuntu 8.x
User avatar
floris v
Volunteer
Posts: 4422
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: AutoCorrect import macro? 2008 version

Post by floris v »

Instead of OOo Basic, I also have Python, BeanShell, and JavaScript, but those all seem to require a Java runtime environment that apparently I have not installed.
What stops you from installing it?
OpenOffice 4.1.11 on Ubuntu; LibreOffice 6.4 on Linux Mint, LibreOffice 7.6.2.1 on Ubuntu
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

Floris -- at the time (two years ago), I think what prevented me from installing it was knowledge on how to do so. But anyway, in the last few days, I have worked up a solution. See http://raywoodcockslatest.blogspot.com/ ... rrect.html
OOo 2.4.X on Ubuntu 8.x
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: AutoCorrect import macro? 2008 version

Post by Tommy »

@raywood

I've read your blog post. you did a good job.

so basically you obtained the autocorrect strings with Calc and then you pasted them into a .txt file and finally pasted in inside the documentlist.xml file.

my question is: do you know if there's a method to import those autocorrect strings inside the documentlist.xml file without opening it?

the problem is that when you have already many entries in the .xml file , opening it may take some time.

you have to decompress the acor.dat file, then locate the xml file, open it in a text editor, then paste the code from the txt file, save the xml and recompress the dat file.

maybe a macro could import the strings directly into the xml file avoiding this multistep procedure

is it possible?
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
raywood
Posts: 6
Joined: Wed Jul 30, 2008 1:01 am

Re: AutoCorrect import macro? 2008 version

Post by raywood »

Good question. I dunno.
OOo 2.4.X on Ubuntu 8.x
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: AutoCorrect import macro? 2008 version

Post by Tommy »

ok.

by the way here's a Calc compliant formula similar to those you used in Excel to assemble the autocorrect xml strings

="<block-list:block block-list:abbreviated-name="&CODICE.CARATT(34)&A4&CODICE.CARATT(34)&"block-list:name="&CODICE.CARATT(34)&B4&CODICE.CARATT(34)&"/>"
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: AutoCorrect import macro? 2008 version

Post by Tommy »

@ rmdemi

do you think that your macro may be used to import a list of autocorrect entries which has been made on a simple 2 columns Calc sheet?

I mean:

cell A1: y3llow cell B1: yellow
cell A2: uellow cell B2: yellow
cell A3: yelloq cell B3: yellow
etc. etc.

see also these topic:
Adding list in Calc to autocorrect
http://user.services.openoffice.org/en/ ... =egosearch
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: AutoCorrect import macro? 2008 version

Post by Tommy »

and what about exporting OOo autocorrect list into MS Word?

can your macro be modified to run backward?
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
Post Reply