簡體   English   中英

使用excel和IE的VBA腳本

[英]VBA script with excel and IE

我正在嘗試打開一個包含A列值的.xlsx文件(假設A1:A1000)。

我需要腳本從A1中選擇值並將其添加到URL。 然后,它需要打開URL並更改該特定站點上的設置。 然后它應確認更改並繼續下一個值A2並執行相同操作直到最后一個值(循環)。

我想出了他的追隨者:

Set IE = CreateObject("InternetExplorer.Application")    
 IE.Visible = 1    
 IE.navigate "http://xxxx/edit_product.php?Prod_ID=45306"    
 Do While (IE.Busy)    
     WScript.Sleep 100    
 Loop    
 Set Helem = IE.document.getElementByID("Status")    
 Helem.Value = "The change I need" 
 Do While (IE.Busy)    
     WScript.Sleep 100   
Loop     
Set Shell = WScript.CreateObject("WScript.Shell")
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{TAB}"
Shell.SendKeys "{ENTER}"

在此代碼中,URL中的prod_ID“45306”是來自單元格的值,因此應該是變量值。

網站上的操作有效,但如何打開Excel文件並循環操作(並創建URL變量)?

是否要從另一個工作簿運行此代碼,然后打開一個值不同的工作簿?

嘗試這個:

    Dim lr As Long, prodID as string
    Dim wb As Workbook, ws As Worksheet

    Set wb = Workbooks.Open(Filename:="c:\temp\attachment.xlsx") 'Set path to your file
    Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet
    lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'Last row used in column A

        For i = 1 To lr
            prodID = ws.Range("A" & i).Value

            IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID
            Do While (IE.Busy)
                WScript.Sleep 100
            Loop
            Set Helem = IE.document.getElementByID("Status")
            Helem.Value = "The change I need"
            Do While (IE.Busy)
                 WScript.Sleep 100
            Loop
            Set Shell = WScript.CreateObject("WScript.Shell")
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{TAB}"
            Shell.SendKeys "{ENTER}"
        Next

好的,這里適用於vbs。

我對vbs不太熟悉,但請嘗試下面的代碼。 如果您遇到任何錯誤,請告訴我。

Dim xlApp, wb, ws 
Dim filename, LastRow, prodID

Set xlApp = CreateObject("Excel.Application")
Set wb = xlapp.Workbooks.Open("c:\temp\attachment.xls") 'Set path to your file
Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1 

    For i = 1 To 10
        prodID = ws.Range("A" & i).Value

        IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID
        Do While (IE.Busy)
            WScript.Sleep 100
        Loop
        Set Helem = IE.document.getElementByID("Status")
        Helem.Value = "The change I need"
        Do While (IE.Busy)
             WScript.Sleep 100
        Loop
        Set Shell = WScript.CreateObject("WScript.Shell")
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{TAB}"
        Shell.SendKeys "{ENTER}"
    Next

wb.Close False
xlApp.Quit

set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing 

在JazzzyJoop的幫助下,我能夠獲得所需的腳本:D感謝JazzyJoop。 任何人都需要相同的東西下面的代碼

Dim xlApp, wb, ws 
Dim filename, LastRow, prodID


Set xlApp = CreateObject("Excel.Application")
Set wb = xlapp.Workbooks.Open("c:\temp\attachment1.xlsx") 'Set path to your file
Set ws = wb.Sheets("Sheet1") 'Set name of your worksheet
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1 

    For i = 1 To 4 'Range of values
        prodID = ws.Range("A" & i).Value

        IE.navigate "http://xxxx/edit_product.php?Prod_ID=" & prodID
        Do While (IE.Busy)
    WScript.Sleep 50
    Loop
    IE.Document.getElementById("Status").Value = "Status Needed"
    WScript.Sleep 100 
    Set oInputs = IE.Document.getElementsByTagName("input")

        For Each elm In oInputs
            If elm.Value = "Verwerk" Then
                elm.Click
                Exit For
            End If
    Next
    Do While IE.Busy Or IE.readyState <> 4
            WScript.Sleep 50
        Loop


    Next

wb.Close False
xlApp.Quit

set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM