2013-07-12 4 views
2

Я привык делать подобные вещи в Word, но методы такого же типа в PowerPoint совершенно разные.Как импортировать текстовый файл в PowerPoint с помощью vba?

У меня есть сетевая папка со списком текстовых файлов. Я хочу иметь возможность щелкнуть текстовое поле на слайде, а затем нажать кнопку импорта в пользовательском меню. Затем я выбираю один из текстовых файлов в диалоговом окне и импортирую текст в указанную область.

Я создал пользовательское меню в 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 
+0

Может быть, вы могли бы добавить код для MS-Word в качестве ссылки. что поможет понять, что вы пытаетесь сделать –

+0

Получите текст и поместите его в форму в Powerpoint. Где в Powerpoint вы хотите добавить текст? Что такое «методы»? Опубликуйте какой-нибудь Word-код! –

+0

Привет, я добавил рабочий код, чтобы дать вам лучшее представление о том, что я пытаюсь сделать. Любая помощь или совет будут оценены. – Boyplunder

ответ

0

кода PowerPoint, чтобы установить текст TextBox или другой формой, является:

ActivePresentation.Slides(1).Shapes(3).TextFrame.TextRange _ 
    = "Hello there" 
+0

Спасибо AndyG. Взгляните на рабочий код, который я добавил в свой первоначальный вопрос. Ваш вклад будет оценен. – Boyplunder

0

Я хочу иметь возможность щелкнуть текстовое поле на слайде, а затем нажать кнопку импорта в пользовательском меню. Затем я выбираю один из текстовых файлов в диалоговом окне и импортирую текст в указанную область.

' 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 
Смежные вопросы