vba - 创建一个表并引用它
问题描述
我正在尝试阅读每两行文本,创建一张幻灯片,然后使用 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
解决方案
这是您的代码的返工:
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 来放置字符串。
推荐阅读
- python - Socket Programming Python:如何确保接收到整个消息?
- python - 使用 bash 命令更改 python3 脚本中的文件夹
- discord - Discord.js 重命名当前频道
- python - Python列表转json格式
- java - Gson 反序列化器只创建超类
- reactjs - React + Styled Components - 分离和重用 css “包”
- redux - ReduxForm 字段的两个输入
- r - 如何通过 R 传递 Pardot 身份验证标头
- angular - 如何在不同窗口中的组件之间使用相同的角度服务实例?
- java - 数组是否具有固有的 hashCode()?