PowerPoint宏从当前日期获取下周四的日期

时间:2016-08-31 11:56:05

标签: vba date macros powerpoint

我试图从当前日期获取下周四的日期并将其写在第一张幻灯片中。到目前为止,我有当前的日期,但我没有找到如何获得下周四的日期。例如,我在2016年8月23日星期二打开我的演示文稿,当我的宏运行时,我希望在第一张幻灯片上获得25.08.2016(对应于星期四)。

到目前为止,我有:

Set objPPTX = CreateObject("PowerPoint.Application")
objPPTX.Visible = True
'Adding Date on First Slide 
Set PPApp = GetObject(, "Powerpoint.Application")
    Set PPPres = PPApp.ActivePresentation
    PPApp.ActiveWindow.ViewType = ppViewSlide
    PPApp.Visible = True
     Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)

' Set PPshape = PPSlide.Shapes.AddShape(Type:=msoShapeRectangle, Left:=220, Top:=150, Width:=270, Height:=75)
 Set PPshape1 = PPSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=20, Top:=150, Width:=680, Height:=70)
 With PPshape1
 .TextFrame.TextRange.Text = "PT PM Weekly"
 .TextFrame.TextRange.Font.Name = "Verdana"
 .TextFrame.TextRange.Font.Color = vbBlack
 .TextFrame.TextRange.Font.Size = 48
 End With

 Set PPshape = PPSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=350, Top:=150, Width:=680, Height:=70)
With PPshape


Todate = DateValue(Now)
oldWeekDay = Weekday(Now)
Select Case Thursday

Case oldWeekDay = 1
.TextFrame.TextRange.Text = Format(Now + 4, " dd.mm.yyyy")
Case oldWeekDay = 2
.TextFrame.TextRange.Text = Format(Now + 3, " dd.mm.yyyy")
Case oldWeekDay = 3
.TextFrame.TextRange.Text = Format(Now + 2, " dd.mm.yyyy")
Case oldWeekDay = 4
.TextFrame.TextRange.Text = Format(Now + 1, " dd.mm.yyyy")
Case oldWeekDay = 5
.TextFrame.TextRange.Text = Format(Now, " dd.mm.yyyy")
Case oldWeekDay = 6
.TextFrame.TextRange.Text = Format(Now - 1, " dd.mm.yyyy")
Case oldWeekDay = 7
.TextFrame.TextRange.Text = Format(Now - 2, " dd.mm.yyyy")

End Select

.TextFrame.TextRange.Font.Name = "Verdana"
.TextFrame.TextRange.Font.Color = vbBlack
.TextFrame.TextRange.Font.Size = 48
End With

但最后我从当前日期得到+4,所以我认为问题出在Case结构中。

2 个答案:

答案 0 :(得分:1)

这样的事情会有所帮助,请阅读WeekDay帮助

Function NEXT_THURSDAY(dtFrom As Date) As Date

Dim intCurrentDay As Integer

intCurrentDay = Weekday(dtFrom, vbThursday)

NEXT_THURSDAY = DateAdd("d", 8 - intCurrentDay, dtFrom)

End Function

答案 1 :(得分:1)

I think you have misunderstood the syntax of the Select Case statement.

You begin with

Select Case Thursday

but you haven't defined Thursday, so VBA will assume this new variable is equal to 0.

Your first case is then

Case oldWeekDay = 1

but Case should be followed by a value, not a comparison. VBA is evaluating the comparison oldWeekDay = 1 and if it is false, converting that to a value of 0. So the first case will always execute, unless oldWeekDay is actually equal to 1.

What I think you wanted to write was

Select Case oldWeekDay
    Case 1
    .TextFrame.TextRange.Text = Format(Now + 4, " dd.mm.yyyy")

and so on.

Note that if you added the statement Option Explicit at the top of your VBA module, you would instead have got a Variable not defined error for the Select Case line. It's highly recommended to always use Option Explicit for this sort of reason.