#vba #powerpoint
#vba #powerpoint
Вопрос:
Я пытаюсь изменить существующий приведенный ниже код PowerPoint VBA, чтобы включить текст формы в дополнение к другим перечисленным атрибутам. Цель этого кода — извлечь каждую фигуру / текстовое поле и его атрибуты из PowerPoint и поместить их в таблицу.
Строка, которую я добавил, была приведена ниже, и я пробовал oSh.TextFrame oSh.TextRange и комбинацию, но безуспешно. Он возвращает файл с заголовками, но полностью пустой. Есть идеи, что я делаю не так и почему это не сработает?
amp; oSh.Text amp; vbTab _
Полный код:
Sub ExportCoords()
Dim oSlides As Slides
Dim oSl As Slide
Dim oSh As Shape
Dim strOutput As String
Dim strFileName As String
Dim intFileNum As Integer
Dim lngReturn As Long
' Get a filename to store the collected text
strFileName = InputBox("Enter the full path and name of file to save info to", "Output file?")
' did user cancel?
If strFileName = "" Then
Exit Sub
End If
' is the path valid? crude but effective test: try to create the file.
intFileNum = FreeFile()
On Error Resume Next
Open strFileName For Output As intFileNum
If Err.Number <> 0 Then ' we have a problem
MsgBox "Couldn't create the file: " amp; strFileName amp; vbCrLf _
amp; "Please try again."
Exit Sub
End If
Close #intFileNum ' temporarily
strOutput = "Slide" amp; vbTab amp; "Name" amp; vbTab amp; "Text" amp; vbTab amp; "Type" _
amp; vbtab amp; "Left" amp; vbTab amp; "Top" amp; vbTab amp; "width" _
amp; vbTab amp; "height" amp; vbCrLf
' Get the info
Set oSlides = ActivePresentation.Slides
For Each oSl In oSlides
For Each oSh In oSl.Shapes
strOutput = strOutput _
amp; oSl.SlideIndex amp; vbTab _
amp; oSh.Name amp; vbTab _
amp; oSh.Text amp; vbTab _
amp; osh.Type amp; vbtab _
amp; oSh.Left amp; vbTab _
amp; oSh.Top amp; vbTab _
amp; oSh.width amp; vbTab _
amp; oSh.height amp; vbCrLf
Next oSh
Next oSl
' now write the text to file
Open strFileName For Output As intFileNum
Print #intFileNum, strOutput
Close #intFileNum
' show what we've done
lngReturn = Shell("NOTEPAD.EXE " amp; strFileName, vbNormalFocus)
End Sub
Ответ №1:
Вместо oSh.Text следует использовать oSh.TextFrame .TextRange.Текст, затем он получит текст внутри фигуры.
Это происходит потому, что объект TextFrame имеет другие свойства, помимо текстового значения. Например, в приведенном ниже коде (из https://learn.microsoft.com/pt-br/office/vba/api/powerpoint.shape.textframe ), вы можете установить поле и значение текста.
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes _
.AddShape(msoShapeRectangle, 180, 175, 350, 140).TextFrame
.TextRange.Text = "Here is some test text"
.MarginTop = 10
End With
Комментарии:
1. Это вытащило все, но похоже, что это сложно, когда в текст формы включено несколько абзацев (см. Скриншот выше). Есть идеи, почему это происходит?
2. Это происходит потому, что при создании txt, если текст содержит несколько абзацев, он будет содержать символы «возврата каретки» вместе с текстом. В случае, если вы хотите избежать этого, вы можете использовать: amp; Replace(oSh.TextFrame . TextRange. Текст, Chr(13), «») _
3. Возможно, в зависимости от версии, он также будет содержать символы перевода строки. Вы также можете удалить те, которые заменяют Chr (10), если это произойдет. Ссылка: pt.wikipedia.org/wiki/Ficheiro:ASCII-Table-wide.svg