|
-
Feb 4th, 2009, 06:55 PM
#1
Thread Starter
New Member
Accesing Directory Issue
Hi
What I'm trying to do is write a small program that copies a file from a USB to another directory so a program already installed on the computer can load this data.
Usually copying files from one directory into another wouldn't be an issue.
Code:
Module Module1
Sub Main()
Dim SpecialFolder As String
Dim fs As Object
Dim oldPath As String, newPath As String
oldPath = "H:\" 'Folder file is located in
newPath = "H:\folder" 'Folder to copy file to
fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile(oldPath & "\" & "January2009.txt", newPath & "\" & "January2009.txt") 'This file was an .xls file
fs = Nothing
End Sub
End Module
Unfortunately the program were using loads information from the specific user application settings directory
So on my computer this is C:\Documents and Settings\Alastair\Application Data\DPlot
but on your computer this might be C:\Documents and Settings\*****\Application Data\DPlot
Hence i cant hardcode the destination directory for the files I'm copying
I have tried googling this with very ineffective results as it is a reasonably specific problem. However every single peace of software that you install on you computer will usually access the "Application Settings" folder and copy information into without experiencing any problems, hence this is defiantly possible I just don't know the correct way to do it.
The only thing that I have been able to find that look remotely is the function
Environment.GetFolderPath(SpecialFolder.ApplicationData)
However I'm having difficulties using this in VBA as it immediately asks me to declare what "SpecialFolder" is.
If possible i would also like to change the code i have written so it just loads January2009.txt from the "root directory" of the USB rather than having to specify H:\ because if i put the USB in someone elses computer this could change to G:\ and then it wouldn't work.
Any ideas?
-
Feb 4th, 2009, 07:03 PM
#2
Re: Accesing Directory Issue
Environment.GetFolderPath is a .net call, and you stated you are using VBA, so that is why it isn't working.
Here is a microsoft article on how to get the special folders using VBA:
http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
-
Feb 4th, 2009, 07:35 PM
#3
Thread Starter
New Member
Re: Accesing Directory Issue
I decided to use the GetUserProfileDirectory function as it seemed a lot more simple
The following code compiles with no errors but when i check C:\Documents and Settings\Alastair the file hasn't been coped across
Dim sBuffer As String, Ret As Long, hToken As Long
Declare Function GetUserProfileDirectory Lib "userenv.dll" Alias "GetUserProfileDirectoryA" (ByVal hToken As Long, ByVal lpProfileDir As String, ByVal lpcchSize As Long) As Boolean
Sub Main()
Dim SpecialFolder As String
Dim fs As Object
Dim oldPath As String, newPath As String, specialPath As String
specialPath = GetUserProfileDirectory(hToken, sBuffer, 255)
oldPath = "H:\"
newPath = "specialPath"
fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile(oldPath & "\" & "January2009.txt", newPath & "\" & "January2009.txt")
fs = Nothing
End Sub
Last edited by Amcl074; Feb 5th, 2009 at 04:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|