How to insert a non-inline picture into a word document using VB6?

点点圈 提交于 2021-02-11 13:50:12

问题


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")

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?


回答1:


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.

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)

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

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.

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.

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.

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

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.

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.




回答2:


Ok, What I ended up doing that worked was this:

Dim a As Object
On Error Resume Next
a = oDoc.Shapes.AddPicture("C:\Users\name\Desktop\file.jpg", , , 25, 25, 25, 25)

For some reason, This places the image at the position and size. When I looked at the docs for ".AddPicture" I realized it returns a Shapes object. So I just had it store it in a throw-away object. For some reason it then would respond with an error, but would end up placing on the doc anyways. So I used:

On Error Resume Next

This skips the error. After that, the picture places as expected and the rest of the doc is made as expected

Thank you for your answers



来源:https://stackoverflow.com/questions/51451179/how-to-insert-a-non-inline-picture-into-a-word-document-using-vb6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!