使用 Access 数据的 Word 书签模板
Word bookmarks template using Access data
我有一个带有书签的 Word 模板。这些书签通过 VBA 代码从 Access 数据库应用程序中提取数据。
On Error GoTo ErrHandler
Me.Recalc
If Me!txtCount = 0 Then
MsgBox "Please select a record to print.", vbOKOnly, "Error"
Else
Dim oWord As Object 'Word.Application
Dim doc As Object 'Word.Document
Set oWord = CreateObject("Word.Application")
Set doc = oWord.Documents.Open("C:\Request_Template.doc")
oWord.Visible = True
Dim oAccess As Object
Dim dbs As Database
Dim rst As Recordset
Dim strCriteria As String
With oWord.ActiveDocument
If .Bookmarks.Exists("DatePage1") = True Then
.Bookmarks("DatePage1").Select
If Not IsNull([Forms]![frmForRequest_Preview]!Date) Then
oWord.selection.Text = (CStr(Format([Forms]![frmForRequest_Preview]!Date, "mmm d, yyyy")))
Else
oWord.selection.Text = ""
End If
End With
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
问题是如何打开模板的副本以允许用户在查看文档后单击 "Save"?现在使用原始模板,用户必须执行 "Save As"。那不方便。
"Template" 在 Word 中是一种特定的文件类型(.dot、.dotx 或 .dotm)。目前,您没有 Word "template",只有标准的 Word 文档 (.doc)。
在 Word 中打开此 .doc 并将其另存为“文档模板 (.dot)”。
现在,将行 Documents.Open 更改为 Documents.Add,引用新的 .dot 并更改参数以匹配 Add 方法的参数。
这将自动打开模板文件的副本,因此用户或您的代码永远不会有任何覆盖模板的危险。
但是请注意,"Save As" 仍然是必需的,因为这是一个新文档,但它会自动出现 - 用户不必考虑使用“另存为”。如果您根本不希望用户看到另存为,您的代码需要执行 Document.SaveAs 并且您需要知道文件路径和保存位置。
我有一个带有书签的 Word 模板。这些书签通过 VBA 代码从 Access 数据库应用程序中提取数据。
On Error GoTo ErrHandler
Me.Recalc
If Me!txtCount = 0 Then
MsgBox "Please select a record to print.", vbOKOnly, "Error"
Else
Dim oWord As Object 'Word.Application
Dim doc As Object 'Word.Document
Set oWord = CreateObject("Word.Application")
Set doc = oWord.Documents.Open("C:\Request_Template.doc")
oWord.Visible = True
Dim oAccess As Object
Dim dbs As Database
Dim rst As Recordset
Dim strCriteria As String
With oWord.ActiveDocument
If .Bookmarks.Exists("DatePage1") = True Then
.Bookmarks("DatePage1").Select
If Not IsNull([Forms]![frmForRequest_Preview]!Date) Then
oWord.selection.Text = (CStr(Format([Forms]![frmForRequest_Preview]!Date, "mmm d, yyyy")))
Else
oWord.selection.Text = ""
End If
End With
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
问题是如何打开模板的副本以允许用户在查看文档后单击 "Save"?现在使用原始模板,用户必须执行 "Save As"。那不方便。
"Template" 在 Word 中是一种特定的文件类型(.dot、.dotx 或 .dotm)。目前,您没有 Word "template",只有标准的 Word 文档 (.doc)。
在 Word 中打开此 .doc 并将其另存为“文档模板 (.dot)”。
现在,将行 Documents.Open 更改为 Documents.Add,引用新的 .dot 并更改参数以匹配 Add 方法的参数。
这将自动打开模板文件的副本,因此用户或您的代码永远不会有任何覆盖模板的危险。
但是请注意,"Save As" 仍然是必需的,因为这是一个新文档,但它会自动出现 - 用户不必考虑使用“另存为”。如果您根本不希望用户看到另存为,您的代码需要执行 Document.SaveAs 并且您需要知道文件路径和保存位置。