简体   繁体   English

使用vbscript进行运行时,宏无法保存结果

[英]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 . 但是,我的意图是使用.vbs文件运行并保存结果,该文件将通过.bat文件执行,以便最终通过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. 当我单击此.bat文件以检查它是否将完全正常工作时,它确实使用.vbs打开了该宏并按预期方式刮取了内容。

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: .vbs包含:

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: .bat包含:

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

This is the macro I'm working with (module name: MyMacro ): 这是我正在使用的宏(模块名称: 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 @ robots.txt您要使用Windows Task Scheduler,创建一个调用.vbs的任务

The .vbs should look like .vbs应该看起来像

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)). 如果我正确理解了您的代码,则尝试将每个链接的内部文本插入到某些单元格中:尝试指定要确切放置的位置(例如,工作表“(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. 您应该在第一册第1到n行中找到结果。

Found out the solution!!! 找出解决方案!!!

The .bat file should contain: .bat文件应包含:

@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: .vbs文件应包含:

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. basicScraper.xlsm应该保持basicScraper.xlsm 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. 现在,将.bat文件位置的完整地址放入“ Action选项卡中的调度program/script框即可。

Post script: I kept all three files .bat , .vbs and .xlsm in a single folder before execution and got the result as expected. 发布脚本:执行之前,我将所有三个文件.bat.vbs.xlsm在一个文件夹中,并得到了预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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