none
Word VSTO: Inserting an image and hitting “enter” causes image to change, then disappear RRS feed

  • Question

  • I have a small add-in whose only purpose is to insert an image into a document when a ribbon button is pressed.

    It is a Word 2010 VSTO add-in. I would attach the project, but I don't see where to attach a file. 

    I set up the add-in to have one ribbon button with text "Insert Image".  The code it executes is below.  

    private void _btnInsertImage_Click(object sender, RibbonControlEventArgs e)
            {
                Application app = ThisAddIn.ThisApplication;
                Selection selection = app.Selection;
    
                if (selection != null)
                {
                    selection.Range.InlineShapes.AddPicture("c:\\projects\\WordInsertImageBullet\\WordInsertImageBullet\\mylogo.png", false, true);
                    selection.ClearFormatting(); 
                    selection.MoveRight();
    
                    //now hit the Enter key - the image is shown smaller than before
                    //hit the Enter key again - the image disappears
                }
            }

    Run Word, and click the ribbon button that says “Insert Image”.  My image will appear.  The cursor is set to be to the right of the image.

    Now, when I hit Enter, the image will change size to smaller than original. 

    Hitting Enter again will cause the image to completely disappear. 

    I have also had a problem in some cases where adding an image in this way causes Word to think I am making a bulleted list, and hitting Enter after inserting the image causes a duplicate image to show below the original, and the “bullet” button on Word’s tool ribbon is shown as highlighted (i.e. the selection is covering a bulleted list.)

    So –what’s the right way to add an image from a file that doesn’t exhibit this behavior?  I’ve trying removing formatting from the paragraph, doing “EndKey”, and numerous other things, but nothing is working.

    Thanks.

    Don

    Monday, October 21, 2013 1:05 PM

Answers

  • Hi Don

    <<If you copy and paste the code I put into my original post, that is all the custom code there is, other than creating a static variable in my "ThisApplication" that can be referenced by the ribbon class.  

    Several of us here at my company see the same behavior, so I've ruled out some configuration issue on my machine. >>

    Then there must be something in your company's configuration...

    I copied your code to a test VSTO add-in with a Ribbon, created a mylogo.png file, started Word, clicked the button. It inserted the file, the insertion point was blinking right next to it. I pressed Enter once, twice. Nothing happened to the picture - only paragraph marks were inserted into the document.

    In your place, the next thing I'd do would be to go to the COM Add-ins and Addins (for Word template add-ins) dialog boxes and make sure no other add-ins are loaded. This will take care of everything except any add-ins that may be registered in the Local Machine hive of the Registry. You might want to check there, as well. If anything is present, set it to not load.

    Something non-standard is causing this behavior. I tried activating the Auto Format as You Type option I thought might be involved, but that didn't affect what I'm seeing here.

    You might also try asking in the Communities whether anyone there knows of a built-in setting that would cause this, just in case there's something new in automatic formatting or possibly a third-party tool  I haven't encountered: http://answers.microsoft.com/en-us/office/forum/word


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 22, 2013 7:21 AM
    Moderator

All replies

  • Hi Don

    I've never seen anything like that happen, and I've been using Word for over twenty years...

    What kind of formatting are you working with before you insert the InlineShape? Anything to do with bullets and numbering?

    Are you inserting the InlineShape into its own paragraph, or a paragraph with other things in it (text, for example)?

    This sounds like some kind of "AutoFormat As You Type" kicking in, but I've never come across an option that does as you describe...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 21, 2013 2:27 PM
    Moderator
  • There is no text or formatting in the document when the "Insert Image" function is run. I simply open Word, which opens a new document, and click the button, which runs the code I posted above.   If you copy and paste the code I put into my original post, that is all the custom code there is, other than creating a static variable in my "ThisApplication" that can be referenced by the ribbon class.  

    Several of us here at my company see the same behavior, so I've ruled out some configuration issue on my machine. 

    Full source code is below or I can zip up and send you the solution if you'd like. I could also videotape it if more evidence is needed.  :-)

    Don

    namespace WordInsertImageBullet
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void _btnInsertImage_Click(object sender, RibbonControlEventArgs e)
            {
                Application app = ThisAddIn.ThisApplication;
                Document document = app.ActiveDocument;
                Selection selection = app.Selection;
    
                if (selection != null)
                {
                    //does the same thing as below: document.InlineShapes.AddPicture("c:\\projects\\WordInsertImageBullet\\WordInsertImageBullet\\mylogo.png", false, true);
                    selection.Range.InlineShapes.AddPicture("c:\\projects\\WordInsertImageBullet\\WordInsertImageBullet\\mylogo.png", false, true);
                    selection.ClearFormatting(); 
                    selection.MoveRight();
    
                    //now hit the Enter key - the image is shown smaller than before
                    //hit the Enter key again - the image disappears
                }
            }
        }
    }

    //add-in code

    namespace WordInsertImageBullet
    {
        public partial class ThisAddIn
        {
    
            public static Word.Application ThisApplication { get; set; }
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                ThisApplication = Application; 
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
        
        }
    }
    


    Monday, October 21, 2013 5:01 PM
  • Hi Don

    <<If you copy and paste the code I put into my original post, that is all the custom code there is, other than creating a static variable in my "ThisApplication" that can be referenced by the ribbon class.  

    Several of us here at my company see the same behavior, so I've ruled out some configuration issue on my machine. >>

    Then there must be something in your company's configuration...

    I copied your code to a test VSTO add-in with a Ribbon, created a mylogo.png file, started Word, clicked the button. It inserted the file, the insertion point was blinking right next to it. I pressed Enter once, twice. Nothing happened to the picture - only paragraph marks were inserted into the document.

    In your place, the next thing I'd do would be to go to the COM Add-ins and Addins (for Word template add-ins) dialog boxes and make sure no other add-ins are loaded. This will take care of everything except any add-ins that may be registered in the Local Machine hive of the Registry. You might want to check there, as well. If anything is present, set it to not load.

    Something non-standard is causing this behavior. I tried activating the Auto Format as You Type option I thought might be involved, but that didn't affect what I'm seeing here.

    You might also try asking in the Communities whether anyone there knows of a built-in setting that would cause this, just in case there's something new in automatic formatting or possibly a third-party tool  I haven't encountered: http://answers.microsoft.com/en-us/office/forum/word


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 22, 2013 7:21 AM
    Moderator
  • Thanks for your reply.  I disabled all add-ins and still see the problem.  I'm not sure what default settings we might have that are causing the problem.

    I'll have to check deeper for a solution at this point. 

    Don

    Tuesday, October 22, 2013 1:11 PM