Solved

using vba to copy file to a network share with username and password

Posted on 2004-08-26
6
4,274 Views
Last Modified: 2012-05-05
I have an access project and I am using the file dialog to allow a user to select a file. I need to copy this file to a network share that is not mounted as a drive, since I do not want anyone access to the shared folder, only the application.

So I need to be able to pass the username and password in the copy command somehow. I tried copy (xcopy) and cannot seem to pass username, password information. I could perhaps use net use to mount it, copy the file, then unmount it. If that turns out is the only solution, how do I find the first available drive letter to use for mounting.

0
Comment
Question by:drakkarnoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:drakkarnoir
ID: 11906872
I tried using Shell() to do a net use to mount the network share, then filecopy to copy it, then net use to delete the drive. it is very very slow that way, so I hope there may be a better alternative
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 11909341
Well, I don't know if this is what you're looking for, but it gets the job done.  I don't know what you consider to be "very very slow" so this might be too slow to.  I tested it by copying a 15mb file over an 11mbps wireless network.  It took about 30 seconds in that setting.  Hope this helps.

set objFSO = CreateObject("Scripting.FileSystemObject")
set wshNet = CreateObject("WScript.Network")
strUsername = "Domain\Username"
strPassword = "Password"
strSource = "Path and file to copy"
strDestination = GetNextLetter("F")     '<-- You can change the letter.  It represents the starting letter to work from.
wshNet.MapNetworkDrive strDestination, "\\Server\Share", , strUsername, strPassword
objFSO.CopyFile strSource, strDestination & "\"
wshNet.RemoveNetworkDrive strDestination, True
Set objFSO = Nothing
Set wshNetwork = Nothing

'The following function is from: http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q303209&ID=KB;EN-US;Q303209
Function GetNextLetter(DriveLetter)
   'Start x at the ascii value of the drive letter to start the search
   'unless something is passed in. This sample uses capital letters and
   'starts at F.
   If IsEmpty(DriveLetter) Then
    x = 70
   Else
    x = Asc(DriveLetter)
   End If
   
   Set oDrives = wshNet.EnumNetworkDrives

   'Step by two since the first item in the collection is the drive letter
   'and the second item is the network mapping
   For i = 0 To oDrives.Count - 1 Step 2
    If Chr(x) & ":" = oDrives.Item(i) Then
        x = x + 1
    End If
   Next
   
   GetNextLetter = Chr(x) & ":"
End Function
0
 

Author Comment

by:drakkarnoir
ID: 11914896
that works good, my anti-virus popped up when doing Set oDrives = wshNet.EnumNetworkDrives but I can work around that, thanks.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 76

Expert Comment

by:David Lee
ID: 11915349
You're welcome.  Glad I could help.
0
 

Expert Comment

by:andybrooke
ID: 32942562
I tried this but get object required error 424. And debugs to Set oDrives = wshNet.EnumNetworkDrives
0
 
LVL 76

Expert Comment

by:David Lee
ID: 32966996
Hi, andybrooke.

That would mean that the object wshNet wasn't created successfully on line #2 of the code or was destroyed prematurely.  Can you run the code from a debugger to see what's happening?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question