简体   繁体   English

使用Access数据的Word书签模板

[英]Word bookmarks template using Access data

I have a Word template with bookmarks. 我有一个带书签的Word模板。 These bookmarks pull data from an Access database application via VBA code. 这些书签通过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"

The question is how to open a copy of the template to allow the user to click on "Save" after reviewing the document? 问题是如何打开模板的副本,以允许用户在查看文档后单击“保存”? For now the original template is used and the user has to perform "Save As". 现在,使用原始模板,并且用户必须执行“另存为”。 That is not convenient. 那不方便。

"Template" in Word is a specific file type (.dot, .dotx or .dotm). Word中的“模板”是一种特定的文件类型(.dot,.dotx或.dotm)。 As it stands, you don't have a Word "template", just a standard Word document (.doc). 就目前而言,您没有Word“模板”,只有标准的Word文档(.doc)。

Open this .doc in Word and save it as a "Document Template (.dot). 在Word中打开此.doc并将其另存为“文档模板(.dot)。

Now, change the line Documents.Open to Documents.Add, referencing the new .dot and changing the parameters to match those of the Add method. 现在,将行Documents.Open更改为Documents.Add,引用新的.dot并更改参数以使其与Add方法的参数匹配。

This will automatically open a COPY of the template file, so there is never any danger of the user or your code overwriting the template. 这将自动打开模板文件的COPY,因此永远不会对用户或您的代码覆盖模板造成任何危险。

Note, however, that "Save As" is still required since this is a new document, but it will come up automatically - the user won't have to think to use Save As. 但是请注意,由于这是一个新文档,仍需要“另存为”,但是它将自动出现-用户将不必考虑使用“另存为”。 If you don't want the user to see Save As at all your code needs to perform Document.SaveAs and you need to know the file path and location to which it should be saved. 如果您不希望用户看到“另存为”,则您的代码需要执行Document.SaveAs,并且您需要知道应将其保存到的文件路径和位置。

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

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