【问题标题】:Create a table and reference it创建一个表并引用它
【发布时间】:2020-11-29 15:48:58
【问题描述】:

我正在尝试读取每两行文本,创建一张幻灯片,然后使用 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

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    这是您的代码的返工:

    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 来放置字符串。

    【讨论】:

    • 感谢约翰的精彩解释。我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2013-01-14
    • 2015-01-23
    相关资源
    最近更新 更多