将表单字段添加到Word .doc书签吗? [英] Add form fields to word .doc bookmarks?

查看:88
本文介绍了将表单字段添加到Word .doc书签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010>新建项目> Visual Basic> Windows> Windows窗体应用程序中,我已经创建了一个窗体(form1.vb)和一个数据库(Local Database>"Database1.sdf")和一个具有3列的表( 名称",城市",年龄").
我喜欢复制这3个字段并粘贴到文档"test1.doc"(使用Office女士或Open Office Writer打开).我在test1.doc的指定位置有书签("PasteName",PasteCity","PasteAge").
如何制作一个按钮来打开文档"test1.doc"并复制-在打印之前将这3个项目从表格粘贴到doc并进行预览? (不适用于保存-仅打印预览并在打印后关闭而不保存)

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)
which object libraries must enable?

推荐答案

如果您在计算机上安装了MS Word,则可以这样做(VB/VBA代码):

If you have installed MS Word on your computer, you can do it like this (VB/VBA code):

Sub OpenAndPasteInBkm(ByVal sFileName As String)
Dim oApp As Object
Dim oBkm As Object
Dim oDoc As Object

On Error GoTo Err_OpenAndPasteInBkm

Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Open(sFileName)
Set oBkm = oDoc.Bookmarks("PasteName")
bkm.Range.Text = "Name"
Set bkm = doc.Bookmarks("PasteCity")
bkm.Range.Text = "City"
Set bkm = doc.Bookmarks("PasteAge")
bkm.Range.Text = "Age"

oApp.Visible = True
''here code to print preview and close document and application

Exit_OpenAndPasteInBkm:
    Set oApp = Nothing
    Set oBkm = Nothing
    Set oDoc = Nothing
    Exit Sub

Err_OpenAndPasteInBkm:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_OpenAndPasteInBkm
End Sub


这篇关于将表单字段添加到Word .doc书签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