none
Power Point Variant, Arrays, And Files RRS feed

  • Question

  • Hi,

    I am having some issues assigning values from a csv file to an array. There is probably something very obvious that I am missing, and I hope some one on the forum can help me out.

    I used this code, in a previous script and it worked fine:

        Dim Input_file As Variant
        Dim FileNum As Integer
        Dim jobData() As String
        FileNum = FreeFile()
        Open Input_file For Input As FreeFile
        While Not EOF(FileNum)
            Line Input #FileNum, Buffer
            jobData() = Split(Buffer, ",") 'split text at ","

    I could access the values from the .csv as array elements

    However, I created a new presentation and scripted it similarly. In this case I needed to access to .csv files. So, I created this:

        Dim Input_file As Variant
        Dim PedUsage_file As Variant
        Dim FileNum As Integer
        Dim PedFileNum As Integer
        Dim pedInfo(4) As Integer
        Dim logData(20) As String
        FileNum = FreeFile()
        PedFileNum = FreeFile()
        Open Input_file For Input As FreeFile
        Open PedUsage_file For Input As FreeFile
        While Not EOF(PedFileNum)
            Line Input #PedFileNum, Buffer
            pedInfo() = Split(Buffer, ",")
                        For i = 0 To MaxImage
                            Line Input #FileNum, Buffer
                            logData() = Split(Buffer, ",")
    When I run the macro in Power Point, I get a compile error: can't assign to array @ the line "pedInfo() = Split(Buffer, ",")"

    When I comment the code out like this:

        Dim Input_file As Variant
    '    Dim PedUsage_file As Variant
        Dim FileNum As Integer
    '    Dim PedFileNum As Integer
    '    Dim pedInfo(4) As Integer
        Dim logData(20) As String
        FileNum = FreeFile()
    '    PedFileNum = FreeFile()
        Open Input_file For Input As FreeFile
    '    Open PedUsage_file For Input As FreeFile
        While Not EOF(PedFileNum)
            Line Input #PedFileNum, Buffer
    '        pedInfo() = Split(Buffer, ",")
                        For i = 0 To MaxImage
                            Line Input #FileNum, Buffer
                            logData() = Split(Buffer, ",")

    I get a compile error: can't assign to array @ "logData() = Split(Buffer, ",")"

    I would think after commenting out the code it would be the same as the initial code (at least in intent). Yet, I get the error.

    Wednesday, May 23, 2012 6:17 PM

Answers

  • With

        Dim pedInfo As Variant
        pedInfo = Split(Buffer, ",")

    you should get neither a compile error nor a runtime error. There must be something else...

    Regards, Hans Vogelaar

    • Marked as answer by Kip Guerrero Wednesday, May 23, 2012 10:46 PM
    Wednesday, May 23, 2012 9:22 PM

All replies

  • You'll have to use

        Dim pedInfo As Variant
        Dim logData As Variant


    Regards, Hans Vogelaar

    Wednesday, May 23, 2012 6:53 PM
  • Thanks Hans,

    That does eliminate the "compile error: can't assign to array" however, I now get a "subscript out of range error"

    I am confused since in the first example code I showed - I did not have to declare the array as a variant. I declared the array as a string, and this worked fine.

    Thanks,

    Kip

    Wednesday, May 23, 2012 8:12 PM
  • On which line do you get the error now?

    Regards, Hans Vogelaar

    Wednesday, May 23, 2012 8:26 PM
  • Hans,

    It is on the same line of code as the compile error: "pedInfo() = Split(Buffer, ",")"

    I attempted it with the "()" and without.

    The only difference is that the code does compile and the subscript out of range error occurs during execution.

    Thank you very much for your help.

    Wednesday, May 23, 2012 8:51 PM
  • With

        Dim pedInfo As Variant
        pedInfo = Split(Buffer, ",")

    you should get neither a compile error nor a runtime error. There must be something else...

    Regards, Hans Vogelaar

    • Marked as answer by Kip Guerrero Wednesday, May 23, 2012 10:46 PM
    Wednesday, May 23, 2012 9:22 PM
  • Hans,

    We're closer. After reviewing my code I never declared "Buffer" a Variant. I also happened to use it twice, so I renamed the Buffer to PedBuffer and declared it a Variant.

    I also did not assign freefile elements - this too got everything working correctly "kind of" - it errors out later on the code which I need to trouble shoot - but it is working now.

    THANKS!

    Kip

    Wednesday, May 23, 2012 10:08 PM