计算字符串中的字符数

时间:2015-12-02 11:03:07

标签: vba count character powerpoint powerpoint-vba

我试图使用Len()函数从变量中获取字符数,但vba表示它不会识别它。所以我想知道我的错误是什么,我该如何解决?

Dim y As Long
Dim counthidden As Byte
Dim nbcharac As Long
Dim espace As Long

'boucle sur toutes les diapos à partir de la 2e
For y = 3 To ActivePresentation.Slides.Count
Set Diapo = ActivePresentation.Slides(y)

If Diapo.SlideShowTransition.Hidden = msoTrue Then
counthidden = counthidden + 1
End If
'si la diapo a un titre
If Diapo.Shapes.HasTitle And Diapo.SlideShowTransition.Hidden = msoFalse Then
Set titre = Diapo.Shapes.Title
nbcharac = Len(titre)
espace = 90 - nbcharac
texte_ajout = texte_ajout & titre.TextFrame. _
TextRange.Text & space(espace) & Format(y - counthidden, "0") & vbCrLf
End If

Next y

提前感谢您的帮助 (而且,如果你知道可以放入powerpoint页面的字符数,虽然我可以测试它)

1 个答案:

答案 0 :(得分:2)

由于tietreShape对象而不是字符串,Len将不起作用。您需要从形状中获取文本以计算长度,例如:

Set titre = Diapo.Shapes.Title
nbcharac = Len(titre.TextFrame.TextRange.Text)

使用模块顶部的Option Explicit可以防止很多这些问题,这将迫使您明确声明所有变量,例如tietreDiapo(a {在您的示例中,{1}} object),并在编译时显示许多问题,而不是在运行时显示。