繁体   English   中英

使用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"

问题是如何打开模板的副本,以允许用户在查看文档后单击“保存”? 现在,使用原始模板,并且用户必须执行“另存为”。 那不方便。

Word中的“模板”是一种特定的文件类型(.dot,.dotx或.dotm)。 就目前而言,您没有Word“模板”,只有标准的Word文档(.doc)。

在Word中打开此.doc并将其另存为“文档模板(.dot)。

现在,将行Documents.Open更改为Documents.Add,引用新的.dot并更改参数以使其与Add方法的参数匹配。

这将自动打开模板文件的COPY,因此永远不会对用户或您的代码覆盖模板造成任何危险。

但是请注意,由于这是一个新文档,仍需要“另存为”,但是它将自动出现-用户将不必考虑使用“另存为”。 如果您不希望用户看到“另存为”,则您的代码需要执行Document.SaveAs,并且您需要知道应将其保存到的文件路径和位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM