无法在自定义文件夹中保存和关闭新创建的工作簿

时间:2019-03-01 22:01:28

标签: excel vba web-scraping filesystemobject

我创建了一个宏,该宏能够解析torrent网站中的某些电影名称。

我现在想做的是( 我的脚本已经可以满足前三个要求,除了第四个要求 ):

1. Create a folder in my desktop
2. Create a new workbook after the name of the `Genre`
3. write the data in that new workbook
4. Save and close the data-ridden workbook in the newly created folder

这是我到目前为止的尝试:

Sub CreateAndSaveWorbook()
    Const link = "https://yts.am/browse-movies/0/all/action/0/latest"
    Dim Http As New XMLHTTP60, Html As New HTMLDocument, genre$
    Dim post As HTMLDivElement, wb As Workbook
    Dim daddr$, fdObj As Object

    daddr = Environ("USERPROFILE") & "\Desktop\Test\"
    Set fdObj = CreateObject("Scripting.FileSystemObject")
    If Not fdObj.FolderExists(daddr) Then fdObj.CreateFolder (daddr)

    With Http
        .Open "GET", link, False
        .send
        Html.body.innerHTML = .responseText
    End With

    genre = Html.querySelector("select[name='genre'] option[value='action']").innerText
    Set wb = Workbooks.Add
    wb.SaveAs daddr & genre & ".xlsx"

    For Each post In Html.getElementsByClassName("browse-movie-bottom")
        R = R + 1: wb.Sheets(1).Cells(R, 1) = post.getElementsByClassName("browse-movie-title")(0).innerText
    Next post
End Sub
  

如何在自定义文件夹中保存并关闭新创建的工作簿?

1 个答案:

答案 0 :(得分:2)

这是您的答案:):将wb.saveAs更改为wb.Close true, daddr & genre & ".xlsx"