PDA

View Full Version : Solved: turning a bitmap



IkEcht
11-21-2008, 08:39 AM
Hi,

it shouldn't be too hard, but somehow I can't seem to do it. I have a bitmap at the end of my document that needs to be rotated by 90 degrees. Doing this in word itself is easy, but somehow when I try to record the macro, it is impossible to select the bitmap, let alone turn it.

There probably is a reason it is impossible to select a bitmap when trying to record a macro, but if recording it doesn't work, can someone here help me out?

all the best, IkEcht

TonyJollans
11-21-2008, 09:12 AM
Select it before starting to record and then use the toolbar button to rotate it.

IkEcht
11-21-2008, 09:16 AM
hmm this was too easy, sorry for taking your time ;)

got the code.. now to see if it works in the document as well, but don't see why it will not.

Then again, just wondering, is there a reason you can't select the bitmap while in recording mode?

fumei
11-21-2008, 10:55 AM
Recording macros is limited to selecting things through the GUI. It has some weird effects, particularly if you want to use a right click. You can sometimes get around this during recording by using Shift-F10 (the keyboard equivalent to a right click).

It sounds like the bitmap is a Shape, rather than an InlineShape.

If you put it in via Insert > Picture then it is an InlineShape. These can not be rotated.

If it is a Shape, and it is the LAST one in the document, then the easiest way is to make, and use, an object:
Dim oShape As Shape
Set oShape = ActiveDocument.Shapes(ActiveDocument.Shapes.Count)

oShape.IncrementRotation 90

This makes the object oShape the LAST shape - using Shapes.Count as the index number- and will rotate it 90 degrees clockwise.

oShape.IncrementRotation -90

will rotate it 90 degrees counterclockwise.

fumei
11-21-2008, 11:14 AM
If your task is a one-off, then the code in the previous post should do the trick. However, if you need to action a Shape (any Shape) in various ways, or multiple times, you can:

1. select the Shape
2. bookmark it, and of course give it a unique name

Now, you can easily create an OBJECT of the Shape, using the bookmark.

In the demo attached, there are two Shapes. There are two procedures (macros) on the top toolbar: "First Spoon" and "Second Spoon". Clicking on either of them will rotate the Shape 90 degrees, via the following code.
Sub Spoon1()
Dim oShape As Shape
Set oShape = ActiveDocument.Bookmarks("spoon_1") _
.Range.ShapeRange(1)

oShape.IncrementRotation 90
End Sub


Sub Spoon2()
Dim oShape As Shape
Set oShape = ActiveDocument.Bookmarks("spoon_2") _
.Range.ShapeRange(1)

oShape.IncrementRotation 90
End Sub

Each Shape was previously selected and bookmarked (spoon_1, spoon_2)

The advantage of this is that code actioning the Shape will execute correctly no matter where you move the Shape. This is because the code is tied to an object (the Shape inside the bookmark).

So even if you move spoon_1 to AFTER spoon_2...it does not make any difference.

The code in the previous post actioned the LAST Shape.

This code actions a specific Shape, again, no matter where it is in the document. Once again demonstrating the power of bookmarks.

IkEcht
02-06-2009, 08:38 AM
This seemed to help for a while. But now the situation just gets a bit more complicated. As in fact the bitmap is pasted in the document through vba.

So in the document there is a bookmark. Within that bookmark is a macrobutton that pastes a bitmap. After pasting, the bookmark is gone.. :(
(activedocument.bookmarks.count indeed changes and looses 1 bookmark).

is there a simple workaround for this?

lucas
02-06-2009, 09:33 AM
You probably need to paste it into the bookmark range. Can you post the code you use to paste the bitmap into the document?

fumei
02-06-2009, 11:20 AM
Yes, please give us the actual code.

It sounds like you ARE pasting into the bookmark range. In which case, the bookmark is deleted. There is code to get around that.

IkEcht
02-10-2009, 02:47 AM
Here is the code I use:

Sub verpltabel()
Dim i As Integer

xl.Sheets("verplaatsingen").Select
xl.Range("a1:r28").Select
xl.Selection.Copy
Selection.PasteSpecial link:=False, DataType:=wdPasteBitmap, _
Placement:=wdInLine, DisplayAsIcon:=False
overzichtstabeldraaien

End Sub

and

Sub overzichtstabeldraaien()
Dim oShape As Shape
Set oShape = ActiveDocument.Bookmarks("overzichtstabel").Range.ShapeRange(1)

oShape.IncrementRotation -90

End Sub

the code verpltabel is triggered by a macrobutton. The bookmark is right around the macrobutton.

fumei
02-10-2009, 01:42 PM
So you have a bookmark, and inside the bookmark is a macrobutton. That macro button puts the Shape into the same bookmark? Thus:

1. deleting the bookmark, AND
2. deleting the macrobutton

Is this correct?

IkEcht
02-11-2009, 01:08 AM
Deleting the macfobutton is for sure and is intended behavior.
And yes it does seem to delete the bookmark, which is unintended behavior.

fumei
02-11-2009, 10:02 AM
Inserting anything into a bookmark range - by default - deletes the bookmark. Microsoft states that the range expands to include the insertion. This is not true.

You need to insert into the range of the bookmark (which will delete the bookmark), then make a NEW bookmark of range and give it the same original name.

There are numerous threads which have this code.

IkEcht
02-25-2009, 02:09 AM
Sorry I have to return to this thread once more. I'm clearly more used to excel-vba. Word just is not my thing. But it looks like the code I use right now still doesn't work. The error mentions

Fout -2147024809 (80070057) tijdens uitvoering:
De index van de opgegeven verzameling valt buiten het bereik.

(sorry Dutch version ;) loose translation):
Error -2147024809 (80070057) while running:
The index of the collection is out of reach

Below is the code I use:

Sub verpltabel()
Dim i As Integer
Dim bmRange As Range

Set bmRange = ActiveDocument.Bookmarks("overzichtstabel").Range

xl.Sheets("verplaatsingen").Select
xl.Range("a1:r28").Select
xl.Selection.Copy
bmRange.PasteSpecial link:=False, DataType:=wdPasteBitmap, _
Placement:=wdInLine, DisplayAsIcon:=False

ActiveDocument.Bookmarks.Add Name:="overzichtstabel", Range:=bmRange

overzichtstabeldraaien

End Sub


and

Sub overzichtstabeldraaien()
Dim oShape As Shape
Set oShape = ActiveDocument.Bookmarks("overzichtstabel").Range.ShapeRange(1)

oShape.IncrementRotation -90

End Sub

I hope you can help me out, it probably is something stupid I've forgotten.