Hello,
I am using Word/PowerPoint 2010 VBA and have had a lot of difficultly. However I believe I narrowed the issue down to one single small issue.
I have code in Word that I am using as the "control" to clean up pictures that will place the result in both Word and PowerPoint. That code works perfectly with no issues. However, I also have UserForms that collect information from the user (just string/integer content) which I am attempting to place in both Word and PowerPoint. I have no issue with placing the collected information into Word exactly where I want it. I have attempted for several days now to place the user content into PowerPoint but cannot quite figure out how to do it.
In PowerPoint, I have named various slides using VBA and I have created/named tags. With those tags I am simply throwing in garbage text as a placeholder so that I can rename the text with the user content derived from the UserForms.
It seems to me that the root of the situation is that when opening PowerPoint through VBA, no slide is physically "activated" if that is the correct term. By that I mean that using anything along the lines of ActiveWindow.Selection.ShapeRange(1) produces a 'nothing' result.
I am able to activate PowerPoint, open a specific presentation (which is a .pptm just in case if that matters), set a specific slide, go to the slide and even select the title text frame. However no matter what I do it seems as though the actual slide never "activates" because when I attempt to reference a function it still produces "nothing" as a result.
Also I would like to note that I have no issues with running the text selection/replacement from PowerPoint VBA. The issue is that I need to run VBA from Word and place the same content from Word into various places in a presentation. I really think I am missing something subtle like a slide activation or maybe mis-referencing an object type?
Please let me know if you have any ideas.
Thanks!
Example of what I am using is shown below (assuming I have already set up a tag As 'tag1' and slide name as 'slide1' in referenced ppt):
Sub Word_to_PowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShp As PowerPoint.Shape
Dim pptName As String
pptName = "C:\Documents\aPresentation.pptm"
Dim pptTagName As String
pptTagName = "Tag1"
Dim pptSlideNum As String
pptSlideName = "Slide1"
' * Launch Powerpoint
On Error Resume Next
Set pptApp = GetObject(Class:="PowerPoint.Application")
Err.Clear
If pptApp Is Nothing Then
Set pptApp = CreateObject(Class:="PowerPoint.Application")
pptApp.Activate
Set pptPres = pptApp.Presentations.Open(pptName)
pptApp.Visible = True
End If
' * Reference slide
pptPres.Slides(pptSlideName).Select
' * Probably useless code, attempting to activate slide
Set pptSlide = pptPres.Slides(pptSlideName)
Windows(pptSlide).Activate
Windows(2).Application.ActiveWindow.Activate
' * Select TextFrame
pptPres.Slides(pptSlideName).Shapes.Title.TextFrame.TextRange.Select
Set pptShp = ShapeRef("ShapeName", pptTagName, ActiveWindow.Selection.ShapeRange(1))
' * Replace Text
If Not pptShp Is Nothing Then
With pptShp.TextFrame.TextRange
.Replace findwhat:="AAA", replacewhat:="BBB"
.Replace findwhat:="CCC", replacewhat:="DDD"
End With
End If
End Sub
Function ShapeRef(TagName As String, TagValue As String, newSlide As PowerPoint.Slide) As Shape
Dim pptShp As Shape
Dim newShp As Shape
Set newShp = Nothing
On Error GoTo ErrorHandler
'Examine All Slide in the Active PPT
For Each newSlide In ActivePresentation.Slides
For Each pptShp In newSlide.Shapes
' Find Shape qualifier
If pptShp.Tags(TagName) = TagValue Then
Set newShp = pptShp
Exit For
End If
Next
Next
Set ShapeRef = newShp
NormalExit:
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Number & " " & Err.Description
Resume NormalExit
End Function