在Powerpoint VBA中测试子字符串

时间:2014-11-12 17:50:45

标签: vba substring powerpoint-vba powerpoint-2010

我有一个脚本可以将每张图片导入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

...但在我制作每张幻灯片之前,我想检查为子标题#指定的字符串中是否有subTitleArray(i)个字符。

基本上我想在宏的最后一部分实现以下元代码:

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

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:3)

您可以使用Instr like

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