2015-02-10 122 views
0

我正在寫一個Windows應用程序,它通過WebBrowser控件導航並根據特定條件自動填充表單。在一些特定的表格中,有一些日期正在被取消,以便根據該標準檢索某些信息。問題在於,星期一,開始日期必須是前一週的星期五。與所有其他日子(週二至週五)一樣,開始日期只是前一天。我正在嘗試使用If語句來解決此問題,但是當我運行該表單時,它不會填充正確的日期。在IF中使用'Day'函數在IF語句中根據工作日更改日期和發送表單

這裏是我使用的是什麼:

Public Class Form1 
    Inherits Form 

    Dim otherdays = Format(Now.AddDays(-1), "M/d/yyyy") 
    Dim mondays = Format(Now.AddDays(-3), "M/d/yyyy") 
    Dim current = Format(Now, "M/d/yyyy") 

    Private Sub approval1() 
     WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", mondays) 
     WebBrowser1.Document.GetElementById("Search").InvokeMember("click") 
    End Sub 

    Private Sub approval2() 
     WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", otherdays) 
     WebBrowser1.Document.GetElementById("Search").InvokeMember("click") 
    End Sub 

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 
     If ComboBox1.SelectedItem = "IM Approval" Then 
      WebBrowser1.Navigate("https:") 
      WaitForPageLoad() 

      'This is the part that does not work: 
      If Now.Day = Day.Monday Then 
       approval1() 
       WaitForPageLoad() 
       WebBrowser1.Navigate("javascript: ('','')") 
      Else 
       approval2() 
       WaitForPageLoad() 
       WebBrowser1.Navigate("javascript: ('','')") 
      End If 
     End If 
    End Sub 

web瀏覽器通過JavaScript的瀏覽不是問題。填寫日期是唯一的問題。

+0

當您逐句通過代碼時,它是否進入頂部條件塊?如果ComboBox1.SelectedItem =「IM Approval Approval」Then'?它是否將任何東西放在表單元素中? – 2015-02-10 14:58:42

+0

@georgecb這聽起來像當前日期是過去的日期,跳過週末。在VBA中,日期時間使用計算機區域設置中設置的時間進行計算。你可以檢查你的,看看是否有任何線索。 – Alex 2015-02-10 15:26:37

+2

'Now.Day'返回每月的一天(一個整數)。用'Now.DayOfWeek'替換你的If語句。 – Blackwood 2015-02-10 15:36:02

回答

1

您檢查星期幾不工作的原因是您正在檢查每月的哪一天。這裏是更正了if語句的更簡單版本的類。

Public Class Form1 
    Inherits Form 

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 
     If ComboBox1.SelectedItem = "IM Approval" Then 
      WebBrowser1.Navigate("https:") 
      WaitForPageLoad() 

      Dim adjust As Integer = If(Now.DayOfWeek = DayOfWeek.Monday, -3, -1) 
      WebBrowser1.Document.GetElementById("txtDate").SetAttribute("Value", Format(Now.AddDays(adjust), "M/d/yyyy")) 
      WebBrowser1.Document.GetElementById("Search").InvokeMember("click") 

      WaitForPageLoad() 
      WebBrowser1.Navigate("javascript: ('','')") 
     End If 
    End Sub 
End Class 
+0

謝謝!這解決了這個問題! – georgecb 2015-02-10 16:30:23