【问题标题】:Finding the English definition of a word in VBA在 VBA 中查找单词的英文定义
【发布时间】:2017-06-17 21:08:47
【问题描述】:

在 Excel 中,如果我在一个单元格中输入单词“PIZZA”,选择它,然后按 SHIFT+F7,我可以得到我最喜欢的食物的一个很好的英语词典定义。很酷。 但我想要一个能做到这一点的函数。 '=DEFINE("PIZZA")' 之类的东西。

有没有办法通过 VBA 脚本访问 Microsoft 的研究数据?我正在考虑使用 JSON 解析器和免费的在线词典,但 Excel 似乎内置了一个不错的词典。关于如何访问它的任何想法?

【问题讨论】:

  • 这是哪个版本的 Excel?
  • 它当然可以在 Excel 2010 for Windows 中使用,尽管“Pizza”在同义词库中给出了“未找到任何结果”
  • @MarkBaker 此处与 Excel 2013 相同。这是日文版,但英文和日文都没有命中。奇怪...

标签: excel vba


【解决方案1】:

如果 VBA 的 Research 对象不起作用,您可以尝试 Google Dictionary JSON 方法,如下所示:

首先,添加对“Microsoft WinHTTP 服务”的引用。

看完我疯狂的 JSON 解析技巧后,你可能还想添加你最喜欢的 VB JSON 解析器,like this one。

然后创建以下公共函数:

Function DefineWord(wordToDefine As String) As String

  ' Array to hold the response data.
    Dim d() As Byte
    Dim r As Research


    Dim myDefinition As String
    Dim PARSE_PASS_1 As String
    Dim PARSE_PASS_2 As String
    Dim PARSE_PASS_3 As String
    Dim END_OF_DEFINITION As String

    'These "constants" are for stripping out just the definitions from the JSON data
    PARSE_PASS_1 = Chr(34) & "webDefinitions" & Chr(34) & ":"
    PARSE_PASS_2 = Chr(34) & "entries" & Chr(34) & ":"
    PARSE_PASS_3 = "{" & Chr(34) & "type" & Chr(34) & ":" & Chr(34) & "text" & Chr(34) & "," & Chr(34) & "text" & Chr(34) & ":"
    END_OF_DEFINITION = "," & Chr(34) & "language" & Chr(34) & ":" & Chr(34) & "en" & Chr(34) & "}"
    Const SPLIT_DELIMITER = "|"

    ' Assemble an HTTP Request.
    Dim url As String
    Dim WinHttpReq As Variant
    Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

    'Get the definition from Google's online dictionary:
    url = "http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=" & wordToDefine & "&sl=en&tl=en&restrict=pr%2Cde&client=te"
    WinHttpReq.Open "GET", url, False

    ' Send the HTTP Request.
    WinHttpReq.Send

    'Print status to the immediate window
    Debug.Print WinHttpReq.Status & " - " & WinHttpReq.StatusText

    'Get the defintion
    myDefinition = StrConv(WinHttpReq.ResponseBody, vbUnicode)

    'Get to the meat of the definition
    myDefinition = Mid$(myDefinition, InStr(1, myDefinition, PARSE_PASS_1, vbTextCompare))
    myDefinition = Mid$(myDefinition, InStr(1, myDefinition, PARSE_PASS_2, vbTextCompare))
    myDefinition = Replace(myDefinition, PARSE_PASS_3, SPLIT_DELIMITER)

    'Split what's left of the string into an array
    Dim definitionArray As Variant
    definitionArray = Split(myDefinition, SPLIT_DELIMITER)
    Dim temp As String
    Dim newDefinition As String
    Dim iCount As Integer

    'Loop through the array, remove unwanted characters and create a single string containing all the definitions
    For iCount = 1 To UBound(definitionArray) 'item 0 will not contain the definition
        temp = definitionArray(iCount)
        temp = Replace(temp, END_OF_DEFINITION, SPLIT_DELIMITER)
        temp = Replace(temp, "\x22", "")
        temp = Replace(temp, "\x27", "")
        temp = Replace(temp, Chr$(34), "")
        temp = iCount & ".  " & Trim(temp)
        newDefinition = newDefinition & Mid$(temp, 1, InStr(1, temp, SPLIT_DELIMITER) - 1) & vbLf  'Hmmmm....vbLf doesn't put a carriage return in the cell. Not sure what the deal is there.
    Next iCount

    'Put list of definitions in the Immeidate window
    Debug.Print newDefinition

    'Return the value
    DefineWord = newDefinition

End Function

之后,只需将函数放入单元格中即可:

=DefineWord("lionize")

【讨论】:

  • 好吧,这不是我想要的路线,但它运行得非常好。干得好先生。我还是会为了好玩而玩弄“研究”对象,但是上面的代码很棒。
  • 如果这个答案对您有帮助,您应该考虑将其标记为答案。
  • 2018 年:脚本中使用的 Google 词典 URL 不再有效。有什么建议吗?
  • 问题:关于你的“疯狂的 JSON 配对技巧”的注释:安装这个 VBJSON 的东西是为了使这个功能正常工作吗?还是您只是提到它而没有在这里真正相关?
  • 你好。我是一个没有 VBA 知识的菜鸟。你能解释一下怎么做吗?如果它可以与其他语言一起使用。谢谢!
【解决方案2】:

通过研究对象

Dim rsrch as Research
rsrch.Query( ...

要查询,您需要有效 Web 服务的 GUID。不过,我无法找到 Microsoft 内置服务的 GUID。

【讨论】:

  • 这看起来很有希望。根据研究设置,看起来他们都引用了office.microsoft.com/Research/query.asmx
  • 您可以使用 Fiddler 来检查调用 - 看起来像 SOAP。
  • @Derek +1 发现了这一点。我无法找到对该服务的任何引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 2016-04-21
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
相关资源
最近更新 更多