Test for a substring in Powerpoint VBA

792 views Asked by At

I have a script to import several pictures each to a slide in Powerpoint 2010:

Sub import_pics_from_dir_explicit()


    Dim longSlideCount As Long
    Dim slideObject As Slide

    ' ----------------------------------------------------------------------------- '
    ' ------------------ define arrays for pic addresses & titles ----------------- '
    ' ----------------------------------------------------------------------------- '

    Dim picAddressArray() As String
    Dim mainTitleArray() As String
    Dim subTitleArray() As String
    Dim i As Integer

    ' -------- read from txt file to picAddressArray1() -------- '
    Open "pic1_addresses.txt" For Input As #1
    picAddressArray1 = Split(Input$(LOF(1), #1), vbLf)
    Close #1

    ' -------- read from txt file to mainTitleArray() -------- '
    Open "main_titles.txt" For Input As #1
    mainTitleArray = Split(Input$(LOF(1), #1), vbLf)
    Close #1

    ' -------- read from txt file to subTitleArray() -------- '
    Open "sub_titles.txt" For Input As #1
    subTitleArray = Split(Input$(LOF(1), #1), vbLf)
    Close #1

    ' --------------------------------------------------------------------------------------------------- '
    ' --------------------------------- set the slide layout parameters --------------------------------- '
    ' --------------------------------------------------------------------------------------------------- '

    slide_width_in = 10
    slide_height_in = 6.25

    slide_width_pt = slide_width_in * 72
    slide_height_pt = slide_height_in * 72

    banner_height_pct = 0.17
    banner_height_pt = (slide_height_pt * banner_height_pct)

    footer_height_pct = 0.05
    footer_height_pt = (slide_height_pt * footer_height_pct)

    side_margin_pct = 0.01
    side_margin_pt = (slide_width_pt * side_margin_pct)

    top_bottom_margin_pct = 0.01
    top_bottom_margin_pt = ((slide_height_pt - banner_height_pt - footer_height_pt) * top_bottom_margin_pct)

    num_pic_columns_on_slide = 1
    pic_default_width_pt = ((slide_width_pt - 2 * side_margin_pt) / num_pic_columns_on_slide) - (2 * side_margin_pt)

    pic_default_aspect_ratio = 1000 / 1700
    pic_default_height_pt = pic_default_width_pt * pic_default_aspect_ratio

    intended_pic_rows = 1
    maximum_allowed_height_of_pic = ((slide_height_pt - banner_height_pt - footer_height_pt) / intended_pic_rows) - (2 * top_bottom_margin_pt)

    If pic_default_height_pt > maximum_allowed_height_of_pic Then
       pic_default_height_pt = maximum_allowed_height_of_pic
       pic_default_width_pt = maximum_allowed_height_of_pic * (1 / pic_default_aspect_ratio)
    End If

    pic_1_top = banner_height_pt + 1 * top_bottom_margin_pt

    pic_1_left = 1 * side_margin_pt

    ' ---------------------------------------------------------------------- '

    For i = 0 To UBound(subTitleArray)

        longSlideCount = ActivePresentation.Slides.Count

        With ActivePresentation.Slides
           Set slideObject = .Add(longSlideCount + 1, ppLayoutTitle)
        End With

        slideObject.Shapes(1).TextFrame.TextRange.Text = mainTitleArray(i)
        slideObject.Shapes(2).TextFrame.TextRange.Text = subTitleArray(i)

        slideObject.Shapes(1).TextFrame.TextRange.Font.Size = 30
        slideObject.Shapes(2).TextFrame.TextRange.Font.Size = 24

        Set pic1 = slideObject.Shapes.AddPicture( _
        FileName:=picAddressArray1(i), _
        LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, _
        Left:=pic_1_left, _
        Top:=pic_1_top)

        pic1.LockAspectRatio = msoTrue
        pic1.Width = pic_default_width_pt

    Next i

End Sub

...but before I make each slide, I'd like to check if there is a # character in the string designated for the sub-title subTitleArray(i).

Basically I want to implement the following metacode to the last part of the macro:

    For i = 0 To UBound(subTitleArray)
        If Not substr('#',subTitleArray(i))
          ...do stuff...
        End If
    Next i

Any ideas on how to do this?

1

There are 1 answers

1
bodjo On BEST ANSWER

You can use Instr like

For i = 0 To UBound(subTitleArray)
    If Instr(1, subTitleArray(i),"#") = 0 Then
      ...do stuff...
    End If
Next i