Donate to Remove ads

Got a credit card? use our Credit Card & Finance Calculators

Thanks to Rhyd6,eyeball08,Wondergirly,bofh,johnstevens77, for Donating to support the site

Visual Basic script

Seek assistance with all types of tech. - computer, phone, TV, heating controls etc.
Nocton
Lemon Slice
Posts: 491
Joined: November 6th, 2016, 11:25 am
Has thanked: 134 times
Been thanked: 138 times

Visual Basic script

#213509

Postby Nocton » April 8th, 2019, 9:23 am

I have a vbs file to copy a spreadsheet (.xls) file from Dropbox and then run it. All works well, but as one of the users does not have Excel, but uses OpenOffice Calc, I want to make sure that on all users PCs the file opens in Calc.
The lines:

Code: Select all

iRC = oShell.Run("""" & sFileName & """", 1, True)

Code: Select all

iRC = oShell.Run("""" & "C:\Program Files (x86)\OpenOffice 4\program\scalc.exe" & """", 1, True)

work OK, where sFilename is the xls file I wish to open.

However, I have not been able to find a way to combine them so that Calc opens and then loads sFileName

Does anyone have any suggestions?

AllYourBase
Posts: 35
Joined: November 6th, 2016, 5:05 pm
Has thanked: 7 times
Been thanked: 12 times

Re: Visual Basic script

#213532

Postby AllYourBase » April 8th, 2019, 10:23 am

Code: Select all

iRC = oShell.Run("""" & sFileName & """", 1, True)


Surely the above will open the file using whatever program is associated with .xls files on the target PC? You shouldn't need to change anything.

Itsallaguess
Lemon Half
Posts: 9129
Joined: November 4th, 2016, 1:16 pm
Has thanked: 4140 times
Been thanked: 10032 times

Re: Visual Basic script

#213534

Postby Itsallaguess » April 8th, 2019, 10:26 am

AllYourBase wrote:

Code: Select all

iRC = oShell.Run("""" & sFileName & """", 1, True)


Surely the above will open the file using whatever program is associated with .xls files on the target PC? You shouldn't need to change anything.


Yes, it will, but that's the problem...

Some people have Excel installed, but the OP wants a script that will always open the spreadsheet in OpenOffice Calc, even where Excel might be the associated program to .xls files...

The Excel users, presumably, also have OpenOffice Calc installed too and would like to maintain the normal association with .xls files in normal operation...

Cheers,

Itsallaguess

Nocton
Lemon Slice
Posts: 491
Joined: November 6th, 2016, 11:25 am
Has thanked: 134 times
Been thanked: 138 times

Re: Visual Basic script

#213538

Postby Nocton » April 8th, 2019, 10:36 am

Exactly right, Itsallaguess.
Calc's default is the Open Document Format .ods, but it can read and write to Microsoft's proprietary .xls format and that is the format the file to be opened is saved as. Normally there is no problem and now the later versions of Excel can read and write in ODF format, but occasionally with fancy formatting there are subtle changes when opened by different programs so I'd like to keep the Calc format, especially as the main editor of the file uses only Calc.

Infrasonic
Lemon Quarter
Posts: 4487
Joined: November 4th, 2016, 2:25 pm
Has thanked: 648 times
Been thanked: 1264 times

Re: Visual Basic script

#213557

Postby Infrasonic » April 8th, 2019, 11:07 am

Not the answer you're looking for but could you not save the spreadsheet in both .xls and open document formats (.ods .fods) so that the OO user(s) can file associate that way?

If there's no macros involved then using the free MS Office Online to open .xls from Dropbox would also work if it's a predominantly read only scenario.

scotia
Lemon Quarter
Posts: 3566
Joined: November 4th, 2016, 8:43 pm
Has thanked: 2376 times
Been thanked: 1947 times

Re: Visual Basic script

#213581

Postby scotia » April 8th, 2019, 12:04 pm

How about using the FileExists Function to determine if OpenOffice scalc is present, and if so then use the version that specifies Openoffice scalc, otherwise default to the other version which will normally go for Excel. There are numerous examples on the web - look for VBScript FileExists

Nocton
Lemon Slice
Posts: 491
Joined: November 6th, 2016, 11:25 am
Has thanked: 134 times
Been thanked: 138 times

Re: Visual Basic script

#213583

Postby Nocton » April 8th, 2019, 12:12 pm

I do not want to save in two different formats as then if one file in one format is changed, the other will not be changed. Since the Shell command can open Calc or the file in separate statements, it must be possible to specify Calc and the file to be opened in one statement.

ReformedCharacter
Lemon Quarter
Posts: 3140
Joined: November 4th, 2016, 11:12 am
Has thanked: 3640 times
Been thanked: 1521 times

Re: Visual Basic script

#213596

Postby ReformedCharacter » April 8th, 2019, 12:41 pm

This works for me:

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:\Program Files (x86)\Microsoft Office\root\Office16\excel.exe """) & "c:\yourfolder\yourfile"
Set objShell = Nothing

I don't have Calc installed but I think the syntax is good.

A batch file might be easier :)

RC

kiloran
Lemon Quarter
Posts: 4112
Joined: November 4th, 2016, 9:24 am
Has thanked: 3250 times
Been thanked: 2855 times

Re: Visual Basic script

#213597

Postby kiloran » April 8th, 2019, 12:42 pm

Haven't tried VB for this, but a command line:
"C:\Program Files\LibreOffice\program\scalc.exe" -o "C:\Users\User\Downloads\testfile.xlsx"

may help you. This is for LibreOffice but I'm sure the OpenOffice equivalent will be OK

--kiloran

swill453
Lemon Half
Posts: 7985
Joined: November 4th, 2016, 6:11 pm
Has thanked: 987 times
Been thanked: 3656 times

Re: Visual Basic script

#213599

Postby swill453 » April 8th, 2019, 12:47 pm

If the main editor uses Calc and all the other users also have Calc installed and you want to force them to open it in Calc, then why on earth don't you just save and distribute it in .ods format?

You may have a reason for saving it in .xls format, but you haven't given it here.

Scott.

StepOne
Lemon Slice
Posts: 668
Joined: November 4th, 2016, 9:17 am
Has thanked: 195 times
Been thanked: 185 times

Re: Visual Basic script

#213608

Postby StepOne » April 8th, 2019, 1:27 pm

swill453 wrote:If the main editor uses Calc and all the other users also have Calc installed and you want to force them to open it in Calc, then why on earth don't you just save and distribute it in .ods format?

You may have a reason for saving it in .xls format, but you haven't given it here.

Scott.


I'm not the OP but I'm guessing the spreadsheet comes in from an external source as .xls

Nocton
Lemon Slice
Posts: 491
Joined: November 6th, 2016, 11:25 am
Has thanked: 134 times
Been thanked: 138 times

Re: Visual Basic script

#213625

Postby Nocton » April 8th, 2019, 2:06 pm

Thanks ReformedCharacter, that works. Nice and simple too. I knew it would not be complicated and gives complete control of how each user opens the file.

It does not/did not work with iRC = in front which was to help show the errors with

Code: Select all

Set oShell = CreateObject("WScript.Shell")

and

Code: Select all

Wscript.Quit iRC
at the end.

Instead it causes/shows an error.

scotia
Lemon Quarter
Posts: 3566
Joined: November 4th, 2016, 8:43 pm
Has thanked: 2376 times
Been thanked: 1947 times

Re: Visual Basic script

#213654

Postby scotia » April 8th, 2019, 4:28 pm

swill453 wrote:If the main editor uses Calc and all the other users also have Calc installed and you want to force them to open it in Calc

I now see what was required - I mistakenly thought that some had calc, and others had excel, and some had both - with the requirement to use calc if present. Ignore my contribution with fileexists - I should read the requirement more carefully.


Return to “Technology - Computers, TV, Phones etc.”

Who is online

Users browsing this forum: No registered users and 16 guests