提前致谢。标题总结得相当简洁,但为了便于说明,以下是我试去做:
Sub Main
dim doc as Object, sheet as Object
doc=ThisComponent
numSheets=doc.Sheets.count()
sheet=doc.Sheets(numSheets-1)
sheetName=sheet.Name
currentDay=Date()
lastDay=sheet.getCellRangeByName("B1").value
mon=Month(lastDay)
Select case mon:
case 1:
monMod=31
case 2:
monMod=28
case 3:
monMod=31
case 4:
monMod=30
case 5:
monMod=31
case 6:
monMod=30
case 7:
monMod=31
case 8:
monMod=31
case 9:
monMod=30
case 10:
monMod=31
case 11:
monMod=30
case 12:
monMod=31
End Select
nextMonEnd=CDate(lastDay+monMod)
If currentDay > lastDay Then
newName$=CDate(lastDay+1) & "-" & CDate(nextMonEnd)
doc.Sheets.copyByName(sheetName, newName$, numSheets) '<--
newSheet=doc.sheets(numSheets) 'this part here is the
newPrevious=newSheet.getCellRangeByName("A1")'snag.Throws a Runtime
newPrevious.string=CDate(lastDay+1) 'Exception, Message: .
newEnd=newSheet.getCellRangeByName("B1") 'I think it's passing a
newEnd.string=CDate(nextMonEnd) 'null, but I don't know
End If 'why. Any help would be
End Sub 'greatly appreciated
如果我对 destName 参数进行硬编码,它会运行正常。我尝试了几种不同的方法,但作为 destName 传递的任何变量都会引发错误。再次感谢大家。
修正:在一些帮助下(见评论),我提出了这个宏的工作修订版,其他一切都保持不变。发布它是为了相关。
nextMonEnd=CDate(lastDay+monMod)
newFirst=CDate(lastDay+1)
newName=CStr(Format(newFirst, "mm-dd-yyyy")) & " " & _
CStr(Format(nextMonEnd, "mm-dd-yyyy"))
If currentDay > lastDay Then
doc.Sheets.copyByName(sheetName, newName, numSheets)
newSheet=doc.sheets(numSheets)
newCurrent=newSheet.getCellRangeByName("A1")
newCurrent.value=lastDay+1
newEnd=newSheet.getCellRangeByName("B1")
newEnd.value=nextMonEnd
print "New Monthly sheet added."
End If
答案1
问题是生成的工作表名称包含禁止字符。在 OpenOffice.Calc 中,
工作表名称中只能使用字母、数字、空格和下划线字符。
您构建的工作表名称包含斜杠/
,导致工作表名称无效。