在 Word 文件頁腳列印序號或遞增數字

在 Word 文件頁腳列印序號或遞增數字

我覺得這可能是重複的,但我找不到好的答案,而「問題可能已經有你的答案」只顯示 Excel 的答案。

我希望透過在每個列印的文件上新增序號(也稱為遞增編號)來追蹤列印的文件。

目標是我們有一個文檔作為主文檔,然後記錄。

我希望每個日誌都有一個增量/唯一的編號,然後人們可以在主文檔上寫下該唯一的編號,以便於引用相應的日誌。

我一直在尋找一些複雜的VBA,但我不熟悉為Word 添加VBA 程式碼,這看起來很複雜,我認為Word 有一個我缺少的內建函數。

我希望該字段看起來像這樣:

Doc #: 1

下次我們列印時它應該會顯示在頁腳中

Doc #: 2

等等等等

謝謝你,

PS:我們目前使用的是Office 2013。

https://stackoverflow.com/questions/50769735/improving-vba-macro-in-word-to-automatically-create-file-related-to-document-na

在Word文檔中嵌入宏

在Word文檔頁腳列印序號或增量號

https://stackoverflow.com/questions/48909968/running-a-macro-before-printing-a-word-document

答案1

Seem 的 VBA 是可行的方法,我只找到了適合我的問題並且有效的解決方案。

方法一:(此方法使文件可移植,但每個文件沒有唯一的編號)

Serial_Numbered_Doc_Template.docm

  • 首先建立一個名為“Counter”的“自訂文件屬性”,並將初始值設為 0(或其他值)。

  • 插入到文件中列印時所需的位置(CTRL + F9)

  • 每次呼叫模組時,該模組都會將數字加一:

Sub FilePrint()
    Dim i As Long, j As Long
    With ActiveDocument
        j = CLng(InputBox("How many copies to print?", "Print Copies"))
        For i = 1 To j
            With .CustomDocumentProperties("Counter")
                .Value = .Value + 1
            End With
            .Fields.Update
            ActiveDocument.PrintOut Copies:=1
        Next
        .Save
    End With
End Sub   
  • 現在我修改了我的“EventClassModule”以在列印時自動呼叫此子程式:
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    ' MsgBox "Before Print"
    ' Call Greeting
    ' Call SerialNumber
    Cancel = True
    Call FilePrint
End Sub

方法二:(此方法使用文件來追蹤序號,其優點是多個 Word 文件共享相同的設定/更新日誌,每個 Word 文件應該有一個唯一的編號)

我按照這裡找到的指南 -->

https://wordmvp.com/FAQs/MacrosVBA/NumberCopiesOf1Doc.htm

當然,網站上發布的程式碼是不正確的...

問題是它需要有這個程式碼片段:

' Display message, title, and default value.
Dim SerialNumber As String
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\settings.txt", "MacroSettings", "SerialNumber")

Dim SerialNumber As String請注意網站上沒有的行。沒有它,您會收到此問題中發現的錯誤>>

https://stackoverflow.com/questions/48348049/vba-compile-error-expected-function-or-variable

為了後代,我將在此處複製該指南:

另請注意,我做了另一項更改,即該網站顯示“settings.txt”的兩個位置,一個是“Settings.Txt”,另一個是“Settings.txt”。我將其切換為“settings.txt”,因為我是一名 Linux 人員,並且知道大寫字母和空格等將來會給您帶來麻煩。

------------------------------------------------`

建立一個SerialNumber在文件中命名的書籤,其中您希望序號出現。如果您需要數字,它可以位於頁首或頁尾中。然後建立一個包含以下命令的巨集來列印文件。

它將詢問您想要製作的副本數量,並按順序對每個副本進行編號。檔案中儲存該編號比上一個副本上的編號多一個。如果首次啟動時,您希望數字從 1 以外的數字開始,請執行宏,輸入 1 作為份數,然後開啟 Settings.Txt 檔案並將檔案中的數字替換為您想要的數字作為系列中的第一個。

Sub SerialNumber()
    '
    ' SerialNumber Macro
    '
    '
    Dim Message  As String, Title  As String, Default  As String, NumCopies  As Long
    Dim Rng1   As Range
    
    ' Set prompt.
    Message = "Enter the number of copies that you want to print"
    ' Set title.
    Title = "Print"
    ' Set default.
    Default = "1"
    
    ' Display message, title, and default value.
    Dim SerialNumber As String
    NumCopies = Val(InputBox(Message, Title, Default))
    SerialNumber = System.PrivateProfileString("W:\settings.txt", _
        "MacroSettings", "SerialNumber")
    
    If SerialNumber = "" Then
        SerialNumber = 1
    End If
    
    Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
    Counter = 0
    
    While Counter < NumCopies
        Rng1.Delete
        Rng1.Text = SerialNumber
        ActiveDocument.PrintOut
        SerialNumber = SerialNumber + 1
        Counter = Counter + 1
    Wend
    
    'Save the next number back to the Settings.txt file ready for the next use.
    System.PrivateProfileString("W:\settings.txt", "MacroSettings", _
        "SerialNumber") = SerialNumber
    
    'Recreate the bookmark ready for the next use.
    With ActiveDocument.Bookmarks
        .Add Name:="SerialNumber", Range:=Rng1
    End With
End Sub

注意:您可以/應該將“settings.txt”的位置更改為您的 LAN 首選位置!

settings.txt
[MacroSettings]
SerialNumber=1

相關內容