将值分配给特定文档后无法保存.docm文件。

时间:2019-06-12 02:26:17

标签: vba ms-word

我有一个带有宏的文件,该宏最初创建为.doc文件。如果我尝试将其另存为.docm.dotm文件,它将不会保存。没有错误信息,但文件没有保存。

如果我卸下模块;或另存为.docx文件,它将保存。

如果我

  • 打开.docx文件
  • 将先前的宏导入或复制/粘贴到常规模块中
  • 运行宏

同样,它不能另存为.docm

如果删除模块,则可以将其另存为.docm

或者,即使存在并运行了宏,我也可以将其另存为.doc文件。

因此,似乎宏中有些东西在运行后会创建无法保存为.docm但仍可以保存为.doc文件的文件。

宏检查手动输入的文档的某些自定义属性。它会计算各种文档变量,这些变量将在整个文档的字段中使用,然后更新这些字段。

通过使用断点,似乎在代码为Document.Variable "sign"分配值时,无法再保存文档。那是一个日期字段,但是文档中还有其他日期字段不会引起此问题。

特定字段的code.text:

docvariable "sign" \@ "MMMM, yyyy" \*upper \* MERGEFORMAT 
docvariable "sign" \@ "d" \* OrdText \*upper\* MERGEFORMAT 

宏代码(在常规模块中):

Option Explicit

Sub Update()
    Dim prop As DocumentProperty
    Dim area As Double, rent As Double, start As Date, tend As Date, notice As Date, term As Long, Sign As Date
    Dim annual As Double, annualcents As Double, monthly As Double, monthlycents As Long
    Dim var As Variables

'get Document Properties
For Each prop In ThisDocument.CustomDocumentProperties
    If prop.Name = "area" Then area = prop.Value
    If prop.Name = "rent" Then rent = prop.Value
    If prop.Name = "start" Then start = Int(prop.Value)
    If prop.Name = "term" Then term = prop.Value
    If prop.Name = "sigDate" Then Sign = Int(prop.Value)
Next prop

    annual = area * rent
    annualcents = 100 * (annual - Int(annual))
    monthly = annual / 12
    monthlycents = 100 * (monthly - Int(monthly))
    tend = DateAdd("yyyy", term, start)
    tend = DateAdd("d", -1, tend)
    notice = DateAdd("m", -4, tend)

Set var = ThisDocument.Variables
    var("area").Value = area
    var("rent").Value = rent
    var("start").Value = start
    var("term").Value = term

    'if this line is not run or
    'if this document variable is deleted,
    'the file will save as expected
    var("sign").Value = Sign


    var("annual").Value = annual
    var("annualcents").Value = annualcents
    var("monthly").Value = monthly
    var("monthlycents").Value = monthlycents
    var("tend").Value = tend
    var("notice").Value = notice

ThisDocument.Fields.Update

End Sub

1 个答案:

答案 0 :(得分:2)

Word使用Document.Variable "sign"表示有效的数字签名已应用于代码项目。

为变量选择其他名称,例如“ sigDate”