hashmap dict enum在VBA中的不同点
在解决一个两组数据的匹配的问题的时候,想到在算法图解中介绍的hashmap的算法,如果将这两组数据都作为数组来处理,其效率应该不会比hashmap的映射来的更快,从O(n)变为O(1)的问题
然后想到了dict的方法,但VBA也支持enum,想要知道两种方法那种效率更高。
PPT中对象的层次关系
application-presentations-slides-shapes
Shapes-ShapeRange-Shape
- Shapes collection to creae shapes and when you want to iterate all the shapes on a slide
- you use the Shape object when you want to modify a single shape
- you use the ShapeRange collection when you want to modify shapes the same way you can work with multiple selected shapes in the user interface.
Shape-table-cell
table 隶属于 Shape
'create table in shapes
ActivePresentation.Slide(2).Shapes _
.AddTable NumRows:=3, NumColumns:=4, Left:=10, _
Top:=10, Width:=288, Height:=288
'Useing HasTable to test whether a shape is tabel
'Table 的rows属性可以进行Cells遍历
shape(i).Table.rows(j).cells
MsoShapeType Enumeration
这里列举了所有的shape的类型,通过shape.type获得类型具体如下表
名称 | 值 | 说明 |
---|---|---|
msoAutoShape | 1 | 自选图形。 |
msoCallout | 2 | 标注。 |
msoCanvas | 20 | 画布。 |
msoChart | 3 | 图。 |
msoComment | 4 | 批注。 |
msoContentApp | 27 | 内容的 Office 加载项 |
msoDiagram | 21 | 图表。 |
msoEmbeddedOLEObject | 7 | 嵌入的 OLE 对象。 |
msoFormControl | 8 | 窗体控件。 |
msoFreeform | 5 | 任意多边形。 |
msoGraphic | 28 | 图形 |
msoGroup | 6 | 组合。 |
msoIgxGraphic | 24 | SmartArt 图形 |
msoInk | 22 | 墨迹。 |
msoInkComment | 23 | 墨迹批注。 |
msoLine | 9 | 线条。 |
msoLinkedGraphic | 29 | 链接的图形 |
msoLinkedOLEObject | 10 | 链接 OLE 对象。 |
msoLinkedPicture | 11 | 链接图片。 |
msoMedia | 16 | 媒体 |
msoOLEControlObject | 12 | OLE 控件对象。 |
msoPicture | 13 | 图片 |
msoPlaceholder | 14 | 占位符 |
msoScriptAnchor | 18 | 脚本定位标记。 |
msoShapeTypeMixed | -2 | 混和形状类型。 |
msoTable | 19 | 表。 |
msoTextBox | 17 | 文本框。 |
msoTextEffect | 15 | 文本效果。 |
msoWebVideo | 26 | Web 视频 |
获取地址
application.path 获得应用软件的地址,特别的在PPT中为PowerPoint应用的地址
C:\Program Files (x86)\Microsoft Office\Office15
presentation.path 获得ppt文档的地址
D:\file1\file2\target_file
Save\SaveAs\SaveCopyAs的区别
Save在当前文件目录下保存
SaveAs在当前目录下或目标目录下保存文件,运行的文件保存后名字自动变更为目标文件名(含地址)
SaveCopyAs必须带参数,保存后,当前的运行的文件不会替换为保存的目标文件,相当于该保存是作为对当前文件的备份
Collection object的区别
Collection 也是一个对象(Object)
Link1
Collection 与Dictionary的区别
Link
Dictionary的效率比Collection的效率更高
DocumentWindow
???
Option Explicit
Sub exchangeElPpt()
'read sheet data
Dim dataRngPH As Range
Dim dataRngTS As Range
Set dataRngPH = Sheet1.Range("a2:a14")
Set dataRngTS = Sheet1.Range("b2:b14")
'Debug.Print dataRng(1, 2).Format
Dim pptAPP As Object
Dim pptPres As Object
Dim pptName As String
pptName = "PPTtemplate.pptx"
Set pptAPP = CreateObject("powerpoint.application")
Set pptPres = pptAPP.presentations.Open(ThisWorkbook.Path & "\" & pptName)
Dim sl
Dim sh
Dim irow As Integer
Dim jcol As Integer
Dim i As Integer
Dim j As Integer
Dim irng As Range
Dim shapeText As String
'For Each sl In pptPres.slides
Set sl = pptPres.slides(1)
For Each sh In sl.Shapes
If sh.HasTextFrame Then
Debug.Print sl.Name & vbTab & sh.Name & vbTab & sh.TextFrame.TextRange.Text
ElseIf sh.HasTable Then
irow = sh.Table.Rows.Count
jcol = sh.Table.Columns.Count
For i = 1 To irow
For j = 1 To jcol
If sh.Table.Cell(i, j).Shape.HasTextFrame Then
shapeText = sh.Table.Cell(i, j).Shape.TextFrame.TextRange.Text
For Each irng In dataRngPH
If shapeText = irng.Text Then
Debug.Print "True"
sh.Table.Cell(i, j).Shape.TextFrame.TextRange.Text = irng.Offset(0, 1).Text
End If
Next
Debug.Print sl.Name & vbTab & sh.Name & vbTab & sh.Table.Cell(i, j).Shape.TextFrame.TextRange.Text
End If
Next
Next
End If
Next
'Next
End Sub
Debug.Print "==Slides In this presentation=="
For Each sl In pptPres.slides
Debug.Print sl.Name
Next
Debug.Print "==Target slide=="
Set sl = pptPres.slides("Slide3")
Debug.Print sl.Name
Dim tableTop As Single
Dim tableLeft As Single
Dim tableWidth As Single
Dim tableHeight As Single
For Each sh In sl.Shapes
If sh.Name = "Table 3" Then
tableTop = sh.Top
tableLeft = sh.Left
tableWidth = sh.Width
tableHeight = sh.Height
Debug.Print sh.Top & vbTab & sh.Left & vbTab & sh.Width & vbTab & sh.Height
sh.Delete
End If
'Debug.Print sh.Name
'sh.Name
'Title 1
'Footer Placeholder 2
'Table 3
Next
Dim pasteRng As Range
Dim targetShape
Set pasteRng = Sheet2.Range("A1:R59")
pasteRng.Copy
Set targetShape = sl.Shapes.Paste
'设置黏贴入的table的字体格式,设置为大小7,Arial格式
irow = targetShape.Table.Rows.Count
jcol = targetShape.Table.Columns.Count
For i = 1 To irow
For j = 1 To jcol
If targetShape.Table.Cell(i, j).Shape.HasTextFrame Then
With targetShape.Table.Cell(i, j).Shape.TextFrame.TextRange.Font
.Size = 7
.Name = "Arial"
End With
End If
Next
Next
'调整table的上边距举例及高度
targetShape.Height = tableHeight
targetShape.Top = tableTop
Debug.Print targetShape.Top & vbTab & targetShape.Left & vbTab & targetShape.Width & vbTab & targetShape.Height
'可以对table这个shape进行命名
targetShape.Name = "fsTable"
Debug.Print targetShape.Name
Slides(?)的参数
在slides的collection中,其参数如果使用数字1,2,3,4,ect. 代表PPT中幻灯片的显示位置
如果使用"slide名"则根据slide.Name属性来调用该Slide
link1
Slide
.Hyperlinks属性
Returns a Hyperlinks collection that represents all the hyperlinks on the specified slide. Read-only.
.Master属性
Returns a Master object that represents the slide master. Read-only.
.SlideId属性
Returns a unique ID number for the specified slide. Read-only.
using the FindBySlideID method with the slide's ID number can be a more reliable way to return a specific Slide object from a Slides collection than using the Item method with the slide's index number.
.SlideId /.SlideIndex /.SlideNumber的区别
???疑问点
- 从外部黏贴的机制
- 在ppt内部复制黏贴的注意点