jeudi 23 juillet 2020

If Then Statement not Working for Powerpoint VBA

I have a powerpoint with a picture on each slide. The name of the picture contains the date and time the picture was taken. And on every slide there is a text box. My goal is to populate the text box with the name of the picture, the date of the picture and the time of the picture. I have figured out a way to input the name of the picture and the date of the picture, however for the time of the picture I am having trouble classifying it as pm or am. I am attempting to use an If Then Else statement to do this, however it does not seem to be working. Whenever I run the code, all of the times say pm, even the ones that are supposed to say am. My code is as follows, and the If Then statement is on line 29.

Sub PlaceNameofPictureInEverySlideEdit()

Dim sld As Slide
Dim shp As Shape
Dim PicName As String
Dim PicDate As String
Dim PicDateDate As Date
Dim PicDate1 As String
Dim PicDate2 As String
Dim PicDate3 As String
Dim PicDate4 As String
Dim PicDate5 As String
Dim PicDate6 As String
Dim DayNight As String




    For Each sld In Application.ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Type = msoPicture Then
                PicName = shp.Name
            End If
        Next
        
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                PicDate = shp.TextFrame.TextRange.Characters(1, 10)
                PicDate1 = shp.TextFrame.TextRange.Characters(6, 2)
                PicDate2 = shp.TextFrame.TextRange.Characters(9, 2)
                PicDate3 = shp.TextFrame.TextRange.Characters(1, 4)
                PicDate4 = shp.TextFrame.TextRange.Characters(12, 2)
                PicDate5 = shp.TextFrame.TextRange.Characters(15, 2)
                PicDateDate = CDate(PicDate)
                    If PicDate4 = "12" Or "13" Or "14" Or "15" Or "16" Or "17" Or "18" Or "19" Or "20" Or "21" Or "22" Or "23" Then
                    DayNight = " pm"
                    Else: DayNight = " am"
                    End If
       

                shp.TextFrame.TextRange.Text = PicName & vbNewLine & PicDateDate _
                & vbNewLine & PicDate4 & ":" & PicDate5 & DayNight
                
                
            With shp
                .TextFrame.TextRange.Replace(FindWhat:=".jpg", ReplaceWhat:="") = ""
                shp.Top = 473.5449
                shp.Left = 536.4305

            End With
            End If
            
            
        Next
    Next


End Sub

Aucun commentaire:

Enregistrer un commentaire