2013-12-23 61 views
0

使用VBA,是否可以從類似於下面格式的網頁中僅提取JavaScript函數文本?使用VBA從Javascript函數源中提取數據

我開始使用.innerText,但我擔心通過JavaScript上方頁面上可能填充的海量數據進行解析。我可以執行JavaScript,然後在存儲時提取值,但之後我必須處理「另存爲」窗口。這是內部公司的網頁,所以我無法提供鏈接。

在一天結束時,我需要的是文件名「save.F1234567890」(它們總是以「save.F」開頭,但每次運行網站時都會更改數字)。然後,我將使用文件名和URLDownloadToFile函數保存到我的計算機,因爲我已成功使用過去。

在此先感謝!

<input name="stuff" type=HIDDEN value="999" > 
<input type=hidden name=orgn value=.> 
<SCRIPT LANGUAGE="JavaScript"> 
function setThis(this)  
{  
for (i=0;i<document.forms[0].set_this.length;i++) 
    { 
    if (document.forms[0].set_this[i].checked==true)  
     setthis= document.forms[0].set_this[i].value;  
    }          
    if (setthis == 1)  
     { document.forms[0].filename.value = "save.F1234567890";  
      document.forms[0]._program.value = "download.excel"; } 
     else          
     if (setthis == 2)  
      { document.forms[0].filename.value = "save.MB1234567890";  
      document.forms[0]._program.value = "download.excel"; }  
     else          
     if (setthis == 3)  
      document.forms[0]._program.value = "download.savereport";  

     else          
     if (setthis == 4)  
     { document.forms[0].filename.value = "save.E1234567890";  
      document.forms[0]._program.value = "download.process"; } 

     document.forms[0].submit();  
}   
</SCRIPT>   
<TABLE BORDER=1> 
<tr><td><input name="set_this" type=radio value="3" checked >Save Parameters. <input type=text size=8 name="logon" VALUE="USERNAME"></td></tr> 
<tr><td><input name="set_this" type=radio value="1" >Download data as .CSV file </td></tr> 
<tr><td > 
<input type=button value="Submit" onClick="javascript:setThis(); "> 
</td></tr><tr><td > 
<input type="button" value="Previous Screen" onClick="history.back()"> 
</td></tr></table> 
+0

using VBA? Visual Basic for Application? – pbenard

+0

是的。我從Access 2010啓動所有自動化。 –

+0

是否總是隻有一個「save.F」? – Blackhawk

回答

0

通常我不會推薦一個正則表達式來解析網頁,但是你想要的是如此具體,它可能是最有效的方法。假設只有一個「save.F」,您可以使用以下命令查找字符串。

您可以通過單擊工具--->參考並選中「Microsoft VBScript Regular Expressions 5.5」旁邊的複選框,將RegExp庫添加到VBA項目中。

Public Function GetSaveName(strHTML) As String 
    Dim oRegExp As RegExp 
    Dim oMatches As MatchCollection 
    Dim oMatch As Match 

    Set oRegExp = New RegExp 
    With oRegExp 
     .Global = True 
     .Multiline = True 
     .IgnoreCase = False 
     .Pattern = """save.F[^""]*?""" 
     Set oMatches = .Execute(strHTML) 
    End With 

    Set oMatch = oMatches.Item(0) 

    GetSaveName = oMatch.Value 
End Function 
+0

如果我拉動網頁的innerText並通過您的功能運行,您建議的答案將起作用。你推薦這種方法或不同的方法? –

+0

@Mars_Out還有其他一些方法可以做到這一點。首先,使用HTML DOM來查找特定的Javascript塊,並通過特定的搜索來代替整個頁面。其次,您甚至可能會找到一種運行它以獲取價值的方法,如編寫SHDocVw.InternetExplorer腳本或將Script標記的內文加載到JScript中。 – Blackhawk

+1

我使用了Tim的建議,它只抓取了函數的文本,然後使用您的建議剪切了文本,結果非常好。謝謝你們倆! –