|
本帖最后由 leikaiyi123 于 2017-8-8 16:20 编辑
经测试,发现4楼的代码仍有两个问题:
1、前面有首行缩进,粘贴后的图片左边仍有一段空白
2、最好将2、4楼综合一下,有打开的ppt就粘贴到打开的ppt中,若没有则新建一个ppt
修改如下,请指正:Sub Copy2PPT() '嵌入图形复制发送到ppt
Dim pptApp As Object, ptPre As Object, mySlide As Object
Dim x As Object
On Error Resume Next
If Selection.Type <> wdSelectionNormal Then
MsgBox "请先选择要导出的内容。仅支持嵌入式图形和文本。"
Exit Sub
End If
oldPageWidth = Selection.PageSetup.PageWidth
sj = Selection.ParagraphFormat.FirstLineIndent '首行缩进
Selection.ParagraphFormat.FirstLineIndent = CentimetersToPoints(0)
Selection.EndKey Unit:=wdLine '光标移到末尾
rightPos = Selection.Information(wdHorizontalPositionRelativeToPage) '获得光标的LEFT位置
newPageWidth = rightPos + Selection.PageSetup.RightMargin + 2 '+右页边距
Selection.PageSetup.PageWidth = newPageWidth '改变页宽
Application.ScreenRefresh '刷新屏幕
Selection.Paragraphs(1).Range.Select '选中该段
Selection.Copy
Err.Clear
Set pptApp = GetObject(, "PowerPoint.Application") '获取打开的ppt
If Err.Number > 0 Then '若没有打开ppt(出错信息>0)
'MsgBox "没有打开的PPT文件。"
'Selection.PageSetup.PageWidth = oldPageWidth '出错恢复原页宽
'Exit Sub
Set pptApp = CreateObject("PowerPoint.Application") '则新建一个ppt
Set ptPre = pptApp.Presentations.Add
Set mySlide = ptPre.Slides.Add(1, 12)
pptApp.Visible = msoTrue
End If
Set mySlide = pptApp.ActiveWindow.View.Slide
With mySlide
Set x = .Shapes.PasteSpecial(2) '非格式粘贴
x.Left = 100: x.Top = 100
End With
Selection.PageSetup.PageWidth = oldPageWidth '恢复原页宽
Selection.ParagraphFormat.FirstLineIndent = sj '恢复原缩进值
pptApp.Activate
Set x = Nothing
Set pptApp = Nothing
Set ptPre = Nothing
Set mySlide = Nothing
End Sub
|
|