PDA

View Full Version : Range wdCollapseEnd not working



aj345
11-22-2011, 11:31 AM
Can anyone explain to me why these two pieces of code have the same effect?

What I want is:
text before the image

< the image >

text after the image

But the resulting doc looks like this, in order:

< the image >

text before the image

text after the image

I've tried it the following two ways:

Sample 1 (with collapse ):
docWordObj.Range.InsertAfter vbLf & "text before the image" & vbLf & vbLf
docWordObj.Range.Collapse Direction:=wdCollapseEnd
docWordObj.Range.InlineShapes.AddPicture (imagepath & imageFileName)
docWordObj.Range.Collapse Direction:=wdCollapseEnd
docWordObj.Range.InsertAfter "text after the image" & vbLf & vbLf

Sample 2 (with collapse commented out):
docWordObj.Range.InsertAfter vbLf & "text before the image" & vbLf & vbLf
' docWordObj.Range.Collapse Direction:=wdCollapseEnd
docWordObj.Range.InlineShapes.AddPicture (imagepath & imageFileName)
' docWordObj.Range.Collapse Direction:=wdCollapseEnd
docWordObj.Range.InsertAfter "text after the image" & vbLf & vbLf

I have been staring at this for hours. The whole range thing is killing me. What am I missing?! Thanks in advance!

Tinbendr
11-22-2011, 12:48 PM
I don't know why collapse doesn't work properly, but this code works.
Sub TEST()
Dim docWordObj As Document
Dim Rng As Range
Set docWordObj = ActiveDocument
Set Rng = docWordObj.Range
ImagePath = "C:\Documents and Settings\owner\My Documents\My Pictures\"
ImageFilename = "MyPicture.jpg"
With Rng
.InsertAfter vbLf & "text before the image" & vbLf & vbLf
.Collapse Direction:=wdCollapseEnd
.InlineShapes.AddPicture (ImagePath & ImageFilename)
.MoveEnd wdWord
.InsertAfter "text after the image" & vbLf & vbLf
End With

End Sub

aj345
11-22-2011, 03:57 PM
Thanks - I don't know why either. I'm using Office 2010, you? Thanks again for your help.

Tinbendr
11-22-2011, 05:19 PM
I'm using Office 2010, you? Word 2007. Your code didn't work properly in Word 2007 either.

aj345
12-01-2011, 11:14 AM
the only difference is the substitution of

<range>.MoveEnd wdWord

instead of

<range>.Collapse Direction:=wdCollapseEnd

The collapse works the first time, but after the image is inserted, it doesn't. It's also a problem when inserting a table.

Frosty
12-01-2011, 12:24 PM
Check the help files... some actions which insert things extend the range at the same time, some don't.

If you step through code, and then use the immediate window to see what your range is using .Select, it will help to visualize what the range is before and after an action.

Without testing the code, my guess is that .AddPicture doesn't redefine the range, whereas .InsertAfter does.

This can be especially tricky when you are working on a range which is the entire document... you may need to manually redefine your range after using a particular method.

aj345
12-01-2011, 02:09 PM
That makes sense. Since AddPicture doesn't expand the range, the collapse has no effect; the end is still before the image. What do you mean by "help files?" I've been using MSDN but it is not always helpful. Thanks for your help.

Frosty
12-02-2011, 02:35 PM
I mean the help files available by pressing F1 within Word's VBA when your cursor is in the middle of a particular property or method... when you're trying to learn about a method, the immediate help can often explain it well.

For example, the helpfile on the InlineShapes.AddPicture Method explains that there are 4 different parameters (FileName, LinkToFile,SaveWithDocument, Range).

The range object is optional... and has the following description:
"The location where the picture will be placed in the text. If the range isn't collapsed, the picture replaces the range; otherwise, the picture is inserted. If this argument is omitted, the picture is placed automatically."

Since you're omitting the argument, you're getting whatever Microsoft considers is "automatic placement" -- however, I bet you can include the range argument and maybe get closer to what you want.

I think your particular issue is solved-- just trying to add to the "how to help yourself" part of the thread.

Cheers!