+ Reply to Thread
Results 1 to 4 of 4

rename the file's name

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-29-2014
    MS-Off Ver
    Office 2013
    Posts
    115

    rename the file's name

    Hi everybody,
    I have to download a lot of files. I have URL for each of them.I listed all urls in one excel column.
    The problem is when I save the second file. When VBA goes and check for the second url, find the file and when try to save it, the code shows me an error because is another file in the same folder with the same name
    How i can put an index so from file1 to go file2?
    Sub test()
    
    Dim myURL As String
    For I = 2 To 4
    myURL = Worksheets("sheet1").Range("A2")
    Dim WinHttpReq As Object
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    WinHttpReq.Open "GET", myURL, False
    WinHttpReq.Send
    
    myURL = WinHttpReq.ResponseBody
    
    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.ResponseBody
        oStream.SaveToFile ("C:\Users\verbamore\Desktop\1.pdf")
        oStream.Close
    End If
    Next I
    
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: rename the file's name

    Untested:

    Option Explicit
    
    Sub test()
        Const sRoot     As String = "C:\Users\verbamore\Desktop\"
        Dim sURL        As String
        Dim i           As Long
        Dim WinHttpReq  As Object
        Dim oStream     As Object
        Dim iFile       As Long
    
        For i = 2 To 4
            sURL = Worksheets("Sheet1").Range("A2").Value2
            Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
            With WinHttpReq
                .Open "GET", sURL, False
                .Send
    
                If .Status = 200 Then
                    Set oStream = CreateObject("ADODB.Stream")
                    oStream.Open
                    oStream.Type = 1
                    oStream.Write .ResponseBody
    
                    Do
                        iFile = iFile + 1
                    Loop While Len(Dir(sRoot & iFile & ".pdf"))
    
                    oStream.SaveToFile sRoot & iFile & ".pdf"
                    oStream.Close
                End If
            End With
        Next i
    End Sub
    That very first line is one you should get used to using.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    05-29-2014
    MS-Off Ver
    Office 2013
    Posts
    115

    Re: rename the file's name

    Quote Originally Posted by shg View Post
    Untested:

    Option Explicit
    
    Sub test()
        Const sRoot     As String = "C:\Users\verbamore\Desktop\"
        Dim sURL        As String
        Dim i           As Long
        Dim WinHttpReq  As Object
        Dim oStream     As Object
        Dim iFile       As Long
    
        For i = 2 To 4
            sURL = Worksheets("Sheet1").Range("A2").Value2
            Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
            With WinHttpReq
                .Open "GET", sURL, False
                .Send
    
                If .Status = 200 Then
                    Set oStream = CreateObject("ADODB.Stream")
                    oStream.Open
                    oStream.Type = 1
                    oStream.Write .ResponseBody
    
                    Do
                        iFile = iFile + 1
                    Loop While Len(Dir(sRoot & iFile & ".pdf"))
    
                    oStream.SaveToFile sRoot & iFile & ".pdf"
                    oStream.Close
                End If
            End With
        Next i
    End Sub
    That very first line is one you should get used to using.
    thanks shg

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: rename the file's name

    You're welcome.

    Please don't quote whole posts; it just clutters the forum.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 01-21-2014, 10:52 AM
  2. Excel VBA find and replace string in non text file and rename file
    By razzack in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2013, 02:43 PM
  3. Replies: 1
    Last Post: 04-20-2013, 10:30 AM
  4. [SOLVED] import every file in the folder and rename sheet to the imported file name
    By vivek_work in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-04-2012, 02:46 PM
  5. Rename file and Create new file with new folder with macro/vba
    By rizki96 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-11-2011, 08:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1