Click here to Skip to main content
15,926,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am having one task that is
i am having one button named "saveas"
when i click this button the current image on the screen have to be saved in msword dynamically.
here my image is nothing but my windows form
i created msword document dynamically but i am not able to save that image in to that msword document so please help me my code is here like this


C#
private void sAVEASToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            
         //|JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif
             string myStream; 
           SaveFileDialog saveFileDialog1 = new SaveFileDialog();
         saveFileDialog1.Filter = "Word document (*.doc)|*.doc|JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
                saveFileDialog1.FilterIndex = 1; 
                saveFileDialog1.Title = "Save Data File As";
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.AddExtension = true;
                saveFileDialog1.CheckPathExists = true;
                Graphics g1 = this.CreateGraphics();
                Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
                Graphics g2 = Graphics.FromImage(MyImage);
                IntPtr dc1 = g1.GetHdc();
                IntPtr dc2 = g2.GetHdc();
                BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //if ((myStream = saveFileDialog1.OpenFile()) != null)
                    //{
                    //    StreamWriter wText = new StreamWriter(myStream);
                    //    wText.Write(" saveFileDialog1.OpenFile()"); 
                    //    myStream.Close();
                    //}
                    myStream = saveFileDialog1.FileName;
                    if (saveFileDialog1.FileName != "")
                    {
                        System.IO.Stream fileStream = (System.IO.FileStream)saveFileDialog1.OpenFile();
                        MyImage.Save(fileStream, ImageFormat.Jpeg);
                        fileStream.Close();
                        object oMissing = System.Reflection.Missing.Value;
                        object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                        //Start Word and create a new document.
                        Word._Application oWord;
                        Word._Document oDoc;
                        oWord = new Word.Application();
                        oWord.Visible = true;
                        oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing);
                        //Word.InlineShape oShape;
                        //object oClassType = "MSGraph.Chart.8";
                        //wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        //oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,
                        //    ref oMissing, ref oMissing, ref oMissing,
                        //    ref oMissing, ref oMissing, ref oMissing);
                       
                    }
                }
}
}
Posted
Updated 17-Feb-10 3:48am
v2

 
Share this answer
 
I have not tried this but you could consider these options:

1. Look at VSTO (Visual Studio Tools for Office)
2. Push the image into your clipboard and have Word use an Auto_Open macro that takes the clipboard image to be inserted
3. Google ?

and your English is not that bad.
 
Share this answer
 
MIDL
object objIndex = "附件";
               Microsoft.Office.Interop.Word.Range rangeAttachment = _WordDoc.Bookmarks.get_Item(ref objIndex).Range;

               object ClassType = objNull; // ClassType:="Package"
               object FileName = attachmentPathFileName;
               object LinkToFile = true;
               object DisplayAsIcon = true;
               object IconFileName = attachmentPathFileName;
               object IconIndex = 0;
               object IconLabel = "附件名称【点击查看" // "显示的标签名称,在这里应该是附件的名称";
               object Range = objNull;

               rangeAttachment.InlineShapes.AddOLEObject(
                   ref ClassType, ref FileName, ref LinkToFile, ref DisplayAsIcon,
                   ref IconFileName, ref IconIndex, ref IconLabel, ref Range);

               _WordDoc.Save();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900