none
Save images on word first in first out basis C# RRS feed

  • Question

  •    

    Hi below code take snapshot and save in word and append on existing word.. when I execute this code its saving the image on first in last out basis means first image pushed down when I took new screenshot is there any possibilities to make it has first in first out means when I took image its store on top of the document and remaining needs to store in down...

     

    Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot as Image);
                gfxScreenshot.CopyFromScreen(0, 0, 0, 0, bmpScreenshot.Size);
                Clipboard.SetImage(bmpScreenshot);  

                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Open(@"F:\sam\test.docx", ReadOnly: false, Visible: true);
                oWord.Visible = true;        
                object start = 0;
                object end = 0;
                Microsoft.Office.Interop.Word.Range rng = oDoc.Range(ref start, ref end);        
                rng.Paste();

                rng.InsertParagraph();
                object oEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                rng.Collapse(ref oEnd);
               
                oDoc.Save();
                Clipboard.Clear();
                oDoc.Close();
                oWord.Quit();

    • Moved by Kristin Xie Friday, December 25, 2015 1:36 AM move to appropriate forum
    Thursday, December 24, 2015 3:55 PM

Answers

  • Hi Swamy

    Did you try the code I suggested in my reply in this discussion? How does that not work? It's very similar to what you already have, the only difference is that you need to paste AFTER going to the end of the document, not the other way around, as in your sample code.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Swamy R S Thursday, December 31, 2015 7:31 AM
    Tuesday, December 29, 2015 6:51 PM
    Moderator
  • Hi Swamy,

    Pease add the logical to check whether oDoc.InlineShapes.Count is 0 or not (if… else…), if it is 0, please add the picture to the top of the document (the code you provided), if not, then use the code that I provided.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Swamy R S Thursday, December 31, 2015 7:31 AM
    Wednesday, December 30, 2015 1:30 AM
    Moderator

All replies

  • Hi Swamy R S,

    This forum is about the C# programming language, IDE, libraries, samples, and tools, as your issue is more related to the Word development, we help you move it to the Word for Developers forum for better support.

    Thank you for your understanding.

    Best Regards,

    Albert Zhang

    Friday, December 25, 2015 1:33 AM
  • hi 

    i am trying to access word document through c#.. but i need correct function to in insert image in word properly 

    Friday, December 25, 2015 5:17 AM
  • need c# function to access word  its not only related to word  kindly see above program its written in c#
    Friday, December 25, 2015 5:19 AM
  • Based on your problem description, you want to insert the new image at the END of the document. You have all the pieces already, you just need to put them together :-)

                Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Open(@"F:\sam\test.docx", ReadOnly: false, Visible: true);
                oWord.Visible = true;         
                Microsoft.Office.Interop.Word.Range rng = oDoc.Content;
                rng.Collapse(ref oEnd);
                 rng.Paste();


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Saturday, December 26, 2015 6:36 PM
    Moderator
  • Hi Swamy,

    According to your description, I don’t understand clearly about your requirement.

    If you want to add image at the end of the document, please refer to Cindy’s reply.

    If you want to add the image below the last image, you could get last InlineShape’s range, then add image.

    For example:

     object start = 0;
                object end = 0;
                //Microsoft.Office.Interop.Word.Range rng = oDoc.Range(ref start, ref end);
                Microsoft.Office.Interop.Word.Range rng = oDoc.InlineShapes[oDoc.InlineShapes.Count].Range;
                object oEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                rng.Collapse(ref oEnd);
                rng.Paste();
    
                //rng.InsertParagraph();
                //object oEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                //rng.Collapse(ref oEnd);
    
                oDoc.Save();
                Clipboard.Clear();
                oDoc.Close();
                oWord.Quit();

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 28, 2015 2:43 AM
    Moderator
  • Below code provided is not working its giving error after run the program  yes I need to insert below the image which previous pated

    object start = 0;
               
    object end = 0;
               
    //Microsoft.Office.Interop.Word.Range rng = oDoc.Range(ref start, ref end);
               
    Microsoft.Office.Interop.Word.Range rng = oDoc.InlineShapes[oDoc.InlineShapes.Count].Range;
               
    object oEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                rng
    .Collapse(ref oEnd);
                rng
    .Paste();

               
    //rng.InsertParagraph();
               
    //object oEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
               
    //rng.Collapse(ref oEnd);

                oDoc
    .Save();
               
    Clipboard.Clear();
                oDoc
    .Close();
                oWord
    .Quit();

    Tuesday, December 29, 2015 6:51 AM
  • Hi Swamy,

    Which line it throws error?

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 29, 2015 8:42 AM
    Moderator
  • Microsoft.Office.Interop.Word.Range rng = oDoc.InlineShapes[oDoc.InlineShapes.Count].Range;

    This line throws error

    Tuesday, December 29, 2015 8:57 AM
  • Hi Cindy

     

    In below link which you provided example of taking screenshot and past in word and mention target range is beginning of the document..  but I am seeking the code paste one after another downwards the document  Please advice below code is also not working. 

    https://social.msdn.microsoft.com/Forums/office/en-US/036e0f31-7829-4e6f-8495-d15830f2205e/how-to-automate-inserting-pictures-one-after-the-other-in-ms-word-document-using-c?forum=worddev

    Tuesday, December 29, 2015 9:59 AM
  • Hi Swamy

    Did you try the code I suggested in my reply in this discussion? How does that not work? It's very similar to what you already have, the only difference is that you need to paste AFTER going to the end of the document, not the other way around, as in your sample code.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Swamy R S Thursday, December 31, 2015 7:31 AM
    Tuesday, December 29, 2015 6:51 PM
    Moderator
  • Hi Swamy,

    Pease add the logical to check whether oDoc.InlineShapes.Count is 0 or not (if… else…), if it is 0, please add the picture to the top of the document (the code you provided), if not, then use the code that I provided.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Swamy R S Thursday, December 31, 2015 7:31 AM
    Wednesday, December 30, 2015 1:30 AM
    Moderator