【问题标题】:Excel VBA ERROR - Compile Error, Syntax ErrorExcel VBA 错误 - 编译错误、语法错误
【发布时间】:2021-10-28 02:39:44
【问题描述】:

大家好,我正在尝试关注https://www.youtube.com/watch?v=Wn9L1MD_y0Y&t=107s,了解如何使用 VBA 自动化 whatsapp。

这是整个代码。

Sub WebWhatsApp()

'Activate Selenium Type Library: Tools > Reference

Dim bot As New WebDriver
Dim ks As New Keys

'Init New Chrome instance & navigate to WebWhatsApp
bot.Start "chrome", "http://web.whatsapp.com/"
bot.Get "/"

'Ask user to scan the QR code. Once logged in, continue with macro
MsgBox "Please scan the QR code. After you are logged in, please confirm this message box by clicking"

'Determinate number of messages by identifying the number of last rows in column A
lastrow = Cells(Rows.Count, 1).End(x1Up).Row

'Search phonenumber/name, press enter, paste text into WebWhatsApp, press Enter to send message

For i = 2 To lastrow
    'Get search text (phone number or name) from worksheet
    searchtext = Sheets(1).Range("A" & i).Value

    'Get textmessage from worksheet
    textmessage = Sheets(1).Range("B" & i).Value

    'click in the searchbox"
    bot.FindElementByXPath("//*[@id="side"]/div[1]/div/label/div/div[2]").Click
    
    
    'Wait 500 ms
    bot.Wait (500)

    'Insert search text(phone number or name)
    bot.SendKeys (searchtext)

    'Wait 500 ms
    bot.Wait (500)

    'Press Enter to confirm search text
    bot.SendKeys (ks.Enter)

    'Wait 500 ms
    bot.Wait (500)

    'Load message into WebWhatsApp
    bot.SendKeys (textmessage)

    'Wait 500 ms
    bot.Wait (500)

    'Press Enter to send the message
    bot.SendKeys (ks.Enter)

Next i

'Get notification once done,
MsgBox "Done :)"


End Sub

显然我遇到了错误

    bot.FindElementByXPath("//*[@id="side"]/div[1]/div/label/div/div[2]").Click

它说明编译错误:语法错误

谁能告诉我为什么?我已经清楚地遵循了说明和代码。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    问题在于side 周围的双引号。

    要解决问题,请用单引号替换它们:

        'click in the searchbox"
        bot.FindElementByXPath("//*[@id='side']/div[1]/div/label/div/div[2]").Click
    

    或者加倍。

        'click in the searchbox"
        bot.FindElementByXPath("//*[@id=""side""]/div[1]/div/label/div/div[2]").Click
    

    【讨论】:

    • 单引号呢?您可能需要三重引号或部分连接:"//*[@id=" & """" & "side" & """" & "]/div[1]/div/label/div/div[2]"。编辑 ine 后,您可以在 VBA 编译时随意玩耍。
    • 好的,谢谢!现在我有另一个错误。 lastrow = Cells(Rows.Count , 1).End(x1UP.Row) 出现运行时错误“1004”:应用程序定义或对象定义错误
    • 应该是End(xlUp).Row 而不是End(x1UP.Row)。请注意,您可以通过在模块顶部添加Option Explicit 来避免此类错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多