• Passing an Array as an Arument (VBA/Word/2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Passing an Array as an Arument (VBA/Word/2000)

    Author
    Topic
    #424642

    Don,
    What is the code in CalledProcedure?

    Viewing 1 reply thread
    Author
    Replies
    • #975962

      Edited by author; Dim statement in Module 1 was misplaced.
      I am not certain whether I have a real problem or not. I have a set of procedures which contain the following code:

      In Module 1

      Public Sub Main()
      Dim myArray()
      ' Some code
          Call Module2.CalledProcedure(myArray())
      ' Some more code
          End
      End Sub
      

      In Module 2

      Public Sub CalledProcedure(subArray() As String)
      
      ' some code that redimensions subArray
      End Sub
      

      My apparent problem is that without the End statement in Module 1, each time the procedure is run the array becomes larger. That is: Run 1 causes the array to have 9 elements; Run 2: 18 elements; Run 3: 27 elements; etc.
      While the End statement cures the problem, I am not convinced that it is the most elegant solution. For instance; if for some reason the process is interrupted and terminated before reaching the End statement, will there be a problem the next time the procedure is run? Or is this phenomenon solely a characteristic of running in VBE?

      Any insight will be greatly appreciated.

      • #975972

        It may be because you have Dim’ed myArray() as a variable in the declarations area of your module rather than inside a particular procedure. Such “global” (I know this might be a loose use, hence the quotation marks) variables may retain value in an unpredictable manner.

        • #975977

          Oops! Sorry for wasting your time Jerfferson. I prepared the original posted code from memory, and of course; got it wrong. Please see the edited version and my response to Rory.

    • #975981

      Hi Rory
      Thanks for the speedy response.
      Please note the correction I have made to my original post.
      The two modules are enclosed in the attached .Zip file.

      Module 1 is GraphicInserter
      The entry point is GraphicInserter.InsertGraphicsByBatch
      The call statement of interest is Call SearchDirectory.InsertGraphics(strPath, filenames())

      To explain what may not be immediately obvious; SearchDirectory is a general purpose module I use to capture file and directory information. The InsertGraphics procedure has been inserted in this instance to make it unnecessary to pass and maintain unnecessary arrays.

      • #975983

        The problem is in the variable lFileCount. This is a module-level variable and it is not reset, so it increases each time the code is run. To remedy this, insert a line

        lFileCount = 0

        at the beginning of the Search procedure.

        Note: you can release the memory used by dynamic arrays when they are no longer needed as follows:

        Erase filenames

    Viewing 1 reply thread
    Reply To: Reply #975972 in Passing an Array as an Arument (VBA/Word/2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel