Open This Workbook.Path

maclachlan19

Board Regular
Joined
Jul 8, 2013
Messages
53
I'm having problems understanding how to designate a specific file path on this line of code.
This works as long as the file is one folder before \old reports..... As soon as the file is moved the code no longer works.
I would like to be able to hardcode a folder such as D:\DealerShare\Accounting\ELR Analysis.log, but this doesn't seem to work.


Code:
Open ThisWorkbook.Path & "\old reports\Log Files\ELR Analysis.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]




the full code is as follows in case this helps
Code:
 Private Sub Workbook_Open()On Error GoTo errhandler:
Open ThisWorkbook.Path & "\old reports\Log Files\ELR Analysis.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Application.UserName, Now
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Exit Sub
errhandler:
MsgBox "Log file not saved"


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This is working, is it what you meant?


Code:
Open "C:\Users\andrew.marshall\Desktop\logfile.txt" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
 
Upvote 0
Try

Code:
Private Sub Workbook_Open()
On Error GoTo errhandler:
Open "D:\DealerShare\Accounting\ELR Analysis.log" For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  , Application.UserName, Now
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  
Exit Sub
errhandler:
MsgBox "Log file not saved"
End Sub
 
Upvote 0
If the drive or path or filename does not exist, it won't "work". A hard coded drive:\path\filename.ext has the same limitation as the dynamic path that you set. You can use Dir() to determine if the folder exists at least.

Of course you could always create the folders. Again, if the drive does not exist, that won't work either.

Some people do hard paths but the drive changes. e.g. D drive is USB drive 1. It has the path. On USB drive 2, when I put it in after drive 1, it becomes drive "E". So, if I use Dir() I can at least check for the path in drive D or drive E. When using the Dir() for folders, use the 2nd parameter. e.g.
Code:
Sub Test()
    MsgBox Dir(ThisWorkbook.Path & "\old reports\Log Files\", vbDirectory) <> "", , _
        ThisWorkbook.Path & "\old reports\Log Files\ELR Analysis.log"
    MsgBox Dir("D:\DealerShare\Accounting\", vbDirectory) <> "", , _
        "D:\DealerShare\Accounting\"
    MsgBox Dir(ThisWorkbook.Path & "\", vbDirectory) <> "", , ThisWorkbook.Path & "\"
End Sub
 
Upvote 0
One last question on this project (which is working amazing)
How can I add the filename to the text document. Currently it shows the username and date/time.

Rich (BB code):
Private Sub Workbook_Open()On Error GoTo errhandler:
Open "D:\Accounting ST\Log Files\ELR Analysis.log" For Append As #1 
Print #1 , Application.UserName, Now
Close #1 
Exit Sub
errhandler:
MsgBox "Log file not saved"


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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