簡體   English   中英

如何使用VBA從Access中的同一字段填充Word中的多個書簽/表單字段

[英]How to fill multiple Bookmarks / FormFields in Word from the same field in Access using VBA

我以前一直使用一些VBA將Access中的字段傳遞到Word文檔中,直到遇到255個字符的限制。 這個站點的幫助使我現在使用書簽代替了表單字段。

我最初是在Word上填充許多不同的字段,在某些情況下,是在Word文檔的兩個位置上使用Access中的相同數據。 我通過致電:

.FormFields("txtReasonforReward").Result = Me![Reason for Reward]
.FormFields("txtReasonforReward2").Result = Me![Reason for Reward]

由於我現在有另一種填充“ Reason for Reward”框的方法,可以繞過字符數限制(下面的代碼),因此我不確定如何填充“ txtReasonforReward2”。 我確實有幾個實例,在其中我添加了第二個字段並在末尾添加了2個...我不相信這是最好的方法,因此,如果有人可以建議如何使用FormFields和Bookmarks來實現這一點,我真的很感激。

Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set doc = objWord.Documents.Open(***path to file***, , True)

Dim rng As Word.Range
Dim x As String

With doc
.FormFields("txtFirstName").Result = Me![First Name]
.FormFields("txtLastName").Result = Me![Last Name]
`examples cut for clarity...
.FormFields("txtHRID").Result = Me![ID]
.FormFields("txtPeriod").Result = Me![Period]

If doc.ProtectionType <> wdNoProtection Then
   doc.Unprotect
End If

Set rng = doc.Bookmarks("txtReasonforReward").Range
rng.MoveStart wdCharacter, -1
x = rng.Characters.First
rng.FormFields(1).Delete
rng.Text = x & Me![Reason for Reward]
doc.Protect wdAllowOnlyFormFields, True

.Visible = True
.Activate

End With

objWord.View.ReadingLayout = True

以問題和背景問題中的代碼為基礎...

Word可以使用REF域代碼復制書簽的內容。 由於表單字段還使用書簽標識符,因此這將與現有表單字段以及加書簽的內容一起使用。 如果熟悉的話,可以直接插入REF字段,或者通過將交叉引用插入書簽來插入。

參考用於插入超過255個字符的變通辦法,在這種情況下,還必須在要插入的范圍周圍放置一個書簽,並更新REF字段,以使它們在整個文檔中鏡像書簽內容。 代碼的修改部分如下。

'Declarations to be added at the beginning of the procedure
Dim fld As Word.Field
Dim bkmName As String

'Name of form field, bookmark to be added and text in REF field code
bkmName = "txtReasonforReward"     

'Existing code
If doc.ProtectionType <> wdNoProtection Then
   doc.Unprotect
End If
Set rng = doc.Bookmarks(bkmName).Range
rng.MoveStart wdCharacter, -1
x = rng.Characters.First
rng.FormFields(1).Delete
rng.Text = x & Me![Reason for Reward]

' New code
'Leave that single character out of the range for the bookmark
rng.MoveStart wdCharacter, 1
'Bookmark the inserted content
doc.Bookmarks.Add bkmName, rng
'Update fields so that REF's pick up the bookmark content
For Each fld In doc.Fields
    If fld.Type = wdFieldRef Then
        fld.Update
    End If
Next

doc.Protect wdAllowOnlyFormFields, True

如果需要將其應用於許多領域,則該方法將變得有些笨拙。 進行如下操作可能很有意義,例如將書簽名稱寫入“訪問”表單中控件的Tag屬性,然后循環控件以從控件中選擇書簽名稱和數據,而不是顯式地將它們分別寫出-但這僅僅是一個為未來而思考。

綜上所述,實現這一目標的“現代”方法是使用內容控件,而不是表單字段/書簽。 內容控件沒有255個字符的限制,文檔可以作為一種形式被保護,多個內容控件可以具有相同的標題(名稱)和/或標簽。 此外,可以將內容控件“映射”到存儲在文檔中的自定義XML部件,以便更改一個控件的內容將更改另一個控件的內容。 試圖在此處描述超出“答案”范圍的所有內容,但所有這些內容都可以通過搜索Internet公開獲得。

就個人而言,如果這是我的項目,並且知道我對它的了解:如果文檔中不需要表單字段(不需要用戶通過字段輸入),我將僅使用書簽和REF字段。

有很多方法可以做這種事情。 看一下下面的方法,看看是否可以使它起作用。

Option Compare Database
' This concept uses Docvariables in MS Word
Sub PushToWord()

Dim wapp As Word.Application
Dim wdoc As Word.Document
Dim db As DAO.Database
Dim fld As DAO.Field
Dim rs As DAO.Recordset
Dim filenm As String
Dim NumFields As Integer
Dim i As Integer

Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_CustomerData")

Set wapp = New Word.Application
wapp.Visible = True


Set rs = DBEngine(0)(0).OpenRecordset("SELECT * FROM tbl_CustomerData")

If rs.RecordCount > 0 Then
  rs.MoveFirst

  Do While Not rs.EOF
     For i = 0 To rs.Fields.Count - 1
        Set fld = rs.Fields(i)
        Debug.Print fld.Name, fld.Value
            If fld.Value = 20 Then
                filenm = "C:\Users\Ryan\Desktop\Coding\Integrating Access and Word\From Access to Word\Letter1.doc"
                    Set wdoc = wapp.Documents.Open(filenm)
                wapp.ActiveDocument.Variables("Name").Value = rs.Fields("Name").Value
            End If
     Next
     rs.MoveNext
  Loop
  Set fld = Nothing
  rs.Close
End If
Set rs = Nothing

End Sub

該代碼從Access運行,您可以通過多種方式將其觸發(按鈕單擊事件,表單加載事件,某些其他對象事件等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM