none
VB.Net 2013 - InlineShape.AddPicture Issue RRS feed

  • Question

  • Dear All

    I have a VB.net application with 2 forms. A main frame to allow user data imput and a result frame to show results. I wanted to export analysed data to Word. So I used tutorial about Automating Word. But I have some issues with adding picture.

    First of all, my first window has a PictureBox which displays images from Resources according to a ComboBox.SelectedIndex control.

    I want to add this picture to my Word Document but I couldn't achieve it. I have read some instructions on forums and some people suggested to save PictureBox image to Temp folder (which I did) and then load it from there. But this time it adds the picture to the top of the page before title.

    So, how can I use InlineShapes.AddPicture to insert the image to the right position? And is there an easier way to use PictureBox image?

    Best.

    Imports Word = Microsoft.Office.Interop.Word Public Class WF_Result_Frame Private Sub tsKaydetButonu_Click(sender As Object, e As EventArgs) Handles tsKaydetButonu.Click Dim oWord As Word.Application Dim oDoc As Word.Document Dim oTable As Word.Table Dim oPara1 As Word.Paragraph Dim oPara2 As Word.Paragraph Dim oPara4 As Word.Paragraph Dim oPara5 As Word.Paragraph Dim oPara3 As Word.Paragraph Dim oParaPic As Word.Paragraph Dim oPicture As Word.InlineShape

    ' Save image to TEMP folder so I can use a file location string Dim SavePictureToTemp As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "MY_Anchor_Pic_Handled") WF_Main_Frame.PXBX_Ankraj_Adedi_Plaka_Ozellikleri.Image.Save(SavePictureToTemp, System.Drawing.Imaging.ImageFormat.Png) Dim oRng As Word.Range Dim Pos As Double 'Start Word and open the document template. oWord = CreateObject("Word.Application") oWord.Visible = True oDoc = oWord.Documents.Add ' Main title and settings here oPara1 = oDoc.Content.Paragraphs.Add oPara1.Range.Text = tsTXTBX_Proje_Adi.Text oPara1.Range.Font.Position = 0 oPara1.Range.Font.Bold = True oPara1.Range.Font.Size = 24 oPara1.Range.Font.Name = "Segoe UI" 'oPara1.Range.Font.ColorIndex = "91;155;213" oPara1.Format.SpaceAfter = 8 '10 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter() 'Insert a paragraph at the end of the document. '** \endofdoc is a predefined bookmark. oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range) oPara2.Range.Text = (tsTXTBX_Kolon_Adi.Text & " Column Anchor Bolt and Base Plate Design ") oPara2.Range.Font.Bold = True oPara2.Range.Font.Size = 18 oPara2.Format.SpaceAfter = 6 oPara2.Range.InsertParagraphAfter() oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddPicture(SavePictureToTemp) oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range) oPara3.Range.Text = "Anchor Bolt and Base Plate Properties" oPara3.Range.Font.Bold = False oPara3.Range.Font.Italic = True oPara3.Range.Font.Size = 11 oPara3.Range.Font.Name = "Segoe UI" oPara3.Format.SpaceAfter = 4 oPara3.Range.InsertParagraphAfter() oRng = oDoc.Bookmarks.Item("\endofdoc").Range oRng.InsertParagraphAfter() oRng.InsertAfter("THE END.") End Sub


    Wednesday, August 27, 2014 3:26 PM

Answers

  • H ramis

    Since you don't show us any code for inserting the picture it's impossible to say very much about the problem you describe. The only recommendation, at this point, would be for you to record a macro while inserting the picture to get the basic syntax (and to test that Word can actually insert the picture). Any my best guess would be that the problem may be that you're not specifying a target RANGE for the parameter provided for that purpose.

    Generally, looking at your code, I would recommend you look into defining and using STYLES to apply formatting to the content you're generating. And perhaps provide these (and any boiler-plate the document will "always" contain) in a template you distribute with your application.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by ramis.e Saturday, August 30, 2014 3:40 PM
    Thursday, August 28, 2014 4:14 PM
    Moderator

All replies

  • Greetings,

    I am moving this question to Word for developers forum. See if they can assist and if not this thread can be moved back to the VB.NET forum.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, August 27, 2014 6:35 PM
  • H ramis

    Since you don't show us any code for inserting the picture it's impossible to say very much about the problem you describe. The only recommendation, at this point, would be for you to record a macro while inserting the picture to get the basic syntax (and to test that Word can actually insert the picture). Any my best guess would be that the problem may be that you're not specifying a target RANGE for the parameter provided for that purpose.

    Generally, looking at your code, I would recommend you look into defining and using STYLES to apply formatting to the content you're generating. And perhaps provide these (and any boiler-plate the document will "always" contain) in a template you distribute with your application.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by ramis.e Saturday, August 30, 2014 3:40 PM
    Thursday, August 28, 2014 4:14 PM
    Moderator
  • Hi Cindy;

    I use this code to insert the picture;

    oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddPicture(SavePictureToTemp)

    I actually did it and inserted the picture by defining range as you said. Now it works fine. But still a little hard to use picturebox image. Any way, it's okay for now.

    Thank you.

    Saturday, August 30, 2014 3:40 PM