FileSave()Word Macro

时间:2015-09-21 12:54:50

标签: vba ms-word word-vba

我编写了一个宏,当创建文件并单击小保存按钮时,宏会被触发(因为它会覆盖默认的FileSave函数)。宏从我的文档中的表中提取信息,将其转换为字符串,然后清理任何回车符的字符串,然后将其用作文件名。然后运行if语句检查表中隐藏的行是否具有值1,如果不是,则将值设置为1并将文档保存在使用新文件名指定的位置。

所有这一切都很有效,除非我重新打开文件进行编辑,正如我的用户所做的那样,然后再次单击“保存”会尝试再次运行所有内容,完全忽略我的If语句第一个语句并将添加回车符到文件名的开头有效地将保存功能分解为SharePoint,因为它中包含无效字符。如果然后再次单击“保存”,它将看似正常运行宏并通过实际正确读取if语句来保存它。我在这里做错了吗?

以下是代码:

Sub FileSave()
    Dim strText As String
    Dim strClean As String
    Dim strFileName As String
    Dim strLocation As String
    Dim strSavedName As String
    Dim strCleanSave As String

    strText = ActiveDocument.Tables(1).Rows(1).Cells(2).Range.Text

    strClean = Application.CleanString(strText)

    strFileName = strClean + "_" + Format(Date, "yyyy-mm-dd")

    strLocation = "[My SharePoint Site]"

    If ActiveDocument.Tables(1).Rows(1).Cells(3).Range.Text = "1" Then
        strSavedName = ActiveDocument.Name
        strCleanSave = Application.CleanString(strSavedName)
        ActiveDocument.SaveAs FileName:=strSavedName
        Exit Sub
    Else
        ActiveDocument.Tables(1).Rows(1).Cells(3).Range.Text = "1"
        ActiveDocument.SaveAs FileName:=strLocation & strFileName & ".docx"
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

Word表格单元格文本范围以两个隐藏字符终止,回车符(ASCII 13)和铃声(ASCII 7)。您的IF条件返回false,因为它正在测试Subject = ReactRouterContext(NewSession) instance = TestUtils.renderIntoDocument(<Subject />) "1"的等效性。

在您的情况下,您可以将测试限制为第一个字符:

"1" & Chr(13) & Chr(7)

更一般地说,您可以使用Len() - 2来测试可见单元格内容。

希望有所帮助。

相关问题