[Solved] VBA chdir problem

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
megs.rs
Posts: 5
Joined: Sun Jan 22, 2023 2:39 pm

[Solved] VBA chdir problem

Post by megs.rs »

Hi there!

I hope that anybody is well.

I made a excel worksheet to organize my mame's games.

It's work very well in excel on windows.

But, i was trying to do work on LibreOffice on Fedora and i am having problem.

This is my code:

Code: Select all

        print Worksheets("configuracao").Cells(1, 2)
        ChDir Worksheets("configuracao").Cells(1, 2)
        print curdir()
        Shell "mame.exe " + "roms/" + Application.Selection, vbNormalFocus
This code return error '53', "file not found" i think.

Debugging the code i see that chdir is not working.

Anyone can help me about this?

Thanks a lot,

Miguel Silva
Last edited by Hagar Delest on Tue Jan 24, 2023 10:31 pm, edited 1 time in total.
Reason: tagged solved.
LibreOffice 7.4.3.2 on Fedora Linux 37
User avatar
Hagar Delest
Moderator
Posts: 32653
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: VBA chdir problem

Post by Hagar Delest »

Hi and welcome to the forum!

Basic language in MS Excel and LO are different. You need to rewrite your macro with the matching function in LO (if it exists).
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: VBA chdir problem

Post by Zizi64 »

Please upload your sample file and the full macro code here.
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.
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: VBA chdir problem

Post by JeJe »

Have you tried putting the full file path in you Shell statement?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
megs.rs
Posts: 5
Joined: Sun Jan 22, 2023 2:39 pm

Re: VBA chdir problem

Post by megs.rs »

Hey guys,

I think you know I'm running MAME on WINE. Works fine on my Fedora.

But MAME needs to run in its own directory.

To solve this problem, I made a workaround. I made a shell script that changes to the MAME directory and runs the game in the parameter.

Everything is fine now. But 'chdir' function didn't work for 'shell' function

Here is the complete code:

Code: Select all

Private Sub CommandButton2_Click()
    If Application.Selection.Count = 1 And Application.Selection.Row >= 8 And Application.Selection.Column = 3 Then
        CommandButton2.Visible = False
        rem print Worksheets("configuracao").Cells(1, 2)
        rem ChDir Worksheets("configuracao").Cells(1, 2)
        rem print curdir()
        rem print "mame.exe " + "roms/" + Application.Selection
        Shell "runmame.sh " + "roms/" + Application.Selection, vbNormalFocus
        Worksheets("jogos").Cells(Application.Selection.Row, Application.Selection.Column - 1) = Worksheets("jogos").Cells(Application.Selection.Row, Application.Selection.Column - 1) + 1
        Cells(Application.Selection.Row, 1).Activate
        CommandButton2.Visible = True
    Else
        MsgBox "Selecione o Jogo"
    End If
End Sub
LibreOffice 7.4.3.2 on Fedora Linux 37
Bidouille
Volunteer
Posts: 575
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: VBA chdir problem

Post by Bidouille »

megs.rs wrote: Tue Jan 24, 2023 6:06 pmEverything is fine now. But 'chdir' function didn't work for 'shell' function
Hmm read your code:

Code: Select all

        rem ChDir Worksheets("configuracao").Cells(1, 2)
If you set a rem, it sound logical. :roll:
megs.rs
Posts: 5
Joined: Sun Jan 22, 2023 2:39 pm

Re: [Solved] VBA chdir problem

Post by megs.rs »

I'm not that stupid...
LibreOffice 7.4.3.2 on Fedora Linux 37
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] VBA chdir problem

Post by karolus »

megs.rs wrote: Sat Jan 28, 2023 2:57 pm I'm not that stupid...
…but ignorant enough to use »Application.Selection« 9 times in 15 lines of code!!

Code: Select all

Private Sub CommandButton2_Click()
    sel = Application.Selection
    If sel.Count = 1 And sel.Row >= 8 And sel.Column = 3 Then
        CommandButton2.Visible = False
        rem print Worksheets("configuracao").Cells(1, 2)
        rem ChDir Worksheets("configuracao").Cells(1, 2)
        rem print curdir()
        rem print "mame.exe " + "roms/" + sel
        Shell "runmame.sh " + "roms/" + sel , vbNormalFocus
        Worksheets("jogos").Cells(sel.Row, sel.Column - 1) = Worksheets("jogos").Cells(sel.Row, sel.Column - 1) + 1
        Cells( sel.Row, 1).Activate
        CommandButton2.Visible = True
    Else
        MsgBox "Selecione o Jogo"
    End If
