PPT文档编辑的完美解决方案

2012-04-29 00:44:03龚建华
电脑知识与技术 2012年24期

龚建华

摘要:基于VBA对Microsoft office PowerPoint编程,笔者提出了一套PPT文字编辑的快速解决方案,大幅提高了PPT制作效率。该文详细描述了编程思路、关键代码、导入文档的编辑原则和宏程序应用规范,设计了通过导入文档一次性构建PPT文本框架和仅仅更新现有PPT文本内容的“开关”,以适应不同制作阶段或不同使用者的需要。

关键词:VBA编程;PPT制作;文本导入

中图分类号:TP37文献标识码:A文章编号:1009-3044(2012)24-5885-03

在教学、科研、商务诸多工作中,PowerPoint(PPT)的应用十分广泛。因此,PPT的制作技巧成为现代成功人士必须掌握的重要技能之一。快速制作一个优秀的PPT作品,需要事先准备好文字、图片和音像等资料。在已经编辑好了文字资料的情况下,通过复制和粘贴的方法,一张张的幻灯片骤一制作,工作效率很低;采用PowerPoint提供的在“大纲”模式下进行复制和粘贴的方法,只能一次性将文字粘贴到标题文本框中,工作效率仍然不高。笔者采取VBA编程方法,完美地解决了上述问题,大幅提高了PPT制作效率。

1 PPT文字编辑解决方案

打开或新建一个PPT文档,按Alt+F11,打开Visual Basic编辑器。首先引用Microsoft Word 11.0 Object Library和Wimdows Script Host Object Model对象,然后插入一个模块,在模块中建立一个名为“导入PPT”的过程,编程思路及关键代码如下:

1.1实现自动查找导入文件功能。

Dim MyFile As String, MyFile1 As String

With Application.FileDialog(msoFileDialogFilePicker)

.AllowMultiSelect = False

.Filters.Clear

.Filters.Add "Word文件", "*.doc".Filters.Add "文该文件", "*.txt"

If .Show = -1 Then MyFile1 = .SelectedItems(1) Else Exit Sub

End With

1.2如果导入文件为Word文件,则转换为文该文件。

If Right(MyFile1, 3) = "doc" Then

On Error GoTo gjh5033

GetDocText1 = " "

Dim wapp As Word.Application Dim doc As Document

Dim GetDocText1 As String

Set wapp = New Word.Application

wapp.Visible = True

Set doc = wapp.Documents.Open(MyFile1) GetDocText1 = doc.Content.Text

Dim sFile As Object, FSO As Object

MyFile1 = Left(MyFile1, Len(MyFile1) - 4) & ".txt"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set sFile = FSO.CreateTextFile(MyFile1, True)

sFile.WriteLine (GetDocText1)

sFile.Close

Set sFile = Nothing

Set FSO = Nothing

Name MyFile1 As MyFile

gjh5033:

If Not (doc Is Nothing) Then

doc.Close

Set doc = Nothing

End If

If Not (wapp Is Nothing) Then

wapp.Quit

Set wapp = Nothing

End If

On Error GoTo 0

End If

1.3获取将写入到每张幻灯片的标题、内容和备注

On Error Resume Next

Dim code As String, code1 As String, code2 As String

Dim str4() As Integer, n As Integer, m As Integer

Dim str5() As String, str6() As String, str7() As String

Dim dfdf As Boolean

code1 = "-----------"幻灯片内容分隔符

code2="备注:"备注标示符

n = 0: m = 0: dfdf = False

Open MyFile For Input As #1 Do While Not EOF(1)

code = ""

Line Input #1, code

code = Trim(CStr(code))

m = m + 1

If code = code1 Then

n = n + 1

ReDim Preserve str4(n) As Integer

ReDim Preserve str5(n) As String

ReDim Preserve str6(n) As String

ReDim Preserve str7(n) As String

str4(n) = m

dfdf = False

Else

If str4(n) + 1 = m Then

str6(n) = code

Else

If code = code2 Then dfdf = True

If dfdf = False Then

str5(n) = str5(n) & code & vbCrLf

Else

If code <> code2 Then str7(n) = str7(n) & code & vbCrLf

End If

End If

End If

Loop

Close #1

On Error GoTo 0

1.4将文件内容全部导入到幻灯片

Dim oPres As Presentation

Set oPres = Application.ActivePresentation

Dim oSlide As Slide

Dim oShape As Shape

Dim tr As TextRange

Dim i As Long, j As Long

YesNo = MsgBox("导入文档请按:确定" & vbCrLf & "更新内容请按:取消", vbOKCancel, "导入/更新")

If YesNo = "1" Then

For i = 2 To oPres.Slides.Count

ActiveWindow.Selection.SlideRange.Delete

Next

End If

If n > 1 Then

For i = 2 To n

If oPres.Slides.Count < i Then

ActiveWindow.View.GotoSlide Index:=ActivePresentation.Slides.Add(Index:=i, Layout:=ppLayoutText).SlideIndex

End If

Set oSlide = oPres.Slides.Item(i)

For j = 1 To 2)

Set oShape = oSlide.Shapes.Item(j)

If oShape.Name = "Rectangle 2" Then

oShape.TextFrame.TextRange.Text = str6(i - 1) End If

If oShape.Name = "Rectangle 3" Then

oShape.TextFrame.TextRange.Text = str5(i - 1)

End If

Set oShape = Nothing

NextPowerPoint.ActivePresentation.Slides(i).NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = str7(i - 1)

Set oSlide = Nothing

Next

Set oPres = Nothing

End If

2导入文档编辑时应注意的几个问题

登录APP查看全文