我有一份在 PowerPoint 中生成的报告,在许多图表的下方,都有文字告诉读者参考附录中的页面。我希望能够动态引用这些幻灯片。
例如,在图表下,我可能会显示文本“请参阅附录第 54 页”,但我需要将 54 链接到幻灯片,以便如果我插入另一张幻灯片,它会显示“55”。
这可以在 VBA 中实现吗?我不希望有人为我编写代码,我只是想在花几个小时尝试这样做之前知道这是否合理。
旁注:我觉得在这里问一个关于 MS Office 的问题很糟糕,但因为我相信它需要在 VBA 中实现(我不认为这个功能是默认内置的),所以我认为这是一个相关的问题。
我正在开发一个自定义工具,可以为给定的演示文稿生成自定义的教师注释。我遇到一个问题,我正在处理一个演示文稿,其中幻灯片基本上没有 Title 对象然后我运行代码,它将我的 if 语句与 .
我已将代码简化为基础内容,以使其尽可能简单。
我的测试课程有一张正常的幻灯片,其中填充了文本占位符,下一张幻灯片是一张徽标幻灯片,没有标题文本框,只有版权信息和徽标(这是有问题的幻灯片),然后是另一张幻灯片,其中标题占位符存在,但留空。
如何检查单个幻灯片以确保标题占位符存在?
Public Sub GetTitle()
Dim pres As Presentation 'PowerPoint presentation
Dim sld As Slide 'Individual slide
Dim shp As Shape 'EIAG Text Shape
Dim ShpType As String 'Shape Type
Dim SldTitle As String 'Slide TITLE
'Go through each slide object
Set pres = ActivePresentation
For Each sld In ActivePresentation.Slides.Range
On Error Resume Next
If sld.Shapes(1).PlaceholderFormat.Type = ppPlaceholderCenterTitle Or sld.Shapes(1).PlaceholderFormat.Type = ppPlaceholderTitle Then
If sld.Shapes.Title.TextFrame.TextRange <> "" Then
SldTitle = sld.Shapes.Title.TextFrame.TextRange
Debug.Print SldTitle …Run Code Online (Sandbox Code Playgroud) 我想克隆一张 PowerPoint 文档的幻灯片并使用 OpenXML 插入到同一个文档中。为此,我使用以下功能:
public static void AddNewSlide(PresentationPart parent, Slide _slideTemplate, string newId)
{
var newSlidePart = parent.AddNewPart<SlidePart>(newId);
newSlidePart.FeedData(_slideTemplate.SlidePart.GetStream(FileMode.Open));
newSlidePart.AddPart(_slideTemplate.SlidePart.SlideLayoutPart, _slideTemplate.SlidePart.GetIdOfPart(_slideTemplate.SlidePart.SlideLayoutPart));
newSlidePart.Slide.Save();
DocumentFormat.OpenXml.Presentation.SlideIdList slideIdList = parent.Presentation.SlideIdList;
uint maxSlideId = 1;
foreach (DocumentFormat.OpenXml.Presentation.SlideId slideId in slideIdList.ChildElements)
{
if (slideId.Id > maxSlideId) maxSlideId = slideId.Id;
}
DocumentFormat.OpenXml.Presentation.SlideId newSlideId = new DocumentFormat.OpenXml.Presentation.SlideId { Id = ++maxSlideId, RelationshipId = parent.GetIdOfPart(newSlidePart) };
slideIdList.Append(newSlideId);
}
Run Code Online (Sandbox Code Playgroud)
如果原始幻灯片只包含文本,它工作正常,但是当原始幻灯片也包含图像时,结果 PowerPoint 文档将被损坏。新幻灯片上的图像将不会显示,只会显示一条消息:“当前无法显示此图像。”
这是我修改 PowerPoint 演示文稿、将其另存为新文件、关闭它,然后尝试打开该文件的代码。
var doc = PresentationDocument.Open(@"d:\temp.pptx", true);
//... proccess presentation
doc.SaveAs(@"d:\temp2.pptx");
doc.Close();
var doc2 = PresentationDocument.Open(@"d:\temp2.pptx", false);
doc2.Close();
Run Code Online (Sandbox Code Playgroud)
我不明白为什么运行时会抛出异常:
该进程无法访问文件“x”,因为它正被另一个进程使用。
如何使用 VBA 更改 powerpoint 文件的“上次修改日期属性”?尝试在底部设置 f.FileDateModified 时出现错误。这是我到目前为止所拥有的:
Set oPresentation = Presentations.Open(sFolder & StrFile, , , False)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Dim fileModDate As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(sFolder & StrFile)
fileModDate = f.DateLastModified
Set oFile = fso.CreateTextFile(sFolder & StrFile & "meta.txt")
oFile.WriteLine "Last author: " & oPresentation.BuiltInDocumentProperties("Last author")
'oFile.WriteLine "Last save time: " & oPresentation.BuiltInDocumentProperties("Last save time")
oFile.WriteLine "Date last modified " & fileModDate
oFile.Close
Call oPresentation.SaveAs(sFolder & …Run Code Online (Sandbox Code Playgroud) 我一直在使用此代码来复制多个范围和图表。然而,随着我的代码的增长,它似乎失败了,在搜索了这个问题后,我认为这是由于图表/范围没有正确复制到剪贴板缓存/从剪贴板缓存复制而来。有没有办法避免这个错误?
错误 - “运行时错误'-2147188160(80048248)':Shapes.PasteSpecial:无效请求。剪贴板为空或包含可能无法粘贴到此处的数据”
Public Sub CopyPasteHeadcountTopGraph()
If PPT Is Nothing Then Exit Sub
If PPT_pres Is Nothing Then Exit Sub
Dim rng As Range
Dim mySlide As Object
Dim myShape As Object
Dim cht As Chart
Set mySlide = PPT_pres.Slides(6)
With mySlide
.Select
Set cht = ThisWorkbook.Worksheets("Headcount").ChartObjects("HcChart").Chart
cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture, Size:=xlScreen
.Shapes.Paste.Select 'ERROR HERE
'''''''''''''''''''''''''''''''''
'Paste as Chart and break link. '
'''''''''''''''''''''''''''''''''
'cht.ChartArea.Copy
'.Shapes.Paste.Select
'With .Shapes("HcChart")
'.LinkFormat.BreakLink
'End With
PPT_pres.Windows(1).Selection.ShapeRange.Left = 35
PPT_pres.Windows(1).Selection.ShapeRange.Top = 110
PPT_pres.Windows(1).Selection.ShapeRange.Width = 655 …Run Code Online (Sandbox Code Playgroud) 我在 PowerPoint 中创建了一个表格,我想将每个单元格中的文本水平居中。我使用了MsoHorizontalAnchor.msoAnchorCenter。
如果文本很短且在一行中,则效果很好,但如果文本很长,则显示在 1 行以上且不居中。编码:
myShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "shortOK";
myShape.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "Long text is not displayed centered";
myShape.Table.Cell(1, 1).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;
myShape.Table.Cell(1, 2).Shape.TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorMiddle;
myShape.Table.Cell(1, 1).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;
myShape.Table.Cell(1, 2).Shape.TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorCenter;
Run Code Online (Sandbox Code Playgroud)
如果显示在 1 法以内,您知道如何使文本居中吗?
我正在从 Excel 电子表格自动生成 PowerPoint 报告。在我粘贴表格之前,我已经完成了这个过程。
我正在使用PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")该表格将表格粘贴到 PowerPoint ,表格在我的幻灯片上显示为一个形状(第三个形状)。
要引用我使用的新形状,Set pShape = Slide2.Shapes(Slide2.Shapes.Count)但现在当我粘贴时,pShape分配了“形状 2”(而不是“形状 3”)。在粘贴和分配对象之间有什么需要做的吗?
下面的代码,评论了问题发生的地方。(已删除完整代码;可在此处查看)
'Copy tables from Excel
Set rng = ws.Range("A:A")
rng.ColumnWidth = 22.75
Set rng = ws.Range("A4:C27")
'Copy the table range
Application.CutCopyMode = False
rng.Copy
Application.Wait (Now + TimeValue("0:00:02"))
'The issue occurs here!!! '-------------------------------------
'Paste the table in to the slide
Slide2.Select
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
'Name the new shape object
Set pShape = Slide2.Shapes(Slide2.Shapes.Count)
pShape.Name = "Slide_2_Table_1"
pShape.LockAspectRatio = …Run Code Online (Sandbox Code Playgroud) 在幻灯片上添加 ROUNDED_RECTANGLE 时,通过使用 pptx,我生成了以下几行和幻灯片。
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(1), Inches(0.5), Inches(8), Inches(2))
prs.save('c:\\PPT\\round rectangle.pptx')
Run Code Online (Sandbox Code Playgroud)
如果是手动的,可以调整圆角(红色箭头指向的地方)。
有没有办法从脚本中控制它?谢谢你。
我有一个包含 50 张幻灯片的文件。我需要创建 50 个不同的文件,每个文件都包含其中一张幻灯片。我想最快的方法包括 VBA,但我不知道如何让 VBA 创建一个新文件,然后回到主人那里。
powerpoint ×10
vba ×6
c# ×3
excel ×2
ms-office ×2
openxml-sdk ×2
openxml ×1
python ×1
python-pptx ×1
shapes ×1