如何使用vba将文本文件导入PowerPoint?

如何使用vba将文本文件导入PowerPoint?,vba,powerpoint,Vba,Powerpoint,我习惯在Word中做这类事情,但在PowerPoint中做同一类事情的方法却大不相同 我有一个包含文本文件列表的网络文件夹。我希望能够单击幻灯片上的文本字段,然后单击自定义菜单上的导入按钮。然后,我在对话框中选择一个文本文件,该文件将文本导入我指定的区域 我在PowerPoint 2010中构建了自定义菜单,并添加了一些其他宏,但我无法确定使用的方法 谁能给我一个开始吗 以下是我正在使用的代码: Sub GetTextFromLibrary() Dim lCurrentView As Long

我习惯在Word中做这类事情,但在PowerPoint中做同一类事情的方法却大不相同

我有一个包含文本文件列表的网络文件夹。我希望能够单击幻灯片上的文本字段,然后单击自定义菜单上的导入按钮。然后,我在对话框中选择一个文本文件,该文件将文本导入我指定的区域

我在PowerPoint 2010中构建了自定义菜单,并添加了一些其他宏,但我无法确定使用的方法

谁能给我一个开始吗

以下是我正在使用的代码:

Sub GetTextFromLibrary()

Dim lCurrentView As Long
Dim SlideNum As Integer
Dim Name$
Dim OldName$

'Store the default shape name to reset later
OldName$ = ActiveWindow.Selection.ShapeRange(1).Name
'Now rename the shape to work with it
Name$ = "temp01"

MsgBox "You are on slide: " & _
        OldName$, vbInformation

ActiveWindow.Selection.ShapeRange(1).Name = Name$

  ' Get the current view type.
  lCurrentView = ActiveWindow.ViewType

  ' Make sure that PowerPoint is in Slide view.
  ' ActiveWindow.Selection.SlideRange.SlideNumber produces an error if
  ' you are using any other view.
  If lCurrentView = ppViewNormal Then
     ' Display the slide number.
     'MsgBox "You are on slide: " & _
        ActiveWindow.Selection.SlideRange.SlideNumber, vbInformation

    SlideNum = ActiveWindow.Selection.SlideRange.SlideNumber

    MsgBox "You are on slide: " & _
        SlideNum, vbInformation

' Dim a variable as a specific object type
Dim oShape As Shape

' Set it to "point" to a specific shape:
Set oShape = ActivePresentation.Slides(SlideNum).Shapes("temp01")

'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Declare a variable for the directory path.
Dim directory As String
'Set the directory path
directory = "C:\Documents and Settings\<USER>\Desktop\PitchTemplateLibrary\Quotes"
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd
    'Change the initial directory\filename
    .InitialFileName = directory
    'Use the Show method to display the File Picker dialog box and return the user's action.
    'The user pressed the button.
    If .Show = -1 Then

        'Step through each string in the FileDialogSelectedItems collection.
        For Each vrtSelectedItem In .SelectedItems

Dim fs As Object
Dim f As Object
Const ForReading = 1, ForWriting = 2, ForAppending = 3

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(vrtSelectedItem, 1, 0)

' Put the text into the text box
oShape.TextFrame.TextRange.Text = f 'oShape IS EMPTY, BUT CAN"T SEE WHY.

        Next vrtSelectedItem
    'The user pressed Cancel.
    Else
    End If
End With

' Until we release the memory used by oShape
Set oShape = Nothing

ActiveWindow.Selection.ShapeRange(1).Name = OldName$

  Else
     ' PowerPoint is not in slide view.
     MsgBox "You must be in slide view to run this macro.", _
        vbInformation
  End If


'Set the object variable to Nothing.
Set fd = Nothing

