首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将excel范围复制到powerpoint时的字体大小

将excel范围复制到powerpoint时的字体大小
EN

Stack Overflow用户
提问于 2015-01-27 14:39:54
回答 2查看 2.5K关注 0票数 0

我正在尝试使用VBA将一个范围从Excel复制到Powerpoint。一旦我运行了VBA宏。该范围被粘贴到字体大小为6的powerpoint中。我希望在粘贴到powerpoint时字体大小为9。

代码如下:

代码语言:javascript
复制
Sub ExcelRangeToPowerPoint()

Dim rng As Excel.Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Dim myShapeRange As PowerPoint.Shape

'Columns("M:M").Select
'Columns("M:M").EntireColumn.AutoFit
'Copy Range from Excel
  Set rng = ThisWorkbook.ActiveSheet.Range("C8:M56")

'Create an Instance of PowerPoint
  On Error Resume Next

    'Is PowerPoint already opened?
      Set PowerPointApp = GetObject(Class:="PowerPoint.Application")

    'Clear the error between errors
      Err.Clear

    'If PowerPoint is not already open then open PowerPoint
      If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(Class:="PowerPoint.Application")

    'Handle if the PowerPoint Application is not found
      If Err.Number = 429 Then
        MsgBox "PowerPoint could not be found, aborting."
        Exit Sub
      End If

  On Error GoTo 0

'Make PowerPoint Visible and Active
  PowerPointApp.Visible = True
  PowerPointApp.Activate

'Create a New Presentation
  Set myPresentation = PowerPointApp.Presentations.Add

'Add a slide to the Presentation
  Set mySlide = myPresentation.Slides.Add(1, ppLayoutTitleOnly)

'Copy Excel Range
  rng.Copy

'Paste to PowerPoint and position
  mySlide.Shapes.Paste

   'Special DataType:=ppPasteEnhancedMetafile


  Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)


      myShapeRange.Left = 25
      'myShapeRange.Top = 27
      myShapeRange.Width = myPresentation.PageSetup.SlideWidth - 30
      myShapeRange.Height = myPresentation.PageSetup.SlideHeight - 120

    'Set position:
    'PowerPointApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, msoTrue
    'PowerPointApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, msoTrue


'Clear The Clipboard
  Application.CutCopyMode = False

End Sub
EN

回答 2

Stack Overflow用户

发布于 2015-01-27 16:01:18

修改原始代码以包含以下内容,而不是复制Excel范围并将其粘贴到PowerPoint (这将生成一个本机PowerPoint表)。这将同时填充PowerPoint中的表并更改字体大小。

代码语言:javascript
复制
Dim tblShape As PowerPoint.Shape

    With mySlide.Shapes
        Set tblShape = .AddTable(NumRows:=rng.Rows.Count, NumColumns:=rng.Columns.Count, Left:=30, _
                                    Top:=110, Width:=660, Height:=320)
    End With
    Dim i As Long, j As Long, k As Long, l As Long
    k = 1 ' PowerPoint Table row
    For i = rng.Row To rng.Row + rng.Rows.Count - 1
        l = 1 'PowerPoint Table column
        For j = rng.Column To rng.Column + rng.Columns.Count - 1
            tblShape.Table.Cell(k, l).Shape.TextFrame.TextRange.Text = ThisWorkbook.ActiveSheet.Cells(i, j).Value
            tblShape.Table.Cell(k, l).Shape.TextFrame.TextRange.Font.Size = 15
            l = l + 1
        Next j
        k = k + 1
    Next i 
票数 0
EN

Stack Overflow用户

发布于 2015-01-30 08:55:45

如果您想保留源格式,只需使用特殊粘贴而不是粘贴,因此请将mySlide.Shapes.Paste更改为mySlide.Shapes.PasteSpecial ppPasteOLEObject

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28164635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档