End Sub
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] VBA chdir problem

Post by JeJe »

With/end with can simplify and make code more readable, in case anyone isn't aware of it. Simplifying the last post:

Code: Select all

Private Sub CommandButton2_Click()
	sel =Application.Selection
    with sel
    If .Count = 1 And .Row >= 8 And .Column = 3 Then
        CommandButton2.Visible = False
        rem print Worksheets("configuracao").Cells(1, 2)
        rem ChDir Worksheets("configuracao").Cells(1, 2)
        rem print curdir()
        rem print "mame.exe " + "roms/" + sel
        Shell "runmame.sh " + "roms/" + sel , vbNormalFocus
        Worksheets("jogos").Cells(.Row, .Column - 1) = Worksheets("jogos").Cells(.Row, .Column - 1) + 1
        Cells( .Row, 1).Activate
        CommandButton2.Visible = True
    Else
        MsgBox "Selecione o Jogo"
    End If
    end with
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
megs.rs
Posts: 5
Joined: Sun Jan 22, 2023 2:39 pm

Re: [Solved] VBA chdir problem

Post by megs.rs »

karolus wrote: Sun Jan 29, 2023 2:32 pm
megs.rs wrote: Sat Jan 28, 2023 2:57 pm I'm not that stupid...
…but ignorant enough to use »Application.Selection« 9 times in 15 lines of code!!

Code: Select all

Private Sub CommandButton2_Click()
    sel = Application.Selection
    If sel.Count = 1 And sel.Row >= 8 And sel.Column = 3 Then
        CommandButton2.Visible = False
        rem print Worksheets("configuracao").Cells(1, 2)
        rem ChDir Worksheets("configuracao").Cells(1, 2)
        rem print curdir()
        rem print "mame.exe " + "roms/" + sel
        Shell "runmame.sh " + "roms/" + sel , vbNormalFocus
        Worksheets("jogos").Cells(sel.Row, sel.Column - 1) = Worksheets("jogos").Cells(sel.Row, sel.Column - 1) + 1
        Cells( sel.Row, 1).Activate
        CommandButton2.Visible = True
    Else
        MsgBox "Selecione o Jogo"
    End If
End Sub
I really don't know what your point is in offending me.
I have 30 years of programming and I use comments to leave codes that I don't use anymore, but I might want to go back to using them saved.
Please, if it's to disturb, don't comment anything else!
LibreOffice 7.4.3.2 on Fedora Linux 37
megs.rs
Posts: 5
Joined: Sun Jan 22, 2023 2:39 pm

Re: [Solved] VBA chdir problem

Post by megs.rs »

JeJe wrote: Sun Jan 29, 2023 5:07 pm With/end with can simplify and make code more readable, in case anyone isn't aware of it. Simplifying the last post:

Code: Select all

Private Sub CommandButton2_Click()
	sel =Application.Selection
    with sel
    If .Count = 1 And .Row >= 8 And .Column = 3 Then
        CommandButton2.Visible = False
        rem print Worksheets("configuracao").Cells(1, 2)
        rem ChDir Worksheets("configuracao").Cells(1, 2)
        rem print curdir()
        rem print "mame.exe " + "roms/" + sel
        Shell "runmame.sh " + "roms/" + sel , vbNormalFocus
        Worksheets("jogos").Cells(.Row, .Column - 1) = Worksheets("jogos").Cells(.Row, .Column - 1) + 1
        Cells( .Row, 1).Activate
        CommandButton2.Visible = True
    Else
        MsgBox "Selecione o Jogo"
    End If
    end with
End Sub
My goal here was not to make pretty code. And yes, it works.
I think you are getting hung up on insignificant details rather than paying attention that 'chdir' doesn't work properly.
LibreOffice 7.4.3.2 on Fedora Linux 37
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] VBA chdir problem

Post by RoryOF »

Sometimes "insignificant details" can mask an underlying error.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] VBA chdir problem

Post by JeJe »

megs.rs wrote: Sun Jan 29, 2023 6:11 pm My goal here was not to make pretty code. And yes, it works.
I think you are getting hung up on insignificant details rather than paying attention that 'chdir' doesn't work properly.
I didn't test it - I'm just showing the use of with/end with - as it looked like someone might not know it and its been very useful to me knowing it.

It can be not worth the time rewriting or optimizing code sometimes and other times it is.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply