将图像添加到Word文档并使用VBScript设置替代文本。

huangapple go评论48阅读模式
英文:

Add image to word with alt text using VBScript

问题

我编写了一个vbscript(.vbs)来将图像添加到Word文档中的书签位置。我需要文档可访问,所以我需要在Word文档中的图像上添加替代文本。我感谢任何帮助。下面的脚本将图像放入文档中,但.AlternativeText未起作用。我做错了什么?

以下是代码:

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
Set doc = objWord.Documents.Open("C:\Test.docx")
On Error Resume Next
Set SHP = doc.Bookmarks("bkm_1").Range.InlineShapes.AddPicture("C:\my_image.png")
SHP.AlternativeText = "这是替代文本"
On Error GoTo 0
Call doc.SaveAs2("C:\test_update.docx", 12)
doc.Saved = True
objWord.Quit
英文:

I wrote a vbscript (.vbs) to add an image to a word document at a bookmark. I need the document to be accessible so I need to add alt text to the image in the word doc. I appreciate any help. The script below puts the image in a doc, but the .AlternativeText isn't working. Any ideas what I am doing wrong?

Here is the code

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
Set doc = objWord.Documents.Open("C:\Test.docx")
On Error Resume Next
Set SHP = doc.Bookmarks("bkm_1").Range.InlineShapes.AddPicture("C:\my_image.png")
Set .AlternativeText = "This is the alt text"
On Error GoTo 0
Call doc.SaveAs2("C:\test_update.docx", 12)
doc.Saved = TRUE
objWord.Quit

答案1

得分: 2

当您添加图片时,SHP 被设置为一个 InlineShape 对象:

Set SHP = doc.Bookmarks("bkm_1").Range.InlineShapes.AddPicture("C:\my_image.png")

您只需要指定该对象以使用其 AlternativeText 属性:

SHP.AlternativeText = "这是替代文本"
英文:

When you add your picture, SHP is set to an InlineShape object:

Set SHP = doc.Bookmarks("bkm_1").Range.InlineShapes.AddPicture("C:\my_image.png")

You just need to specify that object to use its AlternativeText property:

SHP.AlternativeText = "This is the alt text"

huangapple
  • 本文由 发表于 2023年6月2日 05:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定