codewala

code it

Macro to retain paragraph formatting when text is copied from Word to PowerPoint

In my earlier post I had mentioned about paragraph formatting is lost when text is copied from Word to PowerPoint.

Wrote a macro in PowerPoint to retain the paragraph formatting of the copied text from Word to PowerPoint.


'--------------------------------------------------
Option Explicit
'
'
'
Sub CopyTextFromWordWithParaFormat()
'
' FOR WORD
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdFile As String
Dim oTable As Word.Table
Dim oRow As Word.Row
Dim oParas As Word.Paragraphs
Dim oPara As Word.Paragraph
Dim paraBullet()
Dim paraIndentLevel()
Dim p As Long
Dim wdParaCount As Long
Dim counter As Long
'
' FOR POWERPOINT
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim oSlide As Slide
Dim oShape As PowerPoint.Shape
Dim ppParaCount As Long
Dim path As String
'
' INITILIZE
Set ppApp = PowerPoint.Application
Set ppPres = ActivePresentation
path = ppPres.path
p = 1
counter = 1
wdParaCount = 0
ppParaCount = 0
'
'
'-----------------------------------------------------
' SWITCH TO WORD TO READ THE CONTENT
'-----------------------------------------------------
Set wdApp = CreateObject("Word.Application")
wdFile = path & "/" & "sample.doc"
'
Set wdDoc = wdApp.Documents.Open(wdFile)
wdApp.Visible = True
wdDoc.Activate
'
'
' READ WORD TABLE
Set oTable = wdDoc.Tables(1)
Set oRow = oTable.Rows(1)
Set oParas = oRow.Cells(1).Range.Paragraphs
'
wdParaCount = oParas.Count
'
' GET PARAFORMATTING OF EACH PARAGRAPH
ReDim paraBullet(wdParaCount)
ReDim paraIndentLevel(wdParaCount)
'
For p = 1 To wdParaCount
'
Set oPara = oParas(p)
'
' ONLY INCLUDE THOSE PARA'S WHICH ARE NOT EMPTY
If (Len(oPara.Range.Text) > 1) Then
paraBullet(counter) = oPara.Range.ListFormat.ListType
paraIndentLevel(counter) = oPara.Range.ListFormat.ListLevelNumber
counter = counter + 1
End If
'
Next p
'
'
' COPY THE TEXT FROM WORD
oRow.Cells(1).Select
SendKeys "^c", 1
'
'
'---------------------------------------------------------
' SWITCH TO POWERPOINT TO PASTE AND FORMAT THE COPIED TEXT
'---------------------------------------------------------
'
ppApp.Activate
'
With ActivePresentation
'
Set oSlide = .Slides(1)
Set oShape = oSlide.Shapes("Rectangle 3")
'
' PASTE THE TEXT COPIED FROM WORD INTO THE SHAPE
oShape.TextFrame.TextRange.Text = ""
oShape.TextFrame.TextRange.Characters(1, 0).Select
SendKeys "^v", 1
ppParaCount = oShape.TextFrame.TextRange.Paragraphs.Count
'
'
' FIRST CLEAR THE PARAGRAPH FORMATTING FROM POWERPOINT
oShape.TextFrame.TextRange.Paragraphs(1, ppParaCount).ParagraphFormat.Bullet.Type = ppBulletNone
oShape.TextFrame.TextRange.Paragraphs(1, ppParaCount).IndentLevel = 0
'
' APPLY THE WORD PARAGRAPH FORMATTING INTO POWERPOINT
For p = 1 To ppParaCount
'
If (paraBullet(p) = 2 Or paraBullet(p) = 3) Then ' 2 for bullet and 3 for numbered bullet
oShape.TextFrame.TextRange.Paragraphs(p).ParagraphFormat.Bullet.Visible = msoTrue
oShape.TextFrame.TextRange.Paragraphs(p).IndentLevel = paraIndentLevel(p) + 1
End If
'
Next p
'
End With
'
' EXIT WORD
wdDoc.Close
wdApp.Quit
'
' CLEAR ARRAY
ReDim paraBullet(0)
ReDim paraIndentLevel(0)
'
' CLEAR OBJECTS
Set oShape = Nothing
Set oSlide = Nothing
Set oPara = Nothing
Set oRow = Nothing
Set oTable = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
'
MsgBox "process complete."
'
End Sub

‘————————————————–

Advertisements

December 29, 2010 - Posted by | PowerPoint, VBA, Word

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: