PDA

View Full Version : Solved: Insert image in bookmark



kroz
10-11-2010, 04:12 AM
hey guys,

I'm trying to create a macro that would automatically replace 24 bookmarked images in my document with new ones.
I've managed to find a piece of code that would insert the image in my bookmarks but the problem is that i can't resize it to fit my current size.

This is the code I've got so far:



With ActiveDocument

'Initializing the variables
m = 0
For i = 1 To 4
For k = 1 To 2
For j = 1 To 3

'This is where the name for the current image name is created and also the bookmark name

Var5 = Var1(k) & Var2 & Var3(i) & Var4(j)
BmkNm = "bkmk" & m
m = m + 1
'Loading the images in the document


If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.InlineShapes(1).Delete
BmkRng.InlineShapes.AddPicture FileName:=strCommon & Var5
BmkRng.InlineShapes(1).Height = 230.4
BmkRng.InlineShapes(1).Width = 236.15
.Bookmarks.Add BmkNm, BmkRng
End If
Next
Next
Next
End With
Set BmkRng = Nothing
ActiveDocument.SaveAs "Done.doc"
End Sub


I didn't include the variables definition but I've used them in another macro in Powerpoint so that part should be fine.
I must be doing something wrong with the height/width assignment in my macro..

fumei
10-12-2010, 09:05 AM
You can try doing your rewsizing after the bookmark is recreated, rather than before.


If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.InlineShapes(1).Delete
BmkRng.InlineShapes.AddPicture FileName:=strCommon & Var5
.Bookmarks.Add BmkNm, BmkRng
End If
with ActiveDocument.Bookmarks(BmkNm)
.InlineShapes(1).Height = 230.4
.InlineShapes(1).Width = 236.15
End With
Not tested.

kroz
10-12-2010, 10:43 PM
It errors out: Compile error "Method or data member not found"

