【问题标题】:Using VBA to change the font size of embedded content (HTML) from Excel to Powerpoint使用 VBA 将嵌入内容 (HTML) 的字体大小从 Excel 更改为 Powerpoint
【发布时间】:2021-01-11 18:24:30
【问题描述】:

我正在尝试编写代码以将 Excel 工作表中的内容复制到 Powerpoint(我对编程或任何东西一无所知,并且用谷歌搜索了所有内容 - 我的代码可能搞砸了)。我设法将内容复制到 Powerpoint 并将其作为 HTML 嵌入其中(因为我希望能够在复制后在 Powerpoint 中编辑复制的数据),这是代码:

Sub ExportContent()

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)

'open ppt
Set PPTApp = CreateObject("PowerPoint.Application")
PPTApp.Visible = msoTrue
Set pptPres = PPTApp.Presentations.Add(msoTrue)
Set pptslide = pptPres.Slides.Add(Index:=1, Layout:=12)

'copy Excel content
Set rng = ws.Range("A1:G50")
rng.Copy

'copy to ppt
pptslide.Shapes.PasteSpecial DataType:=8
Set excontent = pptslide.Shapes(pptslide.Shapes.Count)

'change font
With excontent.Shapes(1).TextFrame.TextRange.Characters
  .Font.Size = 30
End With

但是,我正在努力使用 Excel VBA 更改复制内容的字体大小的代码。在 Powerpoint 中,您可以选择所有内容并点击字体大小旁边的小箭头来增加 everything 的字体大小。所以我的问题是:

  1. 甚至可以使用 Excel VBA 做到这一点吗?
  2. 如果是,怎么做?我已经尝试过之前在网上找到的解决方案(例如this),但我总是遇到编译错误。

非常感谢您的帮助!

【问题讨论】:

    标签: excel vba powerpoint font-size


    【解决方案1】:
    Sub ExportContent()
    ' two new variables:
    Dim lCol As Long
    Dim lRow As Long
    ' And if you're not already doing so outside this snippet
    ' you should be declaring ALL of your variables
    ' Add OPTION EXPLICIT to the top of every module. 
    ' It'll keep you honest.
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets(1)
    
    'open ppt
    Set PPTApp = CreateObject("PowerPoint.Application")
    PPTApp.Visible = msoTrue
    Set pptPres = PPTApp.Presentations.Add(msoTrue)
    Set pptslide = pptPres.Slides.Add(Index:=1, Layout:=12)
    
    'copy Excel content
    Set rng = ws.Range("A1:G50")
    rng.Copy
    
    'copy to ppt
    pptslide.Shapes.PasteSpecial DataType:=8
    Set excontent = pptslide.Shapes(pptslide.Shapes.Count)
    
    'change font
    ' The pasted HTML will come into PPT as a table, 
    ' so you need to treat it as such:
        With excontent.Table
            For lCol = 1 To .Columns.Count
                For lRow = 1 To .Rows.Count
                    .Cell(lRow, lCol).Shape.TextFrame.TextRange.Font.Name = "Algerian"
                    ' do whatever formatting to the text you want to do here
                    ' or work with .Cell(lRow, lCol).Shape to change cell fills etc
                Next
            Next
        End With
    

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多