首页 > 解决方案 > 创建一个表并引用它

问题描述

我正在尝试阅读每两行文本,创建一张幻灯片,然后使用 VBA 代码分别将每一行插入 2 x 1 表格的单元格中。

Public Sub newSlide()
    Dim FileNum As Integer
    Dim DataLine As String
    Dim Count As Integer
    Count = 0
    FileNum = FreeFile()
    Open "C:\Users\ADMININST\Documents\my.txt" For Input As #FileNum
        
    While Not EOF(FileNum)
        Count = Count + 1
        Line Input #FileNum, DataLine ' read in data 1 line at a time
        ' decide what to do with dataline,
        ' depending on what processing you need to do for each case
        Debug.Print ("Love you")
        If Count Mod 2 = 1 Then
            Dim pptSlide As Slide
            Dim pptLayout As CustomLayout
     
            Set pptLayout = ActivePresentation.Slides(1).CustomLayout
            Set pptSlide = ActivePresentation.Slides.AddSlide(2, pptLayout)
            'ActivePresentation.Slides.Add Index:=ActivePresentation.Slides.Count + 1, Layout:=ppLayoutCustom
        
            Dim pptTable As Table
            pptTable = pptSlide.Shapes.AddTable(2, 1).Select
            pptTable.Cell(1, Count Mod 2) = DataLine
        End If
    Wend
End Sub

我得到一个编译错误;

以下行的“预期函数或变量”。似乎 Select 没有返回表格。

pptTable = pptSlide.Shapes.AddTable(2, 1).Select

标签: vbapowerpoint

解决方案


这是您的代码的返工:

Dim pptTable As Shape
Set pptTable = pptSlide.Shapes.AddTable(2, 1)
pptTable.Table.Cell(1, Count Mod 2).Shape.TextFrame.TextRange.Text = DataLine

Shapes.AddTable 生成的对象是一个包含表格的Shape 。所以我更改了 Dim 语句以使其工作。

Select 是不必要的。使用Set使 pptTable 成为您可以参考的形状。

当您的代码通过该错误时,它会在下一行遇到另一个错误。您需要引用 TextRange 内、TextFrame 内、Shape 内、单元格内的 Text 来放置字符串。


推荐阅读