如何检查PowerPoint文档中的所有文本框是否已填写



我有一个PowerPoint文档,用户可以在其中将文本输入到多个文本框中,总共超过6张幻灯片。在最后一张幻灯片上,我想检查用户是否填写了演示中的每个TextBox。

我试着在互联网上使用了很多代码片段并对其进行了修改,但我是一个完全的VBA傻瓜,令人惊讶的是,它没有成功。:(我非常感谢你在这项任务上的帮助。

如果可以检查用户是否在每个文本框中输入了至少4个字符,那就更好了。然而,我不知道如何开始编程这个

这是我的代码,它不会显示错误,但在最后单击复选框时不会发生任何事情。

Public Sub CheckTextBox()
Dim fTextBox As Object
For Each Slide In ActivePresentation.Slides
For Each fTextBox In ActivePresentation.Slides
If TypeName(fTextBox) = "TextBox" Then
If fTextBox.Text = "" Then
MsgBox "Please make sure to fill out all fields!"
End If
End If
Next
Next
End Sub

'When ticking this CheckBox, the MsgBox should show up
Private Sub CheckBox1_Click()
CheckTextBox
End Sub

非常感谢你们的帮助。

您的内部For-循环是错误的,您需要在幻灯片的所有Shapes上循环,而不是在所有幻灯片上开始另一个循环。

基本上,您放置在幻灯片上的所有对象都是Shapes。如果您使用TypeName,您将获得Shape。要区分单个形状类型,请使用形状对象的属性type。类型列表可在https://learn.microsoft.com/de-de/office/vba/api/office.msoshapetype-文本框的类型为msoTextBox(17(。

若要获取形状的文本,请使用该形状的属性TextFrame.TextRange.Text

请尝试以下代码(它已经检查了至少4个字符的长度(。它将在第一个文本框中少于4个字符处停止(否则,每个文本框将获得一个MsgBox(并选择它。

Public Sub CheckTextBox()
Dim sh As Shape, slide As slide
For Each slide In ActivePresentation.Slides
For Each sh In slide.Shapes
Debug.Print TypeName(sh)
If sh.Type = msoTextBox Then
If Len(sh.TextFrame.TextRange.Text) < 4 Then
MsgBox "Please make sure to fill out all fields!"
slide.Select
sh.Select
Exit For
End If
End If
Next
Next
End Sub

UPDATE上面的代码没有考虑组中的形状。以下代码在所有幻灯片的所有形状上循环,并调用函数checkShape,该函数将检查
a(如果该形状是textBox(msoTextBox,17(-如果是,则检查文本的长度,如果太短,则返回该形状
b(如果形状是(msoGroup,6(,它会(递归(调用所有子形状的函数,并返回找到的第一个子文本框
主例程(CheckAllTextBoxes(检查是否找到任何textBox,如果找到,将选择它并发出消息。

Public Sub CheckAllTextBoxes()
Dim slide As slide, sh As Shape
For Each slide In ActivePresentation.Slides
For Each sh In slide.Shapes
Dim textBox As Shape
Set textBox = CheckShape(sh, 4)
If Not textBox Is Nothing Then
slide.Select
textBox.Select
MsgBox "Please make sure to fill out all fields!"
Exit Sub
End If
Next
Next
End Sub
Function CheckShape(sh As Shape, minLen As Integer) As Shape
' Check if shape is a Textbox and then text is not long enough
If sh.Type = msoTextBox Then
If Len(sh.TextFrame.TextRange.Text) < minLen Then
Set CheckShape = sh
Exit Function
End If
End If
' For a group, check all it's child shapes
If sh.Type = msoGroup Then
Dim child As Shape
For Each child In sh.GroupItems
Dim textBox As Shape
Set textBox = CheckShape(child, minLen)
If Not textBox Is Nothing Then
' Found a Textbox within the group, return it
Set CheckShape = textBox
Exit Function
End If
Next child
End If
End Function

对于那些寻找c#代码以列出演示文稿中所有文本框的人:

using Microsoft.Office.Interop.PowerPoint;
using MsoShapeType = Microsoft.Office.Core.MsoShapeType;
public static IEnumerable<Shape> AllTextBoxes (Presentation presentation) =>
from slide in presentation.Slides.Cast<Slide>()
from shape in slide.Shapes.Cast<Shape>()
from textBox in AllTextBoxes(shape)
select textBox;
public static IEnumerable<Shape> AllTextBoxes (Shape sh)
{
IEnumerable<Shape> _() { if (sh.Type == MsoShapeType.msoTextBox) yield return sh; }
return sh.Type == MsoShapeType.msoGroup ? sh.GroupItems.Cast<Shape>().SelectMany(AllTextBoxes) : _();
}

最新更新