简体   繁体   中英

Macro unable to save results when it is made to run using vbscript

I've created a macro to scrape the title of posts from a webpage. The macro runs fine when I try it manually.

However, my intention is to run and save the result using a .vbs file which will be executed through a .bat file so that I can ultimately make use of it through windows task scheduler .

When I click on this .bat file to check whether it will work at all, It does open that macro using .vbs and scrape the content as it is supposed to.

The only problem I'm facing is that I can't make the .vbs file save the result in that workbook. How can I save the result?*

.vbs contains:

RunMacro
Sub RunMacro()
  Dim xl, path, xlBook
  path = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
  Set xl = CreateObject("Excel.application")
  Set xlBook = xl.Workbooks.Open(path & "\basicScraper.xlsm", 0, True)
  xl.Application.Visible = False
  xl.DisplayAlerts = False
  xl.Application.Run "basicScraper.xlsm!MyMacro.GetPosts"
  xl.ActiveWorkbook.Save
  xl.ActiveWindow.Close
End Sub

.bat contains:

cscript macro.vbs "C:\Users\WCS\Desktop\vba scheduler\macro.vbs"

This is the macro I'm working with (module name: MyMacro ):

Sub GetPosts()
    Dim S$, r&, post As Object

    With New XMLHTTP
        .Open "GET", "https://stackoverflow.com/questions", False
        .send
        S = .responseText
    End With

    With New HTMLDocument
        .body.innerHTML = S
        For Each post In .getElementsByClassName("question-hyperlink")
            r = r + 1: Cells(r, 1) = post.innerText
        Next post
    End With
End Sub

@robots.txt You want to use the Windows Task Scheduler, create a task calling the .vbs

The .vbs should look like

Set XLObj = CreateObject("Excel.Application")
XLObj.visible = true
XLObj.Workbooks.Open "T:\he\path\to\basicScraper.xlsm"
XLObj.Run "'T:\he\path\to\basicScraper.xlsm'!MyMacro.GetPosts"
XLObj.quit
set XLObj = nothing

If I understand your code correctly, you try to insert the inner text of each link into some cells: Try to specify where exactly you want to put it (eg Worksheets"(Book 1").Cells(r, 1)).

Dim xHttp               As MSXML2.XMLHTTP
Dim hDoc                As MSHTML.HTMLDocument
Dim objCollection       As Object
Dim objElement          As Object
Dim strLink             As String


Set xHttp = New MSXML2.XMLHTTP

xHttp.Open "GET", "https://stackoverflow.com/questions", False
xHttp.send

Do Until xHttp.READYSTATE = 4
    DoEvents
Loop

If xHttp.Status = 200 Then
    Set hDoc = New MSHTML.HTMLDocument
    hDoc.body.innerHTML = xHttp.responseText

    Set objCollection = hDoc.getElementsByClassName("question-hyperlink")

            For Each objElement In objCollection
                strLink = objElement.InnerText
                Worksheets("Book 1").Cells(r, 1) = strLink
                r = r + 1
            Next

End If

ActiveWorkbook.Save

You should find your results in Book 1, rows 1 to n.

Found out the solution!!!

The .bat file should contain:

@echo off
echo %~dp0
cd /d %~dp0
"C:\Users\WCS\Desktop\New folder\macro.vbs" "C:\Users\WCS\Desktop\New folder\basicScraper.xlsm"

And the .vbs file should contain:

Dim xl, args

Set args = Wscript.Arguments
Set xl = CreateObject("Excel.application")
xl.Workbooks.Open args(0)
xl.Application.Visible = False
xl.Application.Run "GetPosts"
xl.ActiveWorkbook.Save
xl.ActiveWorkbook.Close
xl.Quit

The basicScraper.xlsm should be as it is. That's it.

Now, putting the complete address of the .bat file's location to the schedulers program/script inputbox in Action tab will do the trick.

Post script: I kept all three files .bat , .vbs and .xlsm in a single folder before execution and got the result as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM