如何使用 VBA 将字形保存为图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6512392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save Word Shapes to image using VBA?
提问by Axle
I have a simple requirement to save MS-Office Drawing Objects embedded within a Word Doc to image files. The following code worked for image extraction from Powerpoint. However it does not work for MS-Word if I modify the ActivePresentation to ActiveDocument. The Export method was not available for shape object. Any ideas?
我有一个简单的要求,将嵌入 Word Doc 中的 MS-Office 绘图对象保存到图像文件中。以下代码适用于从 Powerpoint 提取图像。但是,如果我将 ActivePresentation 修改为 ActiveDocument,则它不适用于 MS-Word。导出方法不适用于形状对象。有任何想法吗?
Dim oPPTShap as Shape
For k = 1 To .Slides(intSlide).Shapes.Count
Set oPPTShape = ActivePresentation.Slides(intSlide).Shapes(k)
oPPTShape.Export "C:\images\s" & k & ".bmp", ppShapeFormatBMP
Next
回答by JMax
The code you are trying to copy from PPT VBA to Word VBA won't work because the functionality does not exist in Word.
您尝试从 PPT VBA 复制到 Word VBA 的代码将不起作用,因为 Word 中不存在该功能。
You can try by yourself : when you select shapes in Word and right-click, you do not have the function to Save as image... (versus in PPT you do have the function).
你可以自己试试:在Word中选择形状并右键单击时,您没有另存为图像的功能...(而在PPT中您有功能)。
Yet, from this page, the author points to a MVP who built a VBA solutionto do what you want : http://www.lebans.com/msword.htm
然而,从这个页面,作者指出了一个MVP,他构建了一个 VBA 解决方案来做你想做的事:http: //www.lebans.com/msword.htm
Hope it will do what you want,
希望它会做你想做的,
回答by Gary Lee
This is not great, as it outputs each image in EMF format, but it does write each of the images in the inline shapes collection to an individual file. It could of course be modified to do the other shapes collection.
这不是很好,因为它以 EMF 格式输出每个图像,但它确实将内联形状集合中的每个图像写入单个文件。当然可以修改它以进行其他形状收集。
I would like to enhance to to write JPG directly, but so far do not know the VBA to push WRITE output through a filter on the way. Therefore, to use these files you need to run some outboard post-process/batch to covert the files from EMF to something more usable.
我想增强到直接写JPG,但到目前为止还不知道VBA通过过滤器推送WRITE输出的方式。因此,要使用这些文件,您需要运行一些外部后处理/批处理来将文件从 EMF 转换为更有用的文件。
Sub WriteInlineShapesToFile()
Dim docCurrent As Document
Dim shapeCurrent As InlineShape
Dim RC As Integer
Dim vData() As Byte
Dim i As Long
Dim lWritePos As Long
Dim strOutFileName As String
Set docCurrent = ActiveDocument
i = 1
For Each shapeCurrent In docCurrent.InlineShapes
strOutFileName = "c:\temp\datafile" & CStr(i) & ".emf"
Open strOutFileName For Binary Access Write As #1
i = i + 1
vData = shapeCurrent.Range.EnhMetaFileBits
lWritePos = 1
Put #1, lWritePos, vData
Close #1
Next shapeCurrent
RC = MsgBox("Job complete.", vbOKOnly, "Job Status")
End Sub
回答by Siddharth Rout
Here is one dirty (and fastest) trick that I can think of :D
这是我能想到的一个肮脏(也是最快的)技巧:D
Save the word document to a temp folder as a webpage. Something like
将 word 文档作为网页保存到临时文件夹。就像是
ActiveDocument.SaveAs FileName:="C:\Temp\Sample.htm", FileFormat:=wdFormatHTML, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
All the shapes and pictures in the word document will be automatically saved in the folder called C:\Temp\Sample_Files
word文档中的所有形状和图片都会自动保存在名为C:\Temp\Sample_Files的文件夹中
You then then simply delete all the files in that folder which are not images :)
然后,您只需删除该文件夹中所有不是图像的文件:)
Let me know if you want to pursue this option :)
如果您想采用此选项,请告诉我:)
Sid
锡德
EDIT
编辑
Gosh I just realized it is an old thread. Hmm, all thanks to "Joel Coehoorn" for editing this thread LOLZ
天哪,我刚刚意识到这是一个旧线程。嗯,非常感谢“Joel Coehoorn”编辑这个帖子 LOLZ
回答by sober
Okay, this is not a solution, but a different approach to an old problem. So far I've come across lots of examples that either save the file out to a .HTML and then grab the images from a subfolder of the HTML document, or code that extracts the document archive and then grabs the files from the extracted set.
好的,这不是解决方案,而是解决旧问题的不同方法。到目前为止,我遇到了很多示例,它们要么将文件保存为 .HTML,然后从 HTML 文档的子文件夹中获取图像,要么提取文档存档,然后从提取的集合中获取文件的代码。
How about this:
这个怎么样:
Iterate through the Inline Shapes of the document if you're in Word.
For each Inline Shape:
a. Copy the image data to the clipboard. You've already got the other metadata of the image as you've obtained it via the object model in VBA.
b. You can copy the image for say the first Inline Shape like this:
ActiveDocument.InlineShapes(1).Range.CopyAsPicture
c. Now that the image data is in the clipboard, it should be possible to get the data off the clipboard. I know VBA sucks with this, but you should be able to use the Windows API to work around that. If you can somehow get the image data back, you can write it to a file with the metadata that you've obtained from the object model.
如果您在 Word 中,请遍历文档的内嵌形状。
对于每个内联形状:
一种。将图像数据复制到剪贴板。您已经获得了图像的其他元数据,因为您已经通过 VBA 中的对象模型获得了它。
湾 您可以像这样复制第一个内联形状的图像:
ActiveDocument.InlineShapes(1).Range.CopyAsPicture
C。现在图像数据在剪贴板中,应该可以从剪贴板中取出数据。我知道 VBA 很糟糕,但您应该能够使用 Windows API 来解决这个问题。如果您能以某种方式取回图像数据,您可以将它写入一个文件,其中包含您从对象模型中获得的元数据。
回答by jumpHyman
You can also write a PowerPoint macro inside word, if you add Microsoft Powerpoint to Word VBA References and define a Powerpoint application object in word.
如果将 Microsoft Powerpoint 添加到 Word VBA 引用并在 Word 中定义 Powerpoint 应用程序对象,则还可以在 Word 中编写 PowerPoint 宏。
So you can take advantage of PP export capabilities:
因此,您可以利用 PP 导出功能:
just select the shape in word, copy it to clipboard (selection.copy) , and paste it into an empty presentation in PP.
只需在word中选择形状,将其复制到剪贴板(selection.copy),然后将其粘贴到PP中的空演示文稿中。