VBA调用微软Bing中的翻译功能

API函数库的下载,请到 https://www.cnblogs.com/ryueifu-VBA/p/10128063.html 

Private IBing As API.Bing
Sub 翻译()
    Dim result As String
    Set IBing = New API.Bing
    result = IBing.Translate(from:="zh-Hans", too:="ja", Text:="这是一位特别上进的90后青年")
    Debug.Print result
End Sub

翻译结果:これは、特に90後の若者です

from和too分别是原先的语言种类和目标语言名称,ja表示日语。

查看全部的语言名称,请运行下面的程序

Sub 所有语言名称()
    Dim langs() As String
    Set IBing = New API.Bing
    langs = IBing.LanguageNameList
    Dim i As Integer
    For i = LBound(langs) To UBound(langs)
        Debug.Print langs(i)
    Next i
End Sub

打印结果

af
am
ar
bn
bg
ca
cs
cy
da
de
el
en
es
et
fa
fi
fr
fr-CA
ga
gu
he
hi
hr
hu
id
is
it
ja
kk
km
kn
ko
lo
lv
lt
mk
ml
mr
ms
mt
my
nl
nb
pl
ps
pt
pt-PT
ro
ru
sk
sl
sr-Cyrl
sv
ta
te
th
tr
uk
ur
uz
vi
zh-Hans
zh-Hant

Sub 翻译查询()
    Dim results() As String
    Set IBing = New API.Bing
    results = IBing.Tlookup(from:="ja", too:="en", Text:="見込み")
    Dim i As Integer
    For i = LBound(results) To UBound(results)
        Debug.Print results(i)
    Next i
End Sub

翻译查询的功能是 一词对应多个翻译结果

上述程序中日语单词的中文意思是“预计”,翻译成英文有下面这么多:

expected
prospective
prospect
potential
chance
likelihood
estimated
promise

Sub 文字转mp3()
    Set IBing = New API.Bing
    IBing.Text2MP3 lang:="zh-Hans", Text:="滚滚长江东逝水", Filename:="D:\Temp\滚滚长江.mp3"
    Dim IAudio As API.Audio
    Set IAudio = New API.Audio
    IAudio.mciSendString "play", "D:\Temp\滚滚长江.mp3"
    IAudio.mciSendString "stop", "D:\Temp\滚滚长江.mp3"
End Sub

文字转mp3,可以把指定语言的指定句子自动生成本地mp3音频文件

接下来用IAudio里的函数播放。

Sub 英文转汉语()
    Dim results() As String
    Set IBing = New API.Bing
    results = IBing.English2Chinese("Like")
    Dim i As Integer
    For i = LBound(results) To UBound(results)
        Debug.Print results(i)
    Next i
End Sub

英文转汉语就是一个大词典,列出每个词性和结果

 

posted @ 2020-12-06 14:06  ryueifu  阅读(1953)  评论(8编辑  收藏  举报