Sorry, another no doubt silly question on VBA...

TartanSpecial

New Member
Joined
May 29, 2005
Messages
42
Continuing in a mini-series of TartanSpecial not having a clue about VBA.....

One of the reports I've inherited in my job has the code below. The code is actually in Business Object but open up a series of Excel files. The files open correctly but for some reason at the very end the file "Service_Comms_Ranking1" re-open , overwriting the changes already made.

Nothing in the Excel files seems to prompt it do so. I was wondering if anything in this code would prompt the file to be opened twice (or whether it lurks somewhere else)

As always any help is really gratefully received


Sub LaunchRankingExcel()
Const blank As String = " "
Dim retval
Dim filename, sWorkingDir, sOutputDir As String
Dim objXLS As New Excel.Application
Dim sCommandLine As String
Dim sXLInstallationDir As String

Application.Interactive = True

sWorkingDir = Application.GetInstallDirectory(boDocumentDirectory) & "\"
sOutputDir = "\\ldnroot\data\Equities\Trading\MDM\CMT_reporting\Marketing Packs\Embedded Charts DO NOT ERASE\"

Set objXLS = Excel.Application
objXLS.Visible = False
objXLS.Interactive = False
objXLS.DisplayAlerts = False

sXLInstallationDir = objXLS.Path & "\"
filename = "Service_Comms_Ranking1"

objXLS.Quit
Set objXLS = Nothing
If Application.Interactive Then
sCommandLine = sXLInstallationDir & "Excel.exe " & Chr$(34) & sOutputDir & filename & Chr$(34)
retval = Shell(sCommandLine, vbNormalFocus)
End If

End Sub
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
Hi TartanSpecial,

It appears that the lines:

sCommandLine = sXLInstallationDir & "Excel.exe " & Chr$(34) & sOutputDir & filename & Chr$(34)
retval = Shell(sCommandLine, vbNormalFocus)

are intended to re-open Excel, this time opening workbook Service_Comms_Ranking1. It looks to me like the code opens Excel the first time without opening the workbook just to find out where it is installed, then closes it and re-opens using a Shell command--which opens it as a separate process--to open the workbook.

If your computer has Excel.exe properly registered and the search path set up properly, I don't believe opening it the first time to find the installation directory is required. I suggest you try just running the following code instead:

Code:
Sub LaunchRankingExcel()

Dim retval
Dim filename As String, sOutputDir As String
Dim sCommandLine As String

Application.Interactive = True

sOutputDir = "\\ldnroot\data\Equities\Trading\MDM\CMT_reporting\Marketing Packs\Embedded Charts DO NOT ERASE\"

filename = "Service_Comms_Ranking1"

If Application.Interactive Then
sCommandLine = "Excel.exe " & Chr$(34) & sOutputDir & filename & Chr$(34)
retval = Shell(sCommandLine, vbNormalFocus)
End If

End Sub

Please let me know if this works (or not).

Damon
 

TartanSpecial

New Member
Joined
May 29, 2005
Messages
42
Thanks

Hi Damon

Thanks a lot for taking a look at this.

I ran the new code but got an error on the line -

retval = Shell(sCommandLine, vbNormalFocus)

Run Time Error 53 File not Found

Any ideas

Thanks for your looking into this
Nick
 

Watch MrExcel Video

Forum statistics

Threads
1,113,953
Messages
5,545,153
Members
410,666
Latest member
forzasec
Top