创建一个表并引用它 [英] Create a table and reference it

查看:63
本文介绍了创建一个表并引用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read every two lines of text, create a slide, and insert each line in the cells of a 2 by 1 table respectively using VBA code.

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 似乎没有返回表.

"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,其中包含一个表格.因此,我更改了 Dim 语句以使其起作用.

The object produced by Shapes.AddTable is a Shape, which contains a table. So I changed the Dim statement to make that work.

选择是不必要的.使用 Set 使 pptTable 成为您可以参考的形状.

The Select is unneccessary. Using Set makes pptTable a shape that you can refer to.

当您的代码越过该错误时,它会在下一行遇到另一个错误.需要参考TextRange里面的Text,TextFrame里面,Shape里面,cell里面来放置字符串.

When your code gets past that error, it runs into another in the next line. You need to refer to the Text inside the TextRange, inside the TextFrame, inside the Shape, inside the cell to place the string.

这篇关于创建一个表并引用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