Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddPicture and Range 1

Status
Not open for further replies.

WCSO

Programmer
Nov 7, 2005
25
US
I call Word to create a document and use find and replace to replace specified places with data I want. Now I want to insert a picture into a specified spot.

I use: oWord.ActiveDocument.Bookmarks("OffenderPhoto").Select to position the cursor, which works.

I then use: oWord.ActiveDocument.Shapes.AddPicture('c:\sor.jpg',.F.,.T.,,,75,75) to insert the picture and size it, which also works.

However, the picture is put at the beginning of the document. I don't know the exact position each time so I need to be able to position based on my bookmark. If I use InLineShapes.AddPicture, the picture ends up where I want, but I cannot size it. Shapes.AddPicture has a range parameter but I cannot figure out how to use it.

Any thoughts on how to accomplish this would be appreciated.
 

WSCO,

Here's how I do it.

Instead of using a bookmark, insert some place-holder text in the document, at the point where you want the image to appear. Say the text is: {{IMAGE}}.

Find the positions of the start and end of that text, and get a range object to it:

lnStart = thisform.oDoc.Characters(lnStartPos).Start
lnEnd =lnStart + LEN("{{IMAGE}}")
loRange = thisform.oDoc.Range(lnStart, lnEnd)

Delete the place-holder:

loRange.Delete

Then insert the graphic. You need to create a new range to do this:

loNewRange = thisform.oDoc.Range(lnStart-1, lnStart-1)
loNewRange.Shapes.AddPicture('c:\sor.jpg',.F.,.T.,,,75,75)

You should also be able to do this by using a range to the bookmark, rather than the place-holder, but when I tried to do that, it went wrong (can't remember the details). I also had trouble using a Find object to get the location of the image. It might have been because my document contained mailmerge fields, which I think was mucking things up.

Hope this helps.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I am testing with a prg file so I changed thisform.oDoc to oDoc where appropriate. The first thing that happens is I get an error Variable 'LNSTARTPOS' is not found..

 
Instead of selecting the bookmark, make a range from it:

oRange = oWord.ActiveDocument.Bookmarks("OffenderPhoto")

Then, you can use oRange in the AddPicture method.

In generally, any time you think about using the Select method, you're better off using a range.

Tamar
 

WCSO,

Sorry, I missed out a couple of line. These should appear before the code shown in my message:

lcText = UPPER(thisform.oDoc.content.text)
lnStartPos = AT("{{IMAGE}}", lcText)

The solution Tamar suggested is much simpler, and you should use that if it works for you. The reason I didn't use a bookmark is that, for some reason which I don't recall, it went wrong -- I think it was because my document also contained mailmerge fields, though I can't remember the details.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I would rather search for a keyword as I have been doing it this way for form letters. It is easier for the average user to create templates this way. I now get an error Member CONTENT does not evaluate to an object. I have pasted my test code below so perhaps my error will stand out. I am running this from a prg file from the command window.

oWord = createobject("Word.Application")
oDocument=oword.Documents.open("c:\sor.doc")

oWord.Selection.Find.ClearFormatting()

oWord.Selection.Find.Execute("<<Date>>",.F.,.T.,.F.,.F.,.F.,.T.,0,.T.,DATE(),2)

lcText = UPPER(oWord.content.text)
lnStartPos = AT("<<Photo>>", lcText)
lnStart = oWord.Characters("<<Photo>>").Start
lnEnd =lnStart + LEN("<<Photo>>")
loRange = oWord.Range(lnStart, lnEnd)

loRange.Delete

loNewRange = oWord.Range(lnStart-1, lnStart-1)
loNewRange.Shapes.AddPicture('c:\sor.jpg',.F.,.T.,,,75,75)

oWord.visible=.t.

Thanks.
 

Which line of code is generating the error?

Also, there's no point in having both the Find and my code for finding the position of the place-holder. They are both doing the same job. In fact, the following two lines:

oWord.Selection.Find.ClearFormatting()
oWord.Selection.Find.Execute .... etc.

are redundant, because they are setting a selection, whereas the rest of the code relies on a range object.

I suggest you comment out those two lines, and see what the result is.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
The oWord.Selection.Find.Execute ....etc. line was not related to the picture, it was placing the current date where needed in the document.

I made some syntax changes and get the following to open the Word document. The word <<Photo>> should be deleted but it is not. I figure I should get that to work before I try inserting the picutre again. I don't get an error running this. The location of <<Photo>> is within a table so I don't know if that is causing a problem.

oWord = createobject("Word.Application")
oDocument=oword.Documents.open("c:\sor.doc")

lcText = UPPER(oWord.ActiveDocument.content.text)
lnStartPos = AT("<<PHOTO>>", lcText)

lnStart=lnStartPos
lnEnd =lnStart + LEN("<<Photo>>")
loRange = oWord.ActiveDocument.Range(lnStart, lnEnd)
loRange.Delete

oWord.visible = .t.
 

The location of <<Photo>> is within a table so I don't know if that is causing a problem.

Yes, that would definitely cause a problem. It would prevent the string from being found, which means that lnStartPos would be 0, which means that the image would appear at the front of the document (which is what was happening originally, but for a different reason).

You will need to write some code to loop through all the table in the document, and, for each table, loop through all the cells, looking for the text.

Alternatively, go back to using the Find object, as you are doing for the date, but use Range.Find rather than Select.Find.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top