:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

[VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#8

by snasui » Mon Jul 10, 2017 8:35 pm

:D กรณี Upload ได้แค่ไฟล์เดียวผมยังไม่มี Idea เพิ่มเติม เนื่องจากไม่มี Environment ให้ทดสอบครับ

สำหรับภาพการแสดง Folder ใน VBA ไม่มี Control ลักษณะนั้นจะมีก็แต่ใน .Net เรียกว่า TreeView ครับ

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#7

by kannaree » Wed Jul 05, 2017 6:22 pm

ขอสอบถามอาจารย์นอกเรื่องหน่อยค่ะ

ฟังก์ชั่นการทำงานในลักษณะนี้ ที่มีเครื่องหมายการเป็นข้อมูล บวก ลบ แตกเป็น subset แบบรูปด้านล่าง
เรียกว่าอะไรหรอคะ ? พยายามจะหา keyword แต่ก็ไม่พบ และ vba สามารถเก็บข้อมูลในลักษณะนี้ได้หรือไม่คะ
what.png
what.png (5.48 KiB) Viewed 181 times
ขอบคุณค่ะ

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#6

by kannaree » Wed Jul 05, 2017 11:19 am

ตามที่ได้แก้ไขโค้ดตามอาจารย์ ไม่ได้คะ upload ไปแค่ไฟล์เดียว

Code: Select all

Sub Sendmail()

    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
    Dim fileName As String
    
    file_path = Range("b3").Value
    inputValue = InputBox("Enter username", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
    
    inputValue = InputBox("Enter password for username " & FTPusername, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
    
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
    
    fileName = Dir(file_path & "\*.xls")
    Do While Len(fileName) > 0
        filenum = FreeFile
        Open cFTPCommandsFile For Output As #filenum
        Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
        'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
        Print #filenum, "open " & cFTPServer & " " & cFTPPort
        Print #filenum, "user " & FTPusername & " " & FTPpassword
        Print #filenum, "cd Website-Search"
        Print #filenum, "binary"
        Print #filenum, "put " & QQ(file_path & "\" & fileName)
        
        
        'The next line is temporarily commented out to omit the bye command from the ftp commands script.
        'This keeps the command window open to show whether the ftp commands worked successfully or not.
        'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
        
        'Print #filenum, "bye"
        Close #filenum
        
        'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
        'upon initial connection because we want to use the username and password specified in the command file
        
        FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
        
        'Run ftp command synchronously, waiting for the command to return
        'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
        'whether or not the ftp commands worked successfully.
        'WindowStyle 0 hides the command window.
        'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
        
        CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
            
        'Delete the ftp commands file so that the username and password are not left lying around
        
        Kill cFTPCommandsFile
        
        fileName = Dir
    Loop
    
    MsgBox "Finished"



End Sub
4455.png
4455.png (54.91 KiB) Viewed 189 times

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#5

by snasui » Tue Jul 04, 2017 7:20 pm

:D ลองใช้การ Loop ตามตัวอย่าง Code ด้านล่างเข้ามาช่วยว่า FTP ทุกไฟล์ได้หรือไม่ครับ

Code: Select all

Sub SendFileViaFTP_Click()
    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
    Dim fileName As String
    
    file_path = Range("b3").Value
    inputValue = InputBox("Enter username", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
    
    inputValue = InputBox("Enter password for username " & FTPusername, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
    
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
    
    fileName = Dir(file_path & "\*.xls")
    Do While Len(fileName) > 0
        filenum = FreeFile
        Open cFTPCommandsFile For Output As #filenum
        Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
        'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
        Print #filenum, "open " & cFTPServer & " " & cFTPPort
        Print #filenum, "user " & FTPusername & " " & FTPpassword
        Print #filenum, "cd Website-Search"
        Print #filenum, "binary"
        Print #filenum, "put " & QQ(file_path & "\" & fileName)
        
        
        'The next line is temporarily commented out to omit the bye command from the ftp commands script.
        'This keeps the command window open to show whether the ftp commands worked successfully or not.
        'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
        
        'Print #filenum, "bye"
        Close #filenum
        
        'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
        'upon initial connection because we want to use the username and password specified in the command file
        
        FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
        
        'Run ftp command synchronously, waiting for the command to return
        'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
        'whether or not the ftp commands worked successfully.
        'WindowStyle 0 hides the command window.
        'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
        
        CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
            
        'Delete the ftp commands file so that the username and password are not left lying around
        
        Kill cFTPCommandsFile
        
        fileName = Dir
    Loop
    
    MsgBox "Finished"

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#4

by kannaree » Tue Jul 04, 2017 8:37 am

ขอบคุณค่ะอาจารย์

* กดปุ่ม (4) UploadFile to FTP
Module 4 : SendFileViaFTP_Click()
Copy of CreateFileCopy - test(Backup).xlsm
(67.93 KiB) Downloaded 20 times


file ตัวอย่างที่ต้องการ upload
ARROW DENMARK.xlsx
(70 Bytes) Downloaded 13 times
AVNET.xlsx
(10.59 KiB) Downloaded 15 times

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#3

by snasui » Mon Jul 03, 2017 8:18 pm

:D แนบไฟล์โปรแกรมมาด้วยจะได้ช่วยทดสอบให้ได้ครับ

Re: [VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#2

by kannaree » Mon Jul 03, 2017 3:17 pm

ทำได้แล้วค่ะ ในการ upload file to ftp สามารถ upload ไปได้ไฟล์งานเดียว
ติดปัญหาตรงที่ มีหลายไฟล์ในโฟรเดอร์ จะต้องแก้โค้ดในส่วนนี้อย่างไรค่ะ

Code: Select all

'download File to FTP
Const cFTPServer As String = "ftp.svi.co.th"     'CHANGE THIS.
Const cFTPPort = 21
Const cFTPCommandsFile As String = "FTP_commands.txt"

Sub SendFileViaFTP_Click()
    Dim inputValue As Variant
    Dim FTPusername As String, FTPpassword As String
    Dim filenum As Integer
    Dim FTPcommand As String
    Dim wsh As Object
    
    File_Path = Range("b3").Value
    inputValue = InputBox("Enter username", cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPusername = CStr(inputValue)
    
    inputValue = InputBox("Enter password for username " & FTPusername, cFTPServer)
    If inputValue = False Or inputValue = "" Then Exit Sub
    FTPpassword = CStr(inputValue)
    
    'Create file containing ftp commands.  The file has to contain the username and password
    'to connect to the ftp server.  Creating the file and deleting it immediately after use gives some degree of
    'security in preference to having a static file containing the username and password
    
    filenum = FreeFile
    Open cFTPCommandsFile For Output As #filenum
    Print #filenum, "!REM upload .csv file"             'Use !REM for comments with a Windows ftp server
    'Print #filenum, "!# upload .csv file"              'Use '# for comments with a Unix ftp server
    Print #filenum, "open " & cFTPServer & " " & cFTPPort
    Print #filenum, "user " & FTPusername & " " & FTPpassword
    Print #filenum, "cd Website-Search"
    Print #filenum, "binary"
    Print #filenum, "put " & QQ(File_Path & "\" & "*.xlsx")
    
    'The next line is temporarily commented out to omit the bye command from the ftp commands script.
    'This keeps the command window open to show whether the ftp commands worked successfully or not.
    'If the bye command is omitted you must type bye in the command window to exit ftp and end this procedure
    
    'Print #filenum, "bye"
    Close #filenum
    
    'Construct ftp command line, specifying the file containing FTP commands.  The -n parameter suppresses auto-login
    'upon initial connection because we want to use the username and password specified in the command file
    
    FTPcommand = "ftp -i -n -s:" & QQ(cFTPCommandsFile)
    
    'Run ftp command synchronously, waiting for the command to return
    'WindowStyle 1 displays the command window. Use this, together with omitting the bye command, to verify
    'whether or not the ftp commands worked successfully.
    'WindowStyle 0 hides the command window.
    'See http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx
    
    CreateObject("WScript.Shell").Run Command:=FTPcommand, WindowStyle:=1, waitonreturn:=True
        
    'Delete the ftp commands file so that the username and password are not left lying around
    
    Kill cFTPCommandsFile
    
    MsgBox "Finished"



End Sub
Private Function QQ(text As String) As String
    QQ = Chr(34) & text & Chr(34)
End Function
307_1.png
307_1.png (135.95 KiB) Viewed 208 times
307_2.png
307_2.png (131.85 KiB) Viewed 208 times
307_3.png
307_3.png (93.42 KiB) Viewed 208 times

[VBA] สอบถามการ Upload File ขึ้นไปยัง ftp server

#1

by kannaree » Mon Jul 03, 2017 1:31 pm

สวัสดีค่ะ อาจารย์และทุกท่าน ดิฉันอยากจะลอง Upload File เมื่อกดปุ่ม ใน Excel ไฟล์งานที่กำหนด
จะถูก Upload ไปยัง ftp ที่มีการตั้งค่าไว้ แต่ลองโค้ดแล้ว ไฟล์ไม่ถูก upload ขึ้นไป และไม่แสดง Error อยากทราบว่าจะต้องทำอย่างไรค่ะ

Code: Select all

'download File to FTP
Sub SendFileViaFTP_Click()

Dim strDirectoryList As String
Dim lStr_Dir As String
Dim lInt_FreeFile01 As Integer
Dim lInt_FreeFile02 As Integer

On Error GoTo Err_Handler
    lStr_Dir = ThisWorkbook.path
    lInt_FreeFile01 = FreeFile
    lInt_FreeFile02 = FreeFile

    '' ANW  07-Feb-2003 :
    strDirectoryList = lStr_Dir & "\Directory"

    '' Delete completion file
    If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")

    '' Create text file with FTP commands
    Open strDirectoryList & ".txt" For Output As #lInt_FreeFile01
    Print #lInt_FreeFile01, "ftp.svi.co.th"
    Print #lInt_FreeFile01, "100001"
    Print #lInt_FreeFile01, "9922"
    Print #lInt_FreeFile01, "cd source/uploads"
    Print #lInt_FreeFile01, "binary"
    Print #lInt_FreeFile01, "send " & ThisWorkbook.path & "\test.txt"

    '' To receive a file, replace the above line with this one
    ''Print #lInt_FreeFile01, "recv \Picture.gif " & ThisWorkbook.Path & "\Picture.gif"
    
    Print #lInt_FreeFile01, "bye"
    Close #lInt_FreeFile01

    '' Create Batch program
    Open strDirectoryList & ".bat" For Output As #lInt_FreeFile02
    Print #lInt_FreeFile02, "ftp -s:" & strDirectoryList & ".txt"

    Print #lInt_FreeFile02, "Echo ""Complete"" > " & strDirectoryList & ".out"
    Close #lInt_FreeFile02

    '' Invoke Directory List generator
    Shell (strDirectoryList & ".bat"), vbHide '', vbMinimizedNoFocus
    'Wait for completion
    Do While Dir(strDirectoryList & ".out") = ""
        DoEvents
    Loop

    Application.Wait (Now + TimeValue("0:00:03"))

    '' Clean up files
    If Dir(strDirectoryList & ".bat") <> "" Then Kill (strDirectoryList & ".bat")
    If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")
    If Dir(strDirectoryList & ".txt") <> "" Then Kill (strDirectoryList & ".txt")

bye:

Exit Sub

Err_Handler:
    MsgBox "Error : " & Err.Number & vbCrLf & "Description : " & Err.Description, vbCritical
    Resume bye



End Sub

ข้อมูลในไฟล์ Directory
ftp.svi.co.th
100001
9922
cd source/uploads
binary
send C:\Users\PUR_Kannaree\Desktop\test.txt
bye


ขอบคุณค่ะ

Top