2017-08-26 49 views
0

我試圖將列表框中的所有文本都轉移到MS Word文檔中的特定位置,類似於書籤。我也嘗試在書籤中添加樣式,但只顯示第一行。列表框中的所有文本到MS Word中的特定位置文檔

我遇到的問題是我需要將ListBox的內容傳輸到Word文檔,並且沒有弄清楚如何做到這一點。

如果可以,請提供幫助。

這是我的代碼。

Imports Word = Microsoft.Office.Interop.Word 

Public Class Form1 
    #Region "dim" 
     Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName) 
     Dim WPath = IO.Path.Combine(exeDir.DirectoryName, "SampleReceipt.doc") 
     Dim Word As Word.Application 
     Dim Doc As Word.Document 
    #End Region 

    Private Sub NAR(ByVal o As Object) 
     Try 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(o) 
     Catch ex As Exception 
      o = Nothing 
     Finally 

     End Try 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Try 
      Word = New Word.Application 
      Word.Visible = True 
      Doc = Word.Documents.Open(WPath) 
      Word.ActiveDocument.Bookmarks("txtDate").Select() 
      Word.Selection.Text = (TextBox1.Text) 
      Word.ActiveDocument.Bookmarks("txtSchoolYear").Select() 
      Word.Selection.Text = (TextBox2.Text) 
      Word.ActiveDocument.Bookmarks("txtr1").Select() 
      Word.Selection.Text = List1.Text 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 
    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     List1.Items.Add("string1") 
     List1.Items.Add("string2") 
     List1.Items.Add("string3") 
    End Sub 

End Class 

回答

0

在一個表中創建項目的字符串,只需使用下面的函數

Private Function GetListBoxStrings() As String 
    Dim tempString As String = "" 
    For Each line As String In List1.Items 
     tempString = tempString & line 
    Next 
    Return tempString 
End Function 

,並用它liike所以

Word.ActiveDocument.Bookmarks("txtr1").Select() 
Word.Selection.Text = GetListBoxStrings() 
+0

嘿大衛!非常感謝你做的這些!代碼起作用。我也可以通過這樣做來弄清楚。 昏暗lngListCount只要 對於lngListCount = 0至List1.Items.Count - 1 Word.Selection.InsertBefore(文本:= List1.Items(lngListCount)&vbCrLf) Word.ActiveDocument.Bookmarks( 「txtr1」) .Select() Next 但你的更清晰。再次感謝! –

+0

!你好,先生,再見!我想知道如何將vbTab包含在word文檔中? –

相關問題