Powerpoint 在幻灯片之间插入来自外部 TXT 文件的文本 [英] Powerpoint inserting text from external TXT file between slides

查看:191
本文介绍了Powerpoint 在幻灯片之间插入来自外部 TXT 文件的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用下面的代码(不是我的代码)成功地让用户打开演示文稿并从标题幻灯片中,能够从他们计算机上的任何位置选择一个 .txt 文件,并让 Powerpoint 将文本导入到Powerpoint 并根据我设置的母版幻灯片格式创建幻灯片.

I have been using the code below (not my code) with success to have users open a presentation and from the title slide, be able to select a .txt file from any location on their computer and have Powerpoint import the text into the Powerpoint and create the slides adhering to the master slide formatting I have set.

Sub AddSlides(text As String)
Dim Pre As Presentation
Dim Sld As Slide

Set Pre = ActivePresentation
Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count + 1, Layout:=1)
Sld.Shapes(1).TextFrame.TextRange = text
End Sub

Sub ReadFile(sFileName As String)

Dim iFileNum As Integer
Dim sBuf As String

' edit this:
'sFileName = "test.csv"

' does the file exist?  simpleminded test:
If Len(Dir$(sFileName)) = 0 Then
    Exit Sub
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum

Do While Not EOF(iFileNum)
    Line Input #iFileNum, sBuf
    AddSlides (sBuf)
Loop

' close the file
Close iFileNum

End Sub

Sub SelectFile()

Dim In_file As Variant

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)

dlgOpen.AllowMultiSelect = False

If dlgOpen.Show = -1 Then
In_file = dlgOpen.SelectedItems.Item(1)
ReadFile (In_file)
End If

End Sub

但是,现在我想使用部分,有效地创建标题和结论幻灯片.第 1 部分将包括标题幻灯片和供用户选择其 .txt 文件的按钮.第 2 部分将由一张幻灯片组成,用于结束演示.我的问题是,当代码从 .txt 文件生成幻灯片时,它会将它们放在第 2 节的结论幻灯片之后,而不是第 1 节的标题幻灯片之后.

However, now I would like to work with sections, effectively creating a title and a conclusion slide. Section 1 would include the title slide and button for users to select their .txt file. Section 2 would consist of a single slide that concludes the presentation. My problem is, when the code generates the slides from the .txt file, it places them after the conclusion slide in Section 2 instead of after the title slide in Section 1.

我研究了用于处理节的各种代码以及用于从外部文件导入/插入的代码,但在与它们合作来实现这一目标方面没有成功.

I have researched various codes for working with sections and codes for importing/inserting from external files and have had no success working with them to achieve this.

虽然我希望在第一张和最后一张幻灯片之间生成的幻灯片数量是可变的,但如果更可行,我可以指定可以生成多少张幻灯片.如果确实需要指定这一点,如果这是一个更可行的选择,我也可以先创建幻灯片,然后用 .txt 文件中的文本填充它们.

Although I wanted the number of slides generated between the first and last slides to be variable, I can specify how many slides can be generated if this is more feasible. If this does need to be specified, I would also be comfortable creating the slides first and have them populated with the text from the .txt file if this is a more workable option.

感谢您对此的任何帮助.

Appreciate any help with this.

注意:当前代码将文本导入限制为每张幻灯片上的单行.如果有一种简单的方法可以将其附加到每张幻灯片中包含 2 行 - 那将非常有用.

Note: Current code limits text import to single lines on each slide. If there is a simple way to append this to include 2 lines per slide - that would be extremely useful.

推荐答案

好的,我将从放置新幻灯片的位置开始.您需要更改 addSlides 函数,以便将所有幻灯片放在结论幻灯片的位置.这很简单,你只需从

Ok, I'll start with where to put the new slides. You need to change the addSlides function so that it places all of the slides in the position of your conclusion slide. This ones easy, you just change the index from

Index:=Pre.Slides.Count + 1

Index:=Pre.Slides.Count

使 addSlides 函数如下:

Making the addSlides function as follows:

Sub AddSlides(text As String)
   Dim Pre As Presentation
   Dim Sld As Slide

   Set Pre = ActivePresentation
   Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count, Layout:=1)
   Sld.Shapes(1).TextFrame.TextRange = text
End Sub

关于你的第二个问题,每张幻灯片有两行文字,这稍微有点困难.您需要阅读每一行,每次到达第二行时,添加页面,然后重置保持变量.类似以下的内容应该可以工作:

On your second issue, getting two lines of text per slide, this is mildly more difficult. You need to read each line, and everytime you get to the second line, add the page, then reset the holding variable. Something like the following should work:

Sub ReadFile(sFileName As String)

Dim iFileNum As Integer
Dim sBuf As String
Dim bFlag As Boolean
Dim sHolder As String
' edit this:
'sFileName = "test.csv"

' does the file exist?  simpleminded test:
If Len(Dir$(sFileName)) = 0 Then
    Exit Sub
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum
bFlag = False
Do While Not EOF(iFileNum)
    If bFlag = False Then
        Line Input #iFileNum, sBuf
        holder = sBuf
        bFlag = True
    Else
        Line Input #iFileNum, sBuf
        holder = holder & vbCrLf & sBuf
        addSlides (holder)
        holder = ""
        bFlag = False
    End If

Loop

' close the file
Close iFileNum

End Sub

这篇关于Powerpoint 在幻灯片之间插入来自外部 TXT 文件的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