Edit Macro to Open Specific File Automatically

erock24

Well-known Member
Joined
Oct 26, 2006
Messages
1,163
I am using this code to get me to a path then I click on the file I want and it opens it and makes it a .csv file. Then my code does stuff with it and closes it. My question is, do I even have to click on it? Can I get the macro to automatically open the file. Note the "NewestFolderInThePath" is a function that open me up to the "newest" created folder in the path. From there I double click file.

Code:
NewestFolderInThePath = ReturnNewestFolder("\\datawhse\root\LAW81\LAWSON\print\RBLEVINS2\anrvwfins\1\")
   Path = "\\datawhse\root\LAW81\LAWSON\print\RBLEVINS2\anrvwfins\1\"
    
    TheFile = Path & NewestFolderInThePath
    CreateObject("WScript.Shell").CurrentDirectory = TheFile
    
    TheFile = Application.GetOpenFilename("Excel Files (*.*), *.*", , "Open Balance Sheet File FRPMBSDTL")
If TheFile = "False" Then
      continue = MsgBox("Do you want to Continue to IS Download?", _
                    vbInformation + vbYesNo, _
                    "Continue?")

If continue = 6 Then GoTo IncSt
If continue = 7 Then Exit Sub
End If
      Oldfilename = TheFile

    ' Separate the file path and file name
      BS = InStrRev(Oldfilename, "\")
      Oldfilepath = Left(Oldfilename, BS)
      Newfilename = Right(Oldfilename, Len(Oldfilename) - BS)
    
    ' Add the CSV extension to the file name
      Dot = InStr(1, Newfilename, ".")
      Newfilename = Oldfilepath & Left(Newfilename, Dot) & "csv"
    
    ' Rename the file
      Name Oldfilename As Newfilename

    Workbooks.Open Filename:=Newfilename
     
    Set OpenA = Workbooks(Workbooks.Count)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
So can't you just add it to:

TheFile = Path & NewestFolderInThePath

like so?

TheFile = Path & NewestFolderInThePath & "SSFRPMBSDTL"
 
Upvote 0
No it errors out at:

Code:
CreateObject("WScript.Shell").CurrentDirectory = TheFile

I was getting the same error to my current macro before I added this:

Code:
Path = "\\datawhse\root\LAW81\LAWSON\print\RBLEVINS2\anrvwfins\1\"

So maybe we need to account for the file name. set it = to something then reference it something like this:

File = ????????
TheFile = Path & NewestFolderInThePath & File

I don't know. I'm obviously not an expert by any means.
 
Upvote 0
it's still not quite there yet.

But I found the problem
I put
Code:
msgbox(TheFile)
after
Code:
TheFile = Path & NewestFolderInThePath & "SSFRPMBSDTL"
it returns

\\datawhse\root\LAW81\LAWSON\print\RBLEVINS2\anrvwfins\1\895993994SSFRPMBSDTL.

It is not separating out the folder name 895993994 and the file name SSFRPMBSDTL.
How do I fix that?
 
Upvote 0
could you not add a forward slash to your code:

TheFile = Path & NewestFolderInThePath & "\SSFRPMBSDTL"
 
Upvote 0
Meader693,
I'm not sure if that is working;

I think I need to change this part also because it is prompting me to open a file:

Code:
TheFile = Application.GetOpenFilename("Excel Files (*.*), *.*", , "Open Balance Sheet File FRPMBSDTL")

To:

Just Open "TheFile". How do I say open "TheFile"
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top