Visual Basic Application (VBA) to Python?

justearth used Ask the Experts™
Can this visual basic code be converted to python? If so,  how?

On Error GoTo ErrorHandler
   Dim pmxdoc As IMxDocument
    Set pmxdoc = thisdocument
    Dim pmap As IMap
    Set pmap = pmxdoc.FocusMap
Dim path As String
Dim yesno As String
yesno = MsgBox("Is your grid located at the bottom of the TOC", vbYesNo)
If yesno = 7 Then
    Exit Sub
    End If
path = Module1.GetFolderName("Path to save new grid files")
'path = InputBox("Path for files")
Dim i As Integer
    ' Get the input raster to be clipped
    Dim player As ILayer
    Set player = pmap.Layer(pmap.LayerCount - 1)
    Dim pRLayer As IRasterLayer
    Set pRLayer = player
    Dim pInRaster As IRaster
    Set pInRaster = pRLayer.Raster
    ' Get the input for the analysis mask
    Dim pGeoDs As IGeoDataset
    Dim pLy As ILayer
    Dim pAllLayers As IEnumLayer
Set pAllLayers = pmap.Layers
Do While Not player Is Nothing
Set pLy = pAllLayers.Next
Dim rastername As String
If TypeOf pLy Is IRasterLayer Then GoTo doodie
    Dim pFeatureLy As IFeatureLayer
    Set pFeatureLy = pLy
    Set pGeoDs = pFeatureLy.FeatureClass
    ' Create a Spatial operator
    Dim pAlgbOp As IMapAlgebraOp
    Set pAlgbOp = New RasterMapAlgebraOp
    ' Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pAlgbOp
    Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile(path, 0)
    Set pEnv.OutWorkspace = pWS
    Set pEnv.Mask = pGeoDs
'set the extent here
    pEnv.SetExtent esriRasterEnvValue, pGeoDs.Extent
    ' Bind a raster
    Call pAlgbOp.BindRaster(pInRaster, "R1")
    ' Perform re-evaluation of input raster
    Dim pOutRaster As IRaster
    Dim str As String
    str = "gr_" & pLy.Name & " = [R1]"
    Set pOutRaster = pAlgbOp.Execute(str)
    'Set pOutRaster = pAlgbOp.Execute ("fd = [R1]")
    ' Add it into ArcMap
    Set pRLayer = New RasterLayer
    pRLayer.CreateFromRaster pOutRaster
        pRLayer.Name = pLy.Name
    pmap.AddLayer pRLayer
MsgBox "Done."
      Dim pactive As IActiveView
      Set pactive = pmxdoc.ActiveView
  Exit Sub
  HandleError True, "ICommand_OnClick " & c_sModuleFileName & " " & GetErrorLineNumberString(Erl), Err.Number, Err.Source, Err.Description, 3

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It depends on whether you have Python implementation of the components/classes that you use in VB (like IMxDocument, IMap, RasterMapAlgebraOp, ...). Better to say, the constructs used in your VB program are easy to convert. However, some things are not bound to the language (VB) -- they implement some functionality that is used through an interface.

If it is ESRI ArcGIS, then it is possible -- see You can dowload from there that contains some demos and PowerPoint presentation on how to do things from Python.


I use python and you, pepr, have helped me quite a bit in the past with python and ESRI Arc Map. I know that a lot of the functionality is 'called' differently in the VB vs python scripts. But I thought if the VB code was changed to python, then I could try to figure out switching the esri specific stuff.

Thanks again,
Well, I have no experience with ESRI Arc Map. I only know it exists. I can only help you to explain something from Python/other point of view and why things are done in Python this way and in COM that way. Not knowing ArcMap, I can barely rewrite the script. But I can help YOU to do that. You already know what are the ArcMap objects about.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


I will get back to this real soon. I will research what the difference in the commands are between VB and Python.



I may need to close /accept your answer for this question for now, as I have not been able to advance upon it yet and it may take considerable time for me to do so.



Thanks, perhaps more to come in the future.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial