如何使用VB6将非内嵌图片插入到word文档中? [英] How to insert a non-inline picture into a word document using VB6?
问题描述
我正在尝试使用 VB^ 附带的 Microsoft Word 15.0 对象库将图像插入到 Word 文档中,而我见过的插入图形文件的唯一方法是通过以下方式:
I am trying to insert an image into a word document using the Microsoft word 15.0 objects library included with VB^ and the only way I've seen to insert a graphics file is through this:
oDoc.Range.InlineShapes.AddPicture ("C:\Users\name\Desktop\file.jpg")
但是,我想要一张可以放置在文本上的图片以及我想要它在文档中的位置...有没有办法使用 VB6 代码来做到这一点?
But, I want a picture that can be positioned over the text and where I want it in the document... Is there any way to do this using VB6 Code?
推荐答案
Word 有两种不同的方式来管理图像和其他嵌入对象:作为 InlineShapes
和作为 Shapes
.第一个被视为与文本流中的字符相同;后者在与文本不同的层中具有文本换行格式和实时".
Word has two different ways to manage images and other embedded objects: as InlineShapes
and as Shapes
. The first are treated the same as characters in the text flow; the latter have text wrap foramtting and "live" in a different layer from the text.
要将图形文件作为Shape
插入:
To insert a graphics file as a Shape
:
Dim shp as Word.Shape
Set shp = oDoc.Shapes.AddPicture(FileName, LinkToFile, _
SaveWithDocument, Left, Top, Width, Height, Anchor)
AddPicture
方法返回一个 Shape
对象.通常,当需要在插入对象后设置其他属性时,这很有用.例如为了指定文本换行格式.如果不需要 Shape
对象,则可以插入 Shape
而无需分配给对象.在这种情况下,请省略括号:
The AddPicture
method returns a Shape
object. Often, this is useful when additional properties need to be set after the object has been inserted. For example in order to specify the text wrap formatting. If no Shape
object is required a Shape
can be inserted without assigning to an object. In this case, leave out the parentheses:
oDoc.Shapes.AddPicture FileName, LinkToFile, _
SaveWithDocument, Left, Top, Width, Height, Anchor
虽然只需要 FileName
参数,但最后一个参数 - Anchor
- 如果您想控制图像的位置,则非常重要插入时定位.
While only the FileName
argument is required, the last argument - Anchor
- is very important if you want to control where the image is positioned when it's inserted.
也可以作为 InlineShape
插入,然后使用 ConvertToShape
以获得可以应用文本换行格式的 Shape
对象.
It's also possible to insert as an InlineShape
then use ConvertToShape
in order to have a Shape
object to which text wrap formatting can be applied.
每个Shape
必须与文档中的一个范围相关联.除非另有说明,否则这将是当前选择所在段落的第一个字符.为此,我强烈建议将 Range
传递给 Anchor
参数中的 Shapes.AddPicture
方法.
Every Shape
must be associated with a Range in the document. Unless otherwise specified, this will be the first character of the paragraph wherein the current selection is. I strongly recommend passing a Range
to the Shapes.AddPicture
method in the Anchor
argument for this reason.
请注意,一旦插入了 Shape
,就无法直接更改 anchor
的位置.可以使用 cut & 来完成.粘贴.另一种可能性是使用 ConvertToInlineShape
方法,以便您可以使用 Range
移动图形,然后使用 ConvertToShape
将其转回Shape
,但在这种情况下,可能需要重置许多定位和包装属性.这是使用转换"方法的示例:
Note that once a Shape
has been inserted there's no direct way to change the anchor
position. It can be done using cut & paste. Another possibility is to use the ConvertToInlineShape
method so that you can work with the Range
to move the graphic, then ConvertToShape
to turn it back into a Shape
, but in this case a number of positioning and wrap properties may need to be reset. Here an example of using the "convert" methods:
Sub MoveShapeToOtherRange()
Dim oDoc As Word.Document
Dim shp As Word.Shape
Dim ils As Word.InlineShape
Dim rngEnd As Word.Range, rngStart As Word.Range
Set oDoc = ActiveDocument
Set rngStart = oDoc.content
rngStart.Collapse wdCollapseStart 'start of document
Set rngEnd = Selection.Range
Set shp = oDoc.shapes.AddPicture(fileName:="C:\Test\icons\Addin_Icon16x16.png", _
Top:=0, Left:=10, anchor:=rngStart)
Set ils = shp.ConvertToInlineShape
Set rngStart = ils.Range
rngEnd.FormattedText = rngStart.FormattedText
rngStart.Delete
Set ils = oDoc.InlineShapes(1)
Set shp = ils.ConvertToShape
End Sub
默认情况下,Shape
将插入并激活 MoveWithText
.这意味着页面上的位置没有设置,编辑会影响垂直位置.例如,如果您希望 Shape
始终位于页面中央,请将其设置为 false.但是请注意,如果锚点移动到不同的页面,Shape
也会移动到该页面.
By default, a Shape
will insert with MoveWithText
activated. That means the position on the page is not set, editing will affect the vertical position. If you want the Shape
to always be centered on the page, for example, set this to false. Note, however, that if the anchor point moves to a different page, the Shape
will also move to that page.
有时,Left
和 Top
参数在添加 Shape
时不会采用" - 您可能需要再次设置这些添加后作为属性.
On occasion, the Left
and Top
arguments don't "take" when adding a Shape
- you may need to set these again as properties after adding it.
这篇关于如何使用VB6将非内嵌图片插入到word文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!