C# - How do I select the InlineShape that I just dropped into Word

C

ChrisW123

Hi Everyone,

I'm hoping someone here can help me. Using C#, I am dragging/dropping a
..png image into a Word document using the code below. This works fine,
creating an InlineImage in my document which contains the image.

My problems are:

1) After completing the drop, I need to be able to access the InlineShape
that was created, in order to set the .AlternateText property. How do I
reference the newly created InlineShape?

2) After completing the drop, the Font of the text near the image changes to
Times Roman for some reason. How do I prevent this from happening and
preserve the original font?

Any help is greatly appreciated, thanks in advance.

private void borSignaturePic_MouseMove(object sender, MouseEventArgs
e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Add our .png image to the Clipboard
Clipboard.SetImage(Bitmap.FromFile(@"C:\MyImage.png"));

// Copy the image data to a Rich Text object
RichTextBox rtb = new RichTextBox();
rtb.Paste();

// Build the drag/drop Item
object item = new DataObject("Rich Text Format",
rtb.Rtf);

// Drag item to Word, creating an InlineShape with the
image in Word at the
// point of the cursor.
DragDropEffects dropEffect =
borSignaturePic.DoDragDrop(item, DragDropEffects.Copy);

// Release our Rich Text object
rtb.Dispose();
}
}


Any help at all is appreciated.

Thanks, -Chris W.
 
C

ChrisW123

I figured out the answer to #1, you select the InlineShape after the drop
like this:

// Save the tag name to our InlineShape
InlineShape shape =
this.myWordApp.Selection.InlineShapes.Item(0);
shape.AlternativeText = "asdf";
shape.Select();

If anyone has any input on item #2 below that would be great!

Thanks, -Chris W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top