创建一个 table 并引用它

Create a table and reference it

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

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

我遇到编译错误;

"expected Function or variable" for the line below. It seems Select is not returning the table.

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

这里是你的代码的返工:

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,其中包含一个table。所以我更改了 Dim 语句以使其工作。

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

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