5941错误集合中请求的成员不存在Word Macro

时间:2016-01-05 17:12:41

标签: vba ms-word word-vba

美好的一天。我离程序员很远,但我需要帮助找出这个VBA脚本的错误。它在修改订单表单的word文档中运行。其目的是为每个印刷品增加一个订单号。计数器更新.txt文件以识别打印所需的下一个数字。就我的理解而言。代码如下。

Sub serialNumberPrint()
'
' 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.
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\Users\GaleR\Documents\SettingsSerial.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 = Format(SerialNumber, "000#")
    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("C:\Users\GaleR\Documents\SettingsSerial.txt", "MacroSettings", _
        "SerialNumber") = SerialNumber

'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
    .Add Name:="SerialNumber", Range:=Rng1
End With

ActiveDocument.Save


End Sub

调试让我走到了这一行:

 Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range

我在斗智斗勇,我不知道该怎么办。我事先为自己缺乏知识而道歉,但确实需要帮助。

1 个答案:

答案 0 :(得分:0)

错误告诉您文档中不存在书签SerialNumber。如果您在文档(或模板)中选择该位置并插入具有该名称的书签,则应该可以解决问题。