开发者

Visual Studio. How to copy record from database to word .doc and print it

开发者 https://www.devze.com 2023-03-10 08:21 出处:网络
In Visual studio 2010>New Project>Visual Basic>Windows>Windows forms Application, i have made a form (form1.vb) and a database (Local Database>\"Database1.sdf\") and a Table with 3 Columns (\"Name\",\

In Visual studio 2010>New Project>Visual Basic>Windows>Windows forms Application, i have made a form (form1.vb) and a database (Local Database>"Database1.sdf") and a Table with 3 Columns ("Name","City","Age").

I like to copy this 3 fields and paste to document "test1.doc" (open this with Ms Office or Open Office Writer). I have bookmarks ("PasteName", PasteCity", "PasteAge") in specified places in test1.doc .

How to make a button to open the document "test1.doc" and copy - paste this 3 items from table to doc and preview before print it? (not for save - only print preview and close without save after printing)

I have find this code for MS Office but didn't work in Visual Studio. I like something similar. (this code is for a doc Form Fields - I have Bookmarks in my doc).

Private Sub cmdPrint_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
Set appWord = GetObject(, "Word.Application")
Set appWord = New Word.Application
Set doc = appWord.Documents.Open("C:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldCustomerID").Result = Me!CustomerID
.FormFields(开发者_Python百科"fldCompanyName").Result = Me!CompanyName
.FormFields("fldContactName").Result = Me!ContactName
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
End Sub

Thanks programers people


This works for me. (button action)

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Print customer slip for current customer.
    Dim appWord As Word.Application
    Dim doc As Word.Document
    'Avoid error 429, when Word isn't open.
    On Error Resume Next
    Err.Clear()
    'Set appWord object variable to running instance of Word.
    appWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        'If Word isn't open, create a new instance of Word.
        appWord = New Word.Application
    End If
    doc = appWord.Documents.Open("D:\Test.docx", , True)
    doc.Visible()
    doc.Activate()
    With doc.Bookmarks
        .Item("Name").Range.Text = Me.NameID.Text
        .Item("City").Range.Text = Me.CityID.Text
    End With

    Dim dlg As Word.Dialog
    dlg = appWord.Dialogs.Item(Word.WdWordDialog.wdDialogFilePrint)
    dlg.Display()
    'doc.Printout
    doc = Nothing
    appWord = Nothing
    Exit Sub
     errHandler:
    MsgBox(Err.Number & ": " & Err.Description)
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消