Is there any other way of adding images to specific places and resize them to a certain height/width? From the looks of it, InlineShapes is kinda restrictive (Shapes method has .H/.W but InlineShapes doesn't)

fumei
10-14-2010, 10:59 AM
Ah, of course.

Try changing your InlineShape to a Shape?

kroz
10-14-2010, 11:42 PM
Yeah, the only problem is that all my pictures are inline and when i go from inlineshape to shape all my formatting goes down the drain. I guess it's the only way to go.

It's going to be a "fun" weekend.

kroz
10-15-2010, 04:12 AM
How about this, do you think it will work? I still need to think of a way to code it but for now i'm searching for alternatives to solve the problem.

I want to load all the my images (24) at the end of the document as shapes, modify them as i see fit (from the looks of it i need to right crop them two by two and resize them to 37%), transform them to InlineShapes and then move each of them to a specified bookmark. I can just delete all my current InlineShapes since i'm building a template and create blank bookmarks but i'm not sure i want to do that just yet.

Can i move the shapes to a specified bookmark?

fumei
10-15-2010, 09:03 AM
Shapes are, by definition, free-floating objects. They do have an anchor, but it is to a paragraph, not a bookmark. can it be done...honest answer is I do not know. Let me see.

kroz
10-18-2010, 03:19 AM
I had a small breakthrough today, when i was able to insert, resize and crop an image into a bookmark. Now i only have to delete the current selection in my bookmark (as in the image) so that i can replace it with a new one.
The code must be a bit sloppy because i can't delete the initial bookmark (when i try to delete the image, the code removes both images - new & old version).

Here's the code:


Sub retest()
Dim BmkNm As String
BmkNm = "BkNm1"
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
Dim WrdPic As Word.InlineShape
Set WrdPic = .Bookmarks(BmkNm).Range.InlineShapes.AddPicture("C:\My_Image.jpeg", False, True)
With WrdPic
.ScaleHeight = 37
.ScaleWidth = 37
.PictureFormat.CropBottom = 95.68
.PictureFormat.CropTop = 17.55
.PictureFormat.CropRight = 91.01
End With
End If
End With
End Sub


Any ideas on how to just replace and not delete the bookmark? I can go with the "delete the bookmark" if i get to keep the image in the position i need, i can use a SaveAs at the end of my template.

Tinbendr
10-18-2010, 05:34 AM
Any ideas on how to just replace and not delete the bookmark?
Have a look at this (http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm).

kroz
10-18-2010, 06:10 AM
This:

If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
Dim WrdPic As Word.InlineShape
Set WrdPic = BmkRng.InlineShapes.AddPicture(strCommon & Var5, False, True)
With WrdPic
.PictureFormat.CropRight = Pcrop
.ScaleHeight = 37
.ScaleWidth = 37
End With
.Bookmarks.Add BmkNm, BmkRng
End If


inserts all my images at the right location, without deleting the initial images. Which doesn't help much since i have to replace the old images.

This:


If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.InlineShapes(1).Delete
.Bookmarks.Add BmkNm, BmkRng
Dim WrdPic As Word.InlineShape
Set WrdPic = BmkRng.InlineShapes.AddPicture(strCommon & Var5, False, True)
With WrdPic
.PictureFormat.CropRight = Pcrop
.ScaleHeight = 37
.ScaleWidth = 37
End With
End If

*Pcrop, strCommon, Var5 and BmkNm are variables declared earlier

deletes some of the bookmarks (not all of them) and replaces them with new images. Which is also not acceptable.

Can it be a formatting problem? I bookmarked all my 24 images (i used InlineShapes for to format my file)

kroz
11-29-2010, 06:04 AM
Any ideas? I'm still trying to figure this out..

macropod
11-29-2010, 03:01 PM
Hi kroz,

Try:
Dim WrdPic As Word.InlineShape
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
With BmkRng
While .InlineShapes.Count > 0
.InlineShapes(1).Delete
Wend
Set WrdPic = .InlineShapes.AddPicture(strCommon & Var5, False, True)
With WrdPic
.PictureFormat.CropRight = Pcrop
.ScaleHeight = 37
.ScaleWidth = 37
End With
End With
.Bookmarks.Add BmkNm, BmkRng
End If
The While-Wend loop takes care of bookmarks from which the inline shape might be missing and bookmarks that might contain more than one inline shape. If you're having problems with bookmarks being deleted, that suggests you may have some of them nested or overlapping - you need to ensure they all refer to discrete ranges.

kroz
11-29-2010, 11:51 PM
Excelent coding, thank you Macropod. It works

kroz
11-30-2010, 05:06 AM
hm..that doesn't really work. I thought it did but i was wrong.
I tried something very simple, i created 6 images (named 1.jpg to 6.jpg) and bookmarked them all with bkmk1-bkmk6. Then i ran this code:

Sub Macro1()
'
Dim strCommon As String
Dim strVar As String
Dim i, j, k, l, m As Integer
Dim opic As Shape
Dim BkMkn As String
Dim BmkRng As Range
Dim BmkTmp As Bookmark

'Declaring the variables used in cycling the pictures

m = 0
strCommon = BrowseFolder("OUTPUT")

With ActiveDocument

'Initializing the variables
For j = 6 To 1 Step -1

'This is where the name for the current image name is created and also the bookmark name

m = m + 1
BmkNm = "bkmk" & m

'Loading the images in the document

Dim WrdPic As Word.InlineShape
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
With BmkRng
While .InlineShapes.Count > 0
.InlineShapes(1).Delete
Wend
Set WrdPic = .InlineShapes.AddPicture(strCommon & j & ".jpg", False, True)
With WrdPic
.PictureFormat.CropRight = 93.6
.PictureFormat.CropBottom = 36#
.Height = 223.9
.Width = 210.95

End With
End With
.Bookmarks.Add BmkNm, BmkRng
End If
Next
End With
Set BmkRng = Nothing
End Sub


And all i got was the last image..

P.S. the code is messy, i copied it from my original file and i may have missed deleting some parts