none
How to put a picture in Word table,but getting the names of picture from listbox. I have visual basic 2010 RRS feed

  • Question

  • Imports Word = Microsoft.Office.Interop.Word
    Public Class Form1

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim a As String = My.Resources.picture
            Dim b As String() = a.Split(vbNewLine)
            ListBox1.Items.AddRange(b)
        End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = ListBox1.SelectedIndex

            If i = -1 Then
                Exit Sub
            End If
            Dim n, m As Integer
            n = ListBox2.Items.Count
            m = n + 1
            ListBox2.Items.Add(m & "." & " " & ListBox1.Items(i))
        End Sub

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim WordApp As Word.Application = CreateObject("Word.Application")
            TextBox1.Text = ListBox2.Items.Count
            ' Novi word dokument
            Dim WordDoc As Word.Document = WordApp.Documents.Add()
            WordApp.Visible = True

            'Podesavanje velicine papira u ovom slucaju A4
            WordDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4


            'Podesavanje margina
            WordDoc.PageSetup.LeftMargin = WordApp.MillimetersToPoints(10)
            WordDoc.PageSetup.RightMargin = WordApp.MillimetersToPoints(10)
            WordDoc.PageSetup.BottomMargin = WordApp.MillimetersToPoints(15)
            WordDoc.PageSetup.TopMargin = WordApp.MillimetersToPoints(5)


            Dim Tabela1 As Word.Table = WordDoc.Tables.Add(WordDoc.Bookmarks.Item("\endofdoc").Range, TextBox1.Text, 3)
            Tabela1.Range.ParagraphFormat.SpaceAfter = 0


            For x As Integer = 0 To ListBox2.Items.Count - 1
                Dim Line = From a In ListBox2.Items(x).ToString.Split(" "c) Select a.Replace(Chr(10), "")
                If Line.Count = 2 Then
                    Tabela1.Cell(x + 1, 1).Range.Text = Line(0)
                    Tabela1.Cell(x + 1, 2).Range.Text = Line(1)
                End If
                    next
            With Tabela1
                .Style = "Table Grid"
            End With
        End Sub

    End Class

     

     

     


    I wont this in table.cell (3), from listbox2

     

    • Moved by Cindy Meister MVPModerator Wednesday, December 28, 2011 3:26 PM not using VSTO technology (From:Visual Studio Tools for Office)
    Wednesday, December 28, 2011 2:19 PM

Answers

  • Hi Falkon10,

     

    Thanks for posting in the MSDN Forum.

     

    I have some questions for your issue. Would you please clarify them:

     

    1.         Where are your picture resources stored in? In the following snippets I store my PNG image in my Resource file. There have 11 pictures in it.

    2.         Where is TextBox1 control? I can’t find it out.

    3.         Your Linq syntax isn’t correct. Please double check it.

     

    OK, I provide a snippet to approach your goals. I hope it can help you. If you have any questions, please feel free to let me know.

     

    Imports Word = Microsoft.Office.Interop.Word
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _
                               As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            For i As Integer = 1 To 11 Step 1
                ListBox1.Items.Add("Image" & CStr(i))
            Next
            ListBox1.SelectedIndex = 0
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
                                                  ByVal e As System.EventArgs) _
                                              Handles ListBox1.SelectedIndexChanged
            PictureBox1.Image = My.Resources.ResourceManager _
                .GetObject("Image" & CStr(ListBox1.SelectedIndex + 1))
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = ListBox1.SelectedIndex
            If i = -1 Then
                Exit Sub
            End If
            Dim n, m As Integer
            n = ListBox2.Items.Count
            m = n + 1
            ListBox2.Items.Add(m & "." & ListBox1.Items(i))
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button2.Click
            Dim objApplication As Word.Application = _
                CreateObject("Word.Application")
            Dim objDocument As Word.Document = objApplication.Documents.Add()
            objApplication.Visible = True
    
            objDocument.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4
            objDocument.PageSetup.LeftMargin = objApplication _
                .MillimetersToPoints(10)
            objDocument.PageSetup.RightMargin = objApplication _
                .MillimetersToPoints(10)
            objDocument.PageSetup.BottomMargin = objApplication _
                .MillimetersToPoints(15)
            objDocument.PageSetup.TopMargin = objApplication.MillimetersToPoints(5)
    
            Dim objTable As Word.Table = objDocument.Tables.Add( _
                objDocument.Bookmarks.Item("\endofdoc").Range, _
                ListBox2.Items.Count, 3)
            objTable.Range.ParagraphFormat.SpaceAfter = 0
            Dim folder = Environment.GetFolderPath _
                         (Environment.SpecialFolder.MyDocuments)
    
            For i As Integer = 0 To ListBox2.Items.Count - 1
                Dim List As String() = ListBox2.Items(i).ToString().Split(".")
                objTable.Cell(i + 1, 1).Range.Text = CStr(i + 1)
                objTable.Cell(i + 1, 2).Range.Text = List(1)
                Dim objBitmap As Bitmap = My.Resources.ResourceManager _
                                          .GetObject(List(1))
                objBitmap.Save(folder & "\" & List(1) & ".png")
                objTable.Cell(i + 1, 3).Range.InlineShapes _
                    .AddPicture(folder & "\" & List(1) & ".png")
                File.Delete(folder & "\" & List(1) & ".png")
                ListBox2.Items.Remove(i)
            Next
            ListBox2.Items.Clear()
            With objTable
                .Style = "Table Grid"
            End With
        End Sub
    End Class
    


     

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 30, 2011 5:12 AM
    Moderator
  • Hi Falkon10,

     

    I think the key issue is your Linq statement. I would recommend you use “Dim Line = From b In (From a In ListBox2.Items(i). ToString.Split("."c) Select a.Replace(Chr(10), "")) Select b.Replace(" ", "_")” instead of the original statement.

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 2, 2012 8:41 AM
    Moderator

All replies

  • Hi Falkon10,

     

    Thanks for posting in the MSDN Forum.

     

    I have some questions for your issue. Would you please clarify them:

     

    1.         Where are your picture resources stored in? In the following snippets I store my PNG image in my Resource file. There have 11 pictures in it.

    2.         Where is TextBox1 control? I can’t find it out.

    3.         Your Linq syntax isn’t correct. Please double check it.

     

    OK, I provide a snippet to approach your goals. I hope it can help you. If you have any questions, please feel free to let me know.

     

    Imports Word = Microsoft.Office.Interop.Word
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _
                               As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            For i As Integer = 1 To 11 Step 1
                ListBox1.Items.Add("Image" & CStr(i))
            Next
            ListBox1.SelectedIndex = 0
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
                                                  ByVal e As System.EventArgs) _
                                              Handles ListBox1.SelectedIndexChanged
            PictureBox1.Image = My.Resources.ResourceManager _
                .GetObject("Image" & CStr(ListBox1.SelectedIndex + 1))
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = ListBox1.SelectedIndex
            If i = -1 Then
                Exit Sub
            End If
            Dim n, m As Integer
            n = ListBox2.Items.Count
            m = n + 1
            ListBox2.Items.Add(m & "." & ListBox1.Items(i))
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button2.Click
            Dim objApplication As Word.Application = _
                CreateObject("Word.Application")
            Dim objDocument As Word.Document = objApplication.Documents.Add()
            objApplication.Visible = True
    
            objDocument.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4
            objDocument.PageSetup.LeftMargin = objApplication _
                .MillimetersToPoints(10)
            objDocument.PageSetup.RightMargin = objApplication _
                .MillimetersToPoints(10)
            objDocument.PageSetup.BottomMargin = objApplication _
                .MillimetersToPoints(15)
            objDocument.PageSetup.TopMargin = objApplication.MillimetersToPoints(5)
    
            Dim objTable As Word.Table = objDocument.Tables.Add( _
                objDocument.Bookmarks.Item("\endofdoc").Range, _
                ListBox2.Items.Count, 3)
            objTable.Range.ParagraphFormat.SpaceAfter = 0
            Dim folder = Environment.GetFolderPath _
                         (Environment.SpecialFolder.MyDocuments)
    
            For i As Integer = 0 To ListBox2.Items.Count - 1
                Dim List As String() = ListBox2.Items(i).ToString().Split(".")
                objTable.Cell(i + 1, 1).Range.Text = CStr(i + 1)
                objTable.Cell(i + 1, 2).Range.Text = List(1)
                Dim objBitmap As Bitmap = My.Resources.ResourceManager _
                                          .GetObject(List(1))
                objBitmap.Save(folder & "\" & List(1) & ".png")
                objTable.Cell(i + 1, 3).Range.InlineShapes _
                    .AddPicture(folder & "\" & List(1) & ".png")
                File.Delete(folder & "\" & List(1) & ".png")
                ListBox2.Items.Remove(i)
            Next
            ListBox2.Items.Clear()
            With objTable
                .Style = "Table Grid"
            End With
        End Sub
    End Class
    


     

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 30, 2011 5:12 AM
    Moderator
  • Texbox1 used to tell me the number of listbox2 and on the ground that creates a table.Thanks for the code.
    Friday, December 30, 2011 10:14 AM
  • I managed to make me read the names of pictures,but I have a problem with my image name has two or more words. 

     

    For example If we are in listbox, picture name is "Night bird", in Resources it is automatically named "Night_bird". And because "_" not to transfer the program to the table.

     

    Do you know how to solve it ? 

     

     

     

    Imports Word = Microsoft.Office.Interop.Word

    Imports System.IO
    Public Class Form1

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim a As String = My.Resources.picture
            Dim b As String() = a.Split(vbNewLine)
            ListBox1.Items.AddRange(b)
        End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim i As Integer = ListBox1.SelectedIndex

            If i = -1 Then
                Exit Sub
            End If
            Dim n, m As Integer
            n = ListBox2.Items.Count
            m = n + 1
            ListBox2.Items.Add(m & "." & "/" & ListBox1.Items(i))
        End Sub

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim WordApp As Word.Application = CreateObject("Word.Application")

            ' Novi word dokument
            Dim WordDoc As Word.Document = WordApp.Documents.Add()
            WordApp.Visible = True

            'Podesavanje velicine papira u ovom slucaju A4
            WordDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4


            'Podesavanje margina
            WordDoc.PageSetup.LeftMargin = WordApp.MillimetersToPoints(10)
            WordDoc.PageSetup.RightMargin = WordApp.MillimetersToPoints(10)
            WordDoc.PageSetup.BottomMargin = WordApp.MillimetersToPoints(15)
            WordDoc.PageSetup.TopMargin = WordApp.MillimetersToPoints(5)

            Dim Tabela1 As Word.Table = WordDoc.Tables.Add(WordDoc.Bookmarks.Item("\endofdoc").Range, ListBox2.Items.Count, 3)
            Tabela1.Range.ParagraphFormat.SpaceAfter = 0

            For x As Integer = 0 To ListBox2.Items.Count - 1
                Dim Line = From a In ListBox2.Items(x).ToString.Split("/"c) Select a.Replace(Chr(10), "")

                Dim objBitmap As Bitmap = My.Resources.ResourceManager.GetObject(Line(1))
                Dim folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                If Line.Count = 2 Then
                    Tabela1.Cell(x + 1, 1).Range.Text = Line(0)
                    Tabela1.Cell(x + 1, 2).Range.Text = Line(1)


                    objBitmap.Save(folder & "/" & Line(1) & ".jpg")
                    Tabela1.Cell(x + 1, 3).Range.InlineShapes.AddPicture(folder & "/" & Line(1) & ".jpg")
                    File.Delete(folder & "/" & Line(1) & ".jpg")
                    ListBox2.Items.Remove(x)

                End If
            Next



            With Tabela1
                .Style = "Table Grid"
            End With

        End Sub
    End Class



    • Edited by Falkon10 Friday, December 30, 2011 4:11 PM
    Friday, December 30, 2011 4:10 PM
  • Hi Falkon10,

     

    I think the key issue is your Linq statement. I would recommend you use “Dim Line = From b In (From a In ListBox2.Items(i). ToString.Split("."c) Select a.Replace(Chr(10), "")) Select b.Replace(" ", "_")” instead of the original statement.

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 2, 2012 8:41 AM
    Moderator