簡體   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