PDA

View Full Version : Solved: Inserting an image and formating without using Paste Special



russell13osu
01-04-2011, 10:44 AM
Hi all,

I'm in the midst of finishing some procedures that are called from a userform which should simplify repetitive manual tasks for the people in my office. All code is functioning well, but there is one small problem I can't seem to get around.

There is a file image that gets inserted into our Word documents, which are eventually translated and displayed online. When done manually, the user would insert the file, Cut, and then Paste Special as a "Picture (PNG)" in order to get the image to display correctly online. Without doing this Cut and Paste Special, our translation tool will place a faint border around the image, which is undesirable. I was able to automate this task with this code:


headingBullet = "Z:\path to my.png"

Selection.InlineShapes.AddPicture FileName:= headingBullet, LinkToFile:=False, SaveWithDocument:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.PasteSpecial Link:=False, DataType:=14, Placement:=wdInLine, DisplayAsIcon:=False
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.LockAspectRatio = msoTrue
Selection.ShapeRange.Height = 28.8
Selection.ShapeRange.Width = 28.8
Selection.ShapeRange.Rotation = 0#
Selection.ShapeRange.PictureFormat.Brightness = 0.5
Selection.ShapeRange.PictureFormat.Contrast = 0.5
Selection.ShapeRange.PictureFormat.ColorType = msoPictureAutomatic
Selection.ShapeRange.PictureFormat.CropLeft = 0#
Selection.ShapeRange.PictureFormat.CropRight = 0#
Selection.ShapeRange.PictureFormat.CropTop = 0#
Selection.ShapeRange.PictureFormat.CropBottom = 0#

The problem I'm having is that the superseding routines now require the clipboard to operate. I need to insert a file image in the same manner as above without using the clipboard. I'm pretty sure the attribute I need is the "DataType:=14." I cannot find a way to directly use this though, as it is a WdPasteDataType. I can't seem to find an enumeration list anywhere that even tells me what this DataType is. Is it simply a PNG picture?

Does anyone have insight about how I might insert this image, in the desired format, without using the clipboard (or by possibly going two items deep into the Office clipboard)?

Thanks in advance,
Bob
MS Word 2003 SP3, Office Pro 2003

fumei
01-04-2011, 11:39 AM
So you insert the file, select it, and Cut it, and paste it back in again?

russell13osu
01-04-2011, 12:20 PM
Yes. It was the only way our employees found to get the image to translate with no border shading. That's obviously an issue with our translation tool, but being able to Paste Special "Picture (PNG)" with any image will lose the border, even if the file image is a PNG to being with. I think it has to do with the DataType used when the image is pasted back in.

russell13osu
01-04-2011, 12:42 PM
Perhaps there's a way to store the image to a variable at run time, using the variable in place of the clipboard... :dunno

fumei
01-04-2011, 01:21 PM
It does seems to be a Paste DataType, and ONLY a Paste DataType. If this is correct then you are indeed stuck going through a paste operation.

macropod
01-04-2011, 10:28 PM
If this is simply for on-line display, why not add the images to the web page directly, instead of going via the document?

If you do need to go via the document, you might eliminate the problem borders by ensuring image compression in Word if 'off' and/or by cropping the images very slightly.

For example, using a somewhat more efficient version of your code:
With Selection
.InlineShapes.AddPicture FileName:=headingBullet, LinkToFile:=False, SaveWithDocument:=True
'.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
'.Cut
'.PasteSpecial Link:=False, DataType:=14, Placement:=wdInLine, DisplayAsIcon:=False
With .ShapeRange.Fill.Visible = msoFalse
.Fill.Solid
.Fill.Transparency = 0#
With .Line
.Weight = 0.75
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoFalse
End With
.LockAspectRatio = msoTrue
.Height = 28.8
.Width = 28.8
.Rotation = 0#
With .PictureFormat
.Brightness = 0.5
.Contrast = 0.5
.ColorType = msoPictureAutomatic
.CropLeft = 1#
.CropRight = 1#
.CropTop = 1#
.CropBottom = 1#
End With
End With
End With
With Application.CommandBars.FindControl(ID:=6382)
MsgBox "Please ensure compression is not selected"
.Execute
End WithYou might care to experiment with/without compression and the 1pt cropping (both of which are implemented in the above code). You may also want to experiment with the amount of cropping.

russell13osu
01-05-2011, 02:34 PM
Gerry and Paul, thank you for your help.

I ended up just programmatically redrawing the graphic using an MsoAutoShape. Then I was able to remove the anchor on the image by setting it inline with the text. So, my workaround was to scrap the file image all together. However, this solution won't help anyone who has the same issue and cannot draw their image in Word. It's working for me though!

Thanks again.