[Solved] Convert code created in MS Office to work on AOO

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
KingTamo
Posts: 4
Joined: Sun Nov 19, 2017 7:49 am

[Solved] Convert code created in MS Office to work on AOO

Post by KingTamo »

Hello everyone

I am totally new in this forum and I have a problem ..
I have created a VBA code for MS Excel and when I send it to a friend he told me that he is using OpenOffice
This is the code

Code: Select all

Sub Test()
    Dim fi As Integer
    Dim fo As Integer
    Dim si As String
    Dim ai() As String
    Dim i As Long
    fi = FreeFile
    Open "Sample.txt" For Input As #fi
    fo = FreeFile
    Open "Output.txt" For Output As #fo
    Do While Not EOF(fi)
        Line Input #fi, si
        ai = Split(si)
        If UBound(ai) Mod 2 = 0 Then
            ReDim Preserve ai(UBound(ai) + 1)
        End If
        For i = 0 To UBound(ai) Step 2
            Print #fo, ai(i) & " " & ai(i + 1)
        Next i
    Loop
    Close #fi
    Close #fo
End Sub
It creates a text file and split the content of the input text file to each two words in line and the results would be put in another new text file named "Output"

Is it possible to convert it to work in OpenOffice?
If possible please give me the steps in details

Thanks advanced for help
Last edited by Hagar Delest on Fri Nov 24, 2017 9:21 am, edited 1 time in total.
Reason: tagged solved.
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert code created in MS office to work on OpenOffice

Post by Zizi64 »

Is it possible to convert it to work in OpenOffice?

Yes, it possible to convert your code. You need manually rework it. (You must rewrite your code based on the API functions of the Apache OpenOffice or the LibreOffice. you can use one of supported programming languages of the AOO or LO, for example the embedded StarBasic. API: Application Programming Interface.)

In other word:
You and your friend can try your code in LibreOffice: it has higher compatibility with the foreign file formats and with tho MS VBA. Maybe it will work in LO without any changing, or maybe you must insert only some extra lines at the begin of the code. See the Options in these topics:
viewtopic.php?f=20&t=56522
viewtopic.php?f=45&t=89411
Last edited by Zizi64 on Sun Nov 19, 2017 9:29 am, edited 3 times in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Convert code created in MS office to work on OpenOffice

Post by Zizi64 »

If possible please give me the steps in details
1.: Download, read and study Andrew Pitonyak's free macro books.
2.: Study the desctiptions of the API.
3.: Search, study and try it the attached sample codes of the macro related topics in this Forum.
4.: Then (maybe) you will able to rewrite your code for Apache OpenOffice or LibreOffice.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
KingTamo
Posts: 4
Joined: Sun Nov 19, 2017 7:49 am

Re: Convert code created in MS office to work on OpenOffice

Post by KingTamo »

Thanks a lot for reply
Forgive me as I am totally newbie

I have enabled macros in OpenOffice and when trying to execute I encountered red error part for the string in this line

Code: Select all

Open "YourTextFile.txt" For Input As #fi

How can I put the quotation for strings?
OpenOffice 3.1 on Windows Vista
KingTamo
Posts: 4
Joined: Sun Nov 19, 2017 7:49 am

Re: Convert code created in MS office to work on OpenOffice

Post by KingTamo »

After many tries I could figure it out (It seems that I have to write the full path)

Code: Select all

Option VBASupport 1

Sub Split_My_Text_File()
    Dim si          As String
    Dim ai()        As String
    Dim fi          As Integer
    Dim fo          As Integer
    Dim i           As Long
	
	DummyTxt=thisworkbook.path & "\YourTextFile.txt"
	
    fi = FreeFile
    Open DummyTxt For Input as #fi
    fo = FreeFile
    Open thisworkbook.path & "\Output.txt" For Output As #fo

    Do While Not EOF(fi)
        Line Input #fi, si
        ai = Split(si)
        If UBound(ai) Mod 2 = 0 Then
            ReDim Preserve ai(UBound(ai) + 1)
        End If
        For i = 0 To UBound(ai) Step 2
            Print #fo, ai(i) & " " & ai(i + 1)
        Next i
    Loop

    Close #fi
    Close #fo
    
    MsgBox "Done", 64
End Sub
OpenOffice 3.1 on Windows Vista
Post Reply