【发布时间】:2018-01-26 03:30:59
【问题描述】:
我正在尝试将格式化的文本内容从 Excel 复制到 VBA 中的 Powerpoint——最好不要复制和粘贴,因为它每次运行时都会崩溃(即使有多个 DoEvents 来减慢它的速度......是数百个重格式文本的单元格)。
这就是为什么我一直试图通过像下面的代码中那样直接寻址单元格来使其工作。
For i = 1 To WS.Range("A65536").End(xlUp).Row
If WS.Cells(i, 1) > 0 Then
Set newSlide = ActivePresentation.Slides(1).Duplicate
newSlide.MoveTo (ActivePresentation.Slides.Count)
With newSlide.Shapes(1).TextFrame.TextRange
.Text = WS.Cells(i, 1).Value ' Inserts the (non-formatted) text from Excel. Have also tried WS.Cells(i, 1).Text
.Font.Name = WS.Cells(i, 1).Font.Name ' This works fine
.Font.Size = WS.Cells(i, 1).Font.Size ' This works fine too
' Neither of the below work because there is a mixture of font styled and colours within individual cells
.Font.FontStyle = WS.Cells(i, 1).Font.FontStyle ' Font Style (Regular, Bold, Italic, Bold Italic)
.Font.Color = WS.Cells(i, 1).Font.Color ' Font Color
End With
End If
Next
它可以(非常快速地)传输单元格内容、字体名称和字体大小......但不适用于 FontStyle(粗体、斜体等)或 FontColor,因为在单个单元格中存在不止一种样式/颜色。
有没有办法解决这个问题?我不知道潜在的解决方案(如果有的话)可能是什么,所以甚至不知道从哪里开始寻找。即使是朝着正确的方向推进也会有很大的帮助。
【问题讨论】:
-
您可能对某些工作表单元格应用了条件格式。如果这样做,则必须使用范围的
DisplayFormat属性。例如。.Font.Color = WS.Cells(i, 1).DisplayFormat.Font.Color等.....(这是因为条件格式层格式化为一个单元格,而顶部格式是您看到的格式。)....... DisplayFormat 从 Excel 2010 开始可用 -
只需将代码中的
.Font替换为.DisplayFormat.Font(在赋值语句的 excel 一侧) -
感谢您的帮助 jsotola。 .DisplayFormat 似乎在单元格中的所有文本都是粗体...或斜体...或单一颜色的情况下工作正常。但是,在我的电子表格中,每个单元格都混合了这些。例如,在某些单元格中,有些单词是粗体的,有些不是粗体的……都在同一个单元格中。在其他单元格中,有些单词是黑色的,有些单词是红色的……同样,都在同一个单元格中。 (这有意义吗?我想也许我的问题并没有把那部分说得很清楚。)
-
(将 .DisplayFormat 与这种样式/颜色混合使用会导致错误:“”运行时错误 438。对象不支持此属性或方法“”)
-
我刚刚重新阅读了您的问题。您是说单元格中的部分文本是一种样式,而该单元格中的其余文本是另一种样式? (意思是颜色、字体等)
标签: excel vba powerpoint