End Sub
子GetTextFromLibrary()
暗淡的L当前视图为长
Dim SlideNum作为整数
模糊名称$
暗淡的旧名$
'存储默认形状名称以稍后重置
OldName$=ActiveWindow.Selection.ShapeRange(1.Name)
'现在重命名形状以使用它
名称$=“temp01”
MsgBox“您在幻灯片上:&”_
OldName$,vb信息
ActiveWindow.Selection.ShapeRange(1).Name=Name$
'获取当前视图类型。
lCurrentView=ActiveWindow.ViewType
'确保PowerPoint处于幻灯片视图中。
'ActiveWindow.Selection.SlideRange.SlideNumber在以下情况下生成错误
'您正在使用任何其他视图。
如果lCurrentView=ppViewNormal,则
'显示幻灯片编号。
“MsgBox”您在幻灯片上:&_
ActiveWindow.Selection.SlideRange.SlideNumber,vbInformation
SlideNum=ActiveWindow.Selection.SlideRange.SlideNumber
MsgBox“您在幻灯片上:&”_
SlideNum,vb信息
'将变量设置为特定对象类型
形状暗淡
'将其设置为“指向”特定形状:
设置oShape=ActivePresentation.Slides(SlideNum.Shapes)(“temp01”)
'将变量声明为FileDialog对象。
将fd设置为文件对话框
'为目录路径声明一个变量。
将目录设置为字符串
'设置目录路径
directory=“C:\Documents and Settings\\Desktop\PitchTemplateLibrary\Quotes”
'将FileDialog对象创建为文件选择器对话框。
Set fd=Application.FileDialog(msoFileDialogFilePicker)
'声明一个包含路径的变量
'每个选定项目的。尽管道路艰难,
'变量必须是变量,因为对于每个…下一个
'例程仅适用于变量和对象。
Dim VRT选择编辑项作为变量
'使用With…End With块引用FileDialog对象。
有fd
'更改初始目录\文件名
.InitialFileName=目录
'使用Show方法显示“文件选择器”对话框并返回用户的操作。
”用户按下了按钮。
如果.Show=-1,则
'逐步浏览FileDialogSelectedItems集合中的每个字符串。
对于中的每个VRT选择编辑项。选择编辑项
作为对象的Dim fs
将f作为对象
读取常数=1,写入常数=2,外观常数=3
设置fs=CreateObject(“Scripting.FileSystemObject”)
Set f=fs.OpenTextFile(vrtSelectedItem,1,0)
'将文本放入文本框中
oShape.TextFrame.TextRange.Text=f'oShape为空,但看不出原因。
下一个vrtSelectedItem
'用户按了“取消”。
其他的
如果结束
以
'直到我们释放oShape使用的内存
设置oShape=无
ActiveWindow.Selection.ShapeRange(1).Name=OldName$
其他的
'PowerPoint不在幻灯片视图中。
MsgBox“您必须在幻灯片视图中才能运行此宏。”_
vb信息
如果结束
'将对象变量设置为Nothing。
设置fd=无
端接头

PowerPoint设置文本框或其他形状的代码为:

    ActivePresentation.Slides(1).Shapes(3).TextFrame.TextRange _
    = "Hello there"
我希望能够单击幻灯片上的文本字段,然后单击自定义菜单上的导入按钮。然后在对话框中选择一个文本文件,将文本导入到我指定的区域


也许您可以添加ms word的代码作为参考。这将有助于理解您试图在Powerpoint中添加文本并使其形成形状的内容。您希望在Powerpoint中添加文本的位置?什么是“方法”“你不一样吗?发布一些单词代码!您好,我添加了工作代码,让您更好地了解我正在尝试做什么。任何帮助或建议都将不胜感激。谢谢。看看我添加到原始问题中的工作代码。如果您能提供意见,我们将不胜感激。
' CYA
If Not ActiveWindow.Selection.Type = ppSelectionText Then
  MsgBox "Select some text first"
  Exit Sub
End if

' You might also want to allow for the case where the user has
' selected a rectangle or other shape rather than a text range.
' You could add the text to the shape as well.

With ActiveWindow.Selection.TextRange
  .TextFrame.Text = "Text you've read from the chosen file"
End With