473,545 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ftp transfer by vba ?

30 New Member
Does anyone know if its possible to make a ftp-file transfer by vba?
What I have in mind is a routine in vba or a macro wich will e.g. transfer my database or parts of it to a back-up site
Aug 8 '08 #1
25 5993
NeoPa
32,563 Recognized Expert Moderator MVP
I just finished implementing one this last week in fact :)

It's not too simple but is certainly possible. Let me dig up some details.
Aug 9 '08 #2
NeoPa
32,563 Recognized Expert Moderator MVP
I start off with two named ranges in a hidden sheet of my workbook. Carats (^) indicate that the following character should indicate a replacement string.
^A = Account name on FTP site
^C = A placeholder to add an optional cd line in FTP file if required - If not set to empty line
^F = Name of file required to upload
^N = Name of FTP file (excluding the .Ftp bit) - Use this for CMD file too for simplicity
^P = Password for ^A
^R = Root Folder - the folder that the spreadsheet is found in
^S = FTP site address

The data for the files themselves are included below. I needed a fair amount of flexibility. If you don't then you can replace the variable bits with hard coded items.
Range("CMD")
Expand|Select|Wrap|Line Numbers
  1. ECHO ON
  2. CD /D "^R"
  3. "%SystemRoot%\System32\FTP.Exe" -n -s:^N.Ftp -w:16384 ^S
Range("FTP")
Expand|Select|Wrap|Line Numbers
  1. user ^A ^P
  2. binary
  3. lcd ^R
  4. ^C
  5. bell
  6. put ^F
  7. bye
Aug 9 '08 #3
zaankanter
30 New Member
Thanx NeoPa for your answer. I'm a novice, so I don't fully grasp it yet, but before asking more dumb questions I'm going to dig in your answer some more.
I'm sure I'll be back with further questions.
Thanks for now.

Chris
Aug 9 '08 #4
NeoPa
32,563 Recognized Expert Moderator MVP
You can use this procedure as a basis for what you need to do to save your CMD & FTP files.
I had to strip out some of my code as it's specific to what I needed and wouldn't suit you. You will need to Copy/Paste the procedure, but you'll also need to make relevant amendments for your situation too.

When the files are prepared and are ready, you just need to execute the CMD file using the Shell() function.
Expand|Select|Wrap|Line Numbers
  1. 'WriteFile() creates a text file (strFile) from the data in ranFile.
  2. 'The file is stored relative to the folder this spreadsheet is found in.
  3. Private Function WriteFile(ranFile As Excel.Range, _
  4.                            ByVal strFile As String) As Boolean
  5.     Dim Various replacement variables as strings
  6.     Dim strMsg As String
  7.     Dim intSaveType As Integer
  8.     Dim ranThis As Excel.Range
  9.  
  10.     WriteFile = True    'Default to failure
  11.     'Set up replacement strings
  12.     Call Workbooks.Add
  13.     'Use a matching range in the new worksheet that will
  14.     'take the data from the passed range
  15.     With Range(...See comment above ...)
  16.         .Value = ranFile.Value
  17.         For Each ranThis In .Cells
  18.             ranThis = Replace(ranThis.Value, "^A", ...)
  19.             ranThis = Replace(ranThis.Value, "^C", ...)
  20.             etc
  21.         Next ranThis
  22.     End With
  23.     With ActiveWorkbook
  24.         strFile = strRootFolder & "\" & strFile
  25.         On Error Resume Next
  26.         Kill strFile
  27.         If Err And Err <> conErrNoFile Then
  28.             strMsg = Replace("Error(%N) - ""%D""", "%N", Err.Number)
  29.             strMsg = Replace("Error(%N) - ""%D""", "%D", Err.Description)
  30.             Call MsgBox(strMsg, vbCritical Or vbOKOnly, "Kill File")
  31.         Else
  32.             'I don't know why, but it seems necessary to clear Err
  33.             Call Err.Clear
  34.             Call .SaveAs(Filename:=strFile, _
  35.                          FileFormat:=xlTextPrinter, _
  36.                          CreateBackup:=False, _
  37.                          AddToMRU:=False)
  38.             WriteFile = Err
  39.         End If
  40.         On Error GoTo 0
  41.         Call .Close(SaveChanges:=False)
  42.     End With
  43. End Function
Aug 9 '08 #5
NeoPa
32,563 Recognized Expert Moderator MVP
Thanx NeoPa for your answer. I'm a novice, so I don't fully grasp it yet, but before asking more dumb questions I'm going to dig in your answer some more.
I'm sure I'll be back with further questions.
Thanks for now.

Chris
That's fine Chris. This isn't too basic stuff - I expect some questions.

Hopefully what I've included so far at least has the building blocks to support what you need to accomplish.

PS. If you manage to get this to work for you then you can probably swagger a bit for your boss and colleagues ;)
Aug 9 '08 #6
zaankanter
30 New Member
Hi

This is realy difficult for me. But one step at a time.

Please tell me what the -n , -s and -w in this bit stand for

Expand|Select|Wrap|Line Numbers
  1. ECHO ON
  2. CD /D "^R"
  3. "%SystemRoot%\System32\FTP.Exe" -n -s:^N.Ftp -w:16384 ^S
By the way, there are no collegues or bosses to boast at. I'm just a one-guy operation, doing a hobby beside my regular non-IT job.
Check me out: ** Link removed **
Aug 13 '08 #7
NeoPa
32,563 Recognized Expert Moderator MVP
I was happy to check that out, but I'm afraid we don't allow links to personal web sites in the technical areas so I've removed it from your post.

Of course, pleasant as it was, I couldn't understand too much of it as I can only do ein - tien in Dutch (pretty well) anyway.
Aug 13 '08 #8
NeoPa
32,563 Recognized Expert Moderator MVP
Hi

This is realy difficult for me. But one step at a time.

Please tell me what the -n , -s and -w in this bit stand for

Expand|Select|Wrap|Line Numbers
  1. ECHO ON
  2. CD /D "^R"
  3. "%SystemRoot%\System32\FTP.Exe" -n -s:^N.Ftp -w:16384 ^S
By the way, there are no collegues or bosses to boast at. I'm just a one-guy operation, doing a hobby beside my regular non-IT job.
Check me out: ** Link removed **
No worries. Any questions can be dealt with. As many as you have :)

FTP Command Line will give you all you need but :
-n : Suppresses the ability to log on automatically when the initial connection is made. This stops anonymous logon.
-s : Specifies a text file that contains FTP commands. These commands run automatically after FTP starts. This parameter allows no spaces. Use this parameter instead of redirection (<).
-w : Specifies the size of the transfer buffer. The default window size is 4096 bytes. This (default) runs quite slowly in my experience, but probably depends on networking parameters and how many you choose to run in parallel.
Aug 13 '08 #9
zaankanter
30 New Member
Its 23.30 local time, so I will struggle on tomorrow.
Thanks for your answer so far, sorry for the link I shouldn't have posted.
Guess it's probably for the best. People googling my name/trademark could very well stumble upon this thread, wich of course doesn't reflect my proffesional qualities in a very good light :)
Aug 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2092
by: Harsh Thakur | last post by:
Hi, I'd like to know the performance related differences between Response.Redirect and Server.Transfer. I'd like to redirect the user to a different page. I can either do a Response.Redirect("URL") or a Server.Transfer("URL"). So I'd like to find out which is more efficient/better. Can anyone please tell me or give any pointers to links on...
5
7810
by: Julien C. | last post by:
Hi all, I have an "EditeItem.aspx" page which lets me edit properties of an "Item". In the OnClick() event of my Save button, I do save Item changes to the database and then I redirect the user to the Item page "ViewItem.aspx" with a simple : Server.Transfer("ViewItem.aspx"); I'd like to pass another HTTP parameter so that in the...
9
4594
by: Mark | last post by:
Hello I'm trying to use a Server.Transfer in a try-catch (I cannot put it outside the Try-Catch as it is nested deep within a component that is called in a try-catch loop) The problem is that the Server.Transfer always throws the ThreadAbortException. MSDN acknowledges that this is a unque exception that will be automatically rethrown - i.e....
5
2561
by: Guadala Harry | last post by:
I've been reading up on Server.Transfer as well as doing some testing, and it appears to always raise the ThreadAbortException error. On one hand I've read a bunch of promotional-type material touting the benefits of Server.Transfer and none of them mention ThreadAbortException - but the MSDN documentation says Server.Transfer will always...
11
6004
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it falls into an infinite loop and later a StackOverflow Exception. I need to do this and not a Response.Redirect or a transfer with the bool in...
15
1923
by: dee | last post by:
Hi, I'm curious why MS decided to have Transfer hide the target page's url invisible? Any guesses ? Thanks. Dee.
8
3878
by: bryan | last post by:
I've got a custom HttpHandler to process all requests for a given extension. It gets invoked OK, but if I try to do a Server.Transfer I get an HttpException. A Response.Redirect works, but I really need to avoid the extra round-trip to the client. I've tried Passing the page name, the full URL, and the instance of the handler class to the...
6
2175
by: n# | last post by:
A Basic Question in ASP.NEt 1.1 In Page_Load Event I am doing a Server.Transfer. But it throws an error on the browser windows showing "Server Application Not Found" Pls help me
4
3991
by: evantay | last post by:
I'm using ASP.NET 2.0 with VS.NET 2005. I'm trying to access properties from my master pages within a page that inherits from that master page (a child page). However the values are always null. In my masterpage I have this: private bool m_AlreadyTested; public bool AlreadyTested { get { return m_AlreadyTested; }
2
3091
by: =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post by:
Hi, I'm using Threads, and when I try to do Server.Transfer, I recieved an error. (child object does not exist...) My Code: Dim t As New Thread(AddressOf Hilo) Private Sub Hilo() Thread.Sleep(1000)
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.