Randomizing Import

Hello, I'm trying to create a macro that imports a random file within a specific folder. Below is the code I'm using for the import itself.

Dim impopt As StructImportOptions
Set impopt = CreateStructImportOptions
With impopt
.Mode = cdrImportFull
.MaintainLayers = True
With .ColorConversionOptions
.SourceColorProfileList = "sRGB IEC61966-2.1,U.S. Web Coated (SWOP) v2,Dot Gain 20%"
.TargetColorProfileList = "sRGB IEC61966-2.1,U.S. Web Coated (SWOP) v2,Dot Gain 20%"
End With
End With
Dim impflt As ImportFilter
Set impflt = ActiveLayer.ImportEx("C:\Users\jaspo\Desktop\PROJECTS\Face generator\Jpgs\Eyes\1.png", cdrPNG, impopt)
impflt.Finish
Dim s1 As Shape
Set s1 = ActiveShape
ActivePage.Shapes.All.CreateSelection
ActiveSelection.Move -0.902173, 0.82013
ActivePage.Shapes.All.CreateSelection
ActiveSelection.AlignAndDistribute cdrAlignDistributeHAlignCenter, cdrAlignDistributeVAlignCenter, cdrAlignShapesToCenterOfPage, cdrDistributeToSelection, False, cdrTextAlignBoundingBox

The one highlighted number is the name of my file. all of the files in the folder are named as numbers 1-14.

is it possible to use the randomize function with this to change the name of the file that it imports?

the randomize function code I'm using is:

Private Function getRandomNumber(nLow As Long, nHigh&) As Long
Dim n1&
Randomize
n1 = (nHigh - nLow + 1) * VBA.Rnd + nLow
getRandomNumber = n1


End Function

End Sub

another option I thought of was to make a separate module for each file. Then assign a button to randomly activate one of the 14 modules.

But I'm not sure how to do that.

I have also made a userform that has a button that imports the files I want when clicked. I just don't know of a way to make the random button work.

I want it to randomly pick one button from each category.

Hopefully I'm making sense.

I don't really know how to write code, I got most of this through tutorials and recording.

If anyone knows of a way that this could work, I'd really appreciate it.

Thanks!

Parents
  • I am assuming here that the code you are using for importing is working, and that the specific question is how you can randomize the file name (so that it can be "1.png", "2.png", "3.png", etc. instead of the "1.png" that is in there now.

    If that's the case, then sure, you could do that.

    First, I think that the code for getting random integers is not quite right. I've changed it a little bit here:

    Private Function getRandomNumber(nLow As Long, nHigh&) As Long
    Dim n1&
    
        Randomize
        n1 = Int((nHigh - nLow + 1) * Rnd + nLow)
        getRandomNumber = n1
    End Function
    

    That function returns a Long. You can use that returned value to help create the String that you are using to specify the filename.

    If you just stuck it right into the line of code you already had, without declaring a new string variable, it might look something like this (for a random integer between 1 and 14, inclusive):

    Set impflt = ActiveLayer.ImportEx("C:\Users\jaspo\Desktop\PROJECTS\Face generator\Jpgs\Eyes\" & CStr(getRandomNumber(1, 14)) & ".png", cdrPNG, impopt)
    
Reply Children