[使用vba代码的ms Word文档文本翻译
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[使用vba代码的ms Word文档文本翻译相关的知识,希望对你有一定的参考价值。
我是VBA的新手,我想将Word文档文本翻译成另一种语言(英语为泰卢固语)算法:1.打开文件2.选择孔文字3.发送翻译键4.复制翻译的文本5.粘贴在该文档中或替换在活动文档中6.保存并关闭代码:
Public Sub text_translate()
Dim insert As String
ActiveDocument.Select
Selection.Copy
SendKeys "%rls~" 'Alt+Review+Translate+Translate Selected Text'
insert = "file:///C:/Users/puser/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/A343AB40.html"
insert.Copy
insert.Paste
ActiveDocument.save
ActiveDocument.close
End Sub
如果有其他翻译方法,请提及,这将对我有所帮助,谢谢我已经在Microsoft社区中发布了https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_win10-mso_365hp/ms-word-vba-code-automating-language-translation/ffffeaca-c87e-4bad-b8a1-e8dd23fa5f88?tm=1583086073941我可以看到重播,但仍然无法解决
答案
我尝试过Web抓取方法,我不知道它是正确的还是违反规则的
但是在复制翻译后的文本时仍然有一些问题
查看代码:
公开Sub Translate_text()
Dim IE As Object 'for browser
ActiveDocument.Select 'select text from open document
Selection.Copy 'copy selected text
Set IE = CreateObject("InternetExplorer.Application") 'Create object for IE
With IE
.Visible = True
.navigate "https://translate.google.com/#view=home&op=translate&sl=auto&tl=te" 'opening the following Url
Set IEdoc = IE.Document ' get complete webpage as document through this we can access html tags
IEdoc.all.Source.Value = Selection 'paste selected copied text in textarea
Dim ele As MSHTML.IHTMLElement 'for search element
Dim eles As MSHTML.IHTMLElementCollection 'for collection of elements
Set eles = IEdoc.getElementsByTagName("div") 'search by classname at first position
For Each ele In eles
If ele.innerText("class") = "tlid-copy-translation-button copybutton" Then 'checking the element in element collection
'I want to copy the translated text "problem with this"
ele.Click 'if the element is found then click on the button by this it will copy the translated text
Else
End If
Next ele
End With
.Quit 'close the browser
ActiveDocument.SendKeys = "^v" 'paste in active document
.Save
.Close
结束子
以上是关于[使用vba代码的ms Word文档文本翻译的主要内容,如果未能解决你的问题,请参考以下文章
选择非粗体文本并更改其颜色(选定段落的)- MS Word VBA 宏
搜索特定列后,如何使用 MS Word VBA 代码对具有特定文本的单元格进行着色?