简体   繁体   English

用于更改文本框内 PPT 幻灯片的日期格式的 VBA 代码

[英]VBA code to change date format of PPT slide inside text box

I am using following Excel VBA Code to insert current date in existing current power point slide.我正在使用以下 Excel VBA 代码在现有的当前幻灯片中插入当前日期。 Right now,I can insert current date in Powerpoint slide in 2nd text box but I'm unable to change date format per my requirement.Date appears like 3/26/2016 while I have to modify it like March 26, 2015 Please note that I'm not taking about date insertion at footer side, I have to add date in Text box.现在,我可以在第二个文本框中的 Powerpoint 幻灯片中插入当前日期,但我无法根据我的要求更改日期格式。日期显示为 3/26/2016 而我必须像 2015 年 3 月 26 日那样修改它请注意我不考虑在页脚端插入日期,我必须在文本框中添加日期。

Sub Date()
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PPshape As PowerPoint.Shape
Dim objPPTX As Object
Dim Todate As Date                  
 Todate = DateValue(Now)

Todate = Format(Todate, "mmmm d, yyyy")''tried this

'or this ?
Todate = Format(CDate(Todate), "mmmm d, yyyy")'''Tried this also

Application.DisplayAlerts = False

Set objPPTX = CreateObject("PowerPoint.Application")
objPPTX.Visible = True
objPPTX.Presentations.Open "path.pptx"

'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) ''copy chart on existing slide

Set PPshape = PPSlide.Shapes.AddShape(Type:=msoShapeRectangle, Left:=220, Top:=150, Width:=270, Height:=75)
With PPshape
.Fill.ForeColor.RGB = RGB(115, 111, 112)
.TextFrame.TextRange.Text = Todate
Todate = Format(Todate, "mmmm d, yyyy")
'or this ?
Todate = Format(CDate(Todate), "mmmm d, yyyy")
.TextFrame.TextRange.Font.Name = "Arial"
.TextFrame.TextRange.Font.Color = vbYellow
.TextFrame.TextRange.Font.Size = 18

End Sub

Pertinent to the Excel VBA code mentioned in your question, the solution can be as shown in the following example:与您的问题中提到的 Excel VBA 代码相关,解决方案可以如下例所示:

Sub DisplayDate()
  Range("A1").Value = CDate(DateValue(Now))
  Range("A1").NumberFormat = "mmmm d, yyyy"
End Sub

Here's your problem.这是你的问题。 You're setting the text of the text frame to Todate and THEN changing the formatting of Todate:您将文本框的文本设置为 Todate,然后更改 Todate 的格式:

.TextFrame.TextRange.Text = Todate
Todate = Format(Todate, "mmmm d, yyyy")

Instead try this:而是试试这个:

.TextFrame.TextRange.Text = Format(Now, "mmmm dd, yyyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM