Assign text to slides

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulwagh
    New Member
    • Feb 2013
    • 29

    Assign text to slides

    Hi,
    I am writing excel VBA program to make a popwerpoint presentation and assign predefined text to each slide depending on number. I am using if condition to check the image number and assigning text to slide.
    Below is the code I have written -
    Code:
    Private Const imgSlide1 = "Example1"
    Private Const imgSlide2 = "Example2"
    Private Const imgSlide3 = "Example3"
    Private Const imgSlide4 = "Example4"
    imgCnt=1
    picCnt=9
    While imgCnt <= picCnt
        
        
        ppApp.ActiveWindow.View.GotoSlide (slideNo)
        
        Set ppSlide = ppApp.ActivePresentation.Slides(slideNo)
        
        If imgCnt = 1 Then
            ppSlide.Shapes(2).TextFrame.TextRange.Text = imgSlide1
        ElseIf imgCnt = 2 Then
            ppSlide.Shapes(2).TextFrame.TextRange.Text = imgSlide2
        ElseIf imgCnt = 3 Then
            ppSlide.Shapes(2).TextFrame.TextRange.Text = imgSlide3
        ElseIf imgCnt = 4 Then
            ppSlide.Shapes(2).TextFrame.TextRange.Text = imgSlide4
        End If
           
        imgCnt = imgCnt + 1
    Here instead of IF...ELSE conditions I want to use like
    Code:
    imgCnt=1
    picCnt=9
    While imgCnt <= picCnt
        
        
        ppApp.ActiveWindow.View.GotoSlide (slideNo)
        
        Set ppSlide = ppApp.ActivePresentation.Slides(slideNo)
        
        'Here for const 1,2,3,4 I want to user imgCnt variable value and will concate it to imSlide as shown below
        'but it is not working 
        ppSlide.Shapes(2).TextFrame.TextRange.Text = "imgSlide" & imgCnt
            
        imgCnt = imgCnt + 1
    But instead of imgSlide1 value it is taking "imgSlide1" as a value

    Could you please let me know how to fix it.
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    Exactly. VB get to you what you have asking :)
    So, you concatenate 2 strings. Logically, you obtain a new STRING, not the variable name. But, about this subject must talk a guy with a much better English than me.

    If you wish to obtain Examle1, Example2, .... Example9 you must concatenate the string "Example" with imgCnt :
    Code:
     ppSlide.Shapes(2).TextFrame.TextRange.Text = "Example" & imgCnt
    But (I think) that is not the goal of your life to put an incremental number after the word "Example" as explanation for your pictures.
    A better approach is to use a SELECT CASE statment:

    Code:
    Dim Txt As String
    
        ppApp.ActiveWindow.View.GotoSlide (slideNo)
        Set ppSlide = ppApp.ActivePresentation.Slides(slideNo)
    
        While imgCnt <= picCnt
            Select Case imgCnt
                Case 1
                    Txt = imgSlide1
                Case 2
                    Txt = imgSlide2
                .......................
                Case Else
                    'Maybe code for trapping errors
            End Select
    
            ppSlide.Shapes(2).TextFrame.TextRange.Text = Txt
    
            imgCnt = imgCnt + 1
        End While ' Not sure about the syntax.
    But why to type all that Private Const bla bla statements ?
    So, a better approache:
    Code:
    'No more need to declare constants
    Dim Txt As String
    
        ppApp.ActiveWindow.View.GotoSlide (slideNo)
        Set ppSlide = ppApp.ActivePresentation.Slides(slideNo)
    
        While imgCnt <= picCnt
            Select Case imgCnt
                Case 1
                    Txt = "Example1"
                Case 2
                    Txt = "Example2"
                .......................
                Case Else
                    'Maybe code for trapping errors
            End Select
    
            ppSlide.Shapes(2).TextFrame.TextRange.Text = Txt
    
            imgCnt = imgCnt + 1
        End While ' Not sure about the syntax.
    There are also other approaches (better) but I think that is enough for a very beginner.

    That is about using VBA.
    But I have a question:
    Any approach require to manually type the explanation for every picture.
    So... where is the benefit to use VBA ? You can type the explanation directly in the slide, isn't it ?

    Cheers !

    Comment

    • rahulwagh
      New Member
      • Feb 2013
      • 29

      #3
      Mihali thanks for answering the question.
      Regarding your question - I have written a macro this will take a screen shot of graphs from the webpage and it will create a powerpoint presentation.
      There are total 37 graphs but we don't know how many screen shots we are going to take(it can be 10,12,15 etc)
      We know the name of the graphs hence I am planning to declare 37 constant and will ask user to open the graph in sequence as their names are declared. Except opening the graphs on webpage rest of the process is automated hence we can not type directly on slide.
      I don't want 37 cases or 37 else if conditions hence I post this question to find out any alternate way.

      Hope this clear your doubt.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Use an array.

        Comment

        • rahulwagh
          New Member
          • Feb 2013
          • 29

          #5
          Rabbit, could you please tell how to use it.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Your thread has been moved to the Excel forum.

            Arrays are a basic and broad subject. If you do not know what an array is, you may want to invest some time in a tutorial. you can find Microsoft's documentation on arrays here: http://msdn.microsoft.com/en-us/libr...ffice.10).aspx

            Comment

            Working...