LTSdinamico

Hi,
I need assisting with something very simple that I haven¡¯t been able to figure out.
Basically I am inserting info. from a DB to a MS Word doc. On one of the fields called <STEP> I have the steps with their related jpg images. That is:

¡°1.- Open New Doc. (DOC_IMG_01) 2.- Type Text. (DOC_IMG_02) 3.- Save File (DOC_IMG_03)¡±

What I am doing is a search on the word doc. for the name of the image and then replace the selected text for the image (rng.InlineShapes.AddPicture) but I am having problems with the format since I am unable to add a break line after the image is inserted.

Currently the doc is showing like this:
STEPS : ¡°1.- Open New Doc.

(DOC_IMG_01) 2.- Type Text.

(DOC_IMG_02) 3.- Save File

(DOC_IMG_03)¡±


And I want the format to be:

STEPS : ¡°1.- Open New Doc.

(DOC_IMG_01)

2.- Type Text.

(DOC_IMG_02)

3.- Save File

(DOC_IMG_03)¡±

Here is the code I am using:

for (int imgs = 1; imgs <= no_imgs; imgs++)

{ // GET NAME OF THE IMAGE

string image = reader[0].ToString() + "_0" + imgs.ToString();

//SEARCH METHOD.

Search_Text(image);

//GET POSITION OF THE TEXT

rng = rs_word_app.Selection.Range;

// INSERTING LINE BREAK BEFORE THE IMAGE

rng.Text = "\r" + imgs.ToString() + ". BEFORE \r \r";

rng.InsertParagraphAfter();

rng.Collapse(ref objCollapseEnd);

// BUILD file name and insert jpg

string FN = @"C:\IMAGES\" + image + ".jpg";

rng.InlineShapes.AddPicture(FN, ref rs_false, ref rs_true, ref missing);

//HERE IS WHERE I AM TRYING TO ADD THE NEXT BREAKLINE

// rng.Text = imgs.ToString() + " \r";

rng.InsertParagraphAfter();

rng.Collapse(ref objCollapseEnd);

}

THANKS!





Re: Visual Studio Tools for Office Insert IMG

Cindy Meister

I think what is happening is that inserting the picture doesn't extend the range to include it - the range stays before it in the text flow (to the left). Try putting another InsertParagraphAfter command line following the first rng.Collapse(ref objCollapseEnd), then do a CollapseStart. That should put you between the two paragraphs and inserting the picture should put it before that second paragraph.

You may want to work with two ranges. Create a second one and set it to the rng.Duplicate before you use CollapseEnd the first time. Collapse this second range after inserting the second paragraph and it should be just after the picture.






Re: Visual Studio Tools for Office Insert IMG

LTSdinamico

Hi Cindy ...
This what I end up doing, don't know if it's the best method:

for (int imgs = 1; imgs <= no_imgs; imgs++)

{

string image = reader[0].ToString() + "_0" + imgs.ToString();

Search_Text(image);

rng = rs_word_app.Selection.Range;

// Insert Breakline

rng.Text = "\r" ;

rng.Select();

rng.Collapse(ref objCollapseEnd);

string tester = @"C:\IMAGES\" + image + ".jpg";

// insert image

rng.InlineShapes.AddPicture(tester, ref rs_false, ref rs_true, ref missing);

// move the cursor right (rs_cursor handles the number)

rs_word_app.Selection.MoveRight(ref rs_units,ref rs_count, ref missing);
// insert a break line

rs_word_app.Selection.TypeParagraph();

}

// to set range to the current cursor position for the following text.

rng = rs_word_app.Selection.Range;






Re: Visual Studio Tools for Office Insert IMG

Cindy Meister

<<don't know if it's the best method>>

Not "best" because you use the Selection object where it wouldn't be strictly necessary. You can also use rng.MoveStart and move the start to the right (that will automatically move the end point as well), thus avoiding use of the Selection object.