【问题标题】:VBA returning incorrect month dates wordVBA返回不正确的月份日期字
【发布时间】:2018-02-28 02:25:34
【问题描述】:

由于某种原因,我在创建文件时遇到了以下代码的问题,它只在正确月份的 30 号而不是 31 号运行,并且在 2 月它创建到 30 号。该代码旨在为每个月创建文件夹,然后从 1 个主文档创建一个月的文件。我使用的原始代码有效,但没有创建文件夹。

这是返回错误的代码

 Sub Folder()

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Dim fso As FileSystemObject     ' ''early binding. Requires reference to MS Scripting runtime
    'Set fso = New FileSystemObject     ''early binding

    Dim myYear As Long
    Dim endOfMonth As Long
    Dim filePathStub As String

    filePathStub = "c:\user\test briefing sheet\2019\" ' path to create folders at"

    myYear = 19

    Dim monthsArray() As Variant

    monthsArray = Array("Jan", "Feb", "Mar", "April", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")

   Dim currentMonth As Long

   For currentMonth = LBound(monthsArray) To UBound(monthsArray)

       Dim folderName As String

       folderName = monthsArray(currentMonth) & " " & CStr(myYear)

       folderName = fso.CreateFolder(folderName)

       endOfMonth = CLng(Format$(dhLastDayInMonth(DateSerial(myYear, currentMonth + 1, 0)), "dd"))

       Dim currentDay As Long

       For currentDay = 1 To endOfMonth

           ActiveDocument.SaveAs2 FileName:=folderName & Application.PathSeparator & monthsArray(currentMonth) & " " & currentDay, FileFormat:=wdFormatXMLDocument

       Next currentDay

   Next currentMonth

End Sub

Function dhLastDayInMonth(Optional dtmDate As Date = 0) As Date
    ' Return the last day in the specified month.
    If dtmDate = 0 Then
        ' Did the caller pass in a date? If not, use
        ' the current date.
        dtmDate = Date
    End If
    dhLastDayInMonth = DateSerial(Year(dtmDate), _
     Month(dtmDate) + 1, 0)

End Function

这是原始代码

    Sub Mine()
     Dim DateStr, FileStr As String
      DateStr = Format$(Date, "DD")
      FileStr = DateStr & ".docx"

      ActiveDocument.Save
      ChangeFileOpenDirectory "c:\user\test briefing sheet\2019\"
      ActiveDocument.SaveAs2 FileName:=FileStr, FileFormat:=wdFormatXMLDocument

End Sub

有什么想法吗?

【问题讨论】:

    标签: vba ms-word filenames


    【解决方案1】:

    在这一行:

    ActiveDocument.SaveAs2 FileName:=folderName & Application.PathSeparator & monthsArray(currentMonth) & " " & currentDay, FileFormat:=wdFormatXMLDocument
    

    currentDay 是 Long,而您正试图将其用作字符串。我将其编码如下:

    Dim documentName as string
    documentName = monthsArray(currentMonth) & " " & CStr(currentDay)
    ActiveDocument.SaveAs2 FileName:=folderName & Application.PathSeparator & documentName, FileFormat:=wdFormatXMLDocument
    

    另外,我会重写这一行:

    endOfMonth = CLng(Format$(dhLastDayInMonth(DateSerial(myYear, currentMonth + 1, 0)), "dd"))
    

    作为:

    endOfMonth = DAY(dhLastDayInMonth(DateSerial(myYear, currentMonth + 1, 0)))
    

    【讨论】:

    • 嗨,我试过了,它仍在创建 2 月 31 日,不会创建 12 月 31 日。不知道为什么?
    • VBA 中的数组从 0 开始,而不是从 1 开始,所以这可能是问题所在。要修复它,请将 Option Base 1 添加到模块的顶部
    • 或者,您可以在函数调用中添加 1:endOfMonth = DAY(dhLastDayInMonth(DateSerial(myYear, (currentMonth+1) + 1, 0)))
    • 你必须原谅我缺乏知识,因为我对此比较陌生,但以上对你来说是什么意思?
    • 当你写这个时: For currentMonth = LBound(monthsArray) To UBound(monthsArray) 你可能的意思是“For Month = 1 to Month = 12 .....”但实际发生的是“对于月 = 0 到月 = 11 ... "
    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多