How can I change a portion of my object IDs to something else in MapViewer using Scripter?

This article contains a sample script for replacing substrings in the ID field of all selected objects in an open MapViewer plot.

To run this script:

  1. Copy the script below, or click here to download the BAS file: ID_Replace.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\MapViewer 8\Scripter.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Click Script | Run to run the script.


' ID_Replace.bas
' Copyright (C) Golden Software Inc. 1992-2015
' This script replaces substrings in the
' ID or attribute field of the selected objects.
' To run this script, first open your GSM file in MapViewer and select one or more objects.
' Then run the script in Scripter. Type in the old name and new name of the selected ID/Attribute.
' For all of the selections with that old name, the ID/Attribute will be renamed to the new name.
Sub Main
    Dim mvApp, lyr As Object

    ' Find MapViewer and create a pointer to the application object
    Set mvApp = GetObject(,"MapViewer.Application")

    ' Make sure that at least one object is selected
    Set lyr = mvApp.ActiveDocument.ActiveLayer
    If lyr.Selection.Count = 0 Then
      MsgBox "Replacing strings in IDs requires that" & vbCrLf & "one or more objects be selected first."
    End If

    ' Create and display the dialog to find out which
    ' IDs should be modified and what to change
    Dim ID_List(4) As String
    ID_List(0) = "PID"
    ID_List(1) = "SID"
    ID_List(2) = "ID3"
    ID_List(3) = "ID4"
    ID_List(4) = "Hyperlink"
    Begin Dialog UserDialog 270,119,"Replace substring in IDs" ' %GRID:10,7,1,1
        DropListBox 90,7,180,112,ID_List(),.ID1
        Text 30,42,60,14,"Old",.Text2,2
        OKButton 40,91,90,21
        CancelButton 150,91,90,21
        Text 0,7,80,14,"ID to modify",.Text1,1
        TextBox 10,56,110,21,.OldStr
        TextBox 150,56,110,21,.NewStr
        Text 180,42,50,14,"New",.Text3,2
    End Dialog
    Dim dlg As UserDialog
    dlg.ID1 = 0
    Dim sts As Integer

    sts = Dialog(dlg)

    ' The Cancel button returns 0.
    ' The OK button returns -1.
    If sts = 0 Then ' cancel button pressed
    End If

    ' For each selected object, replace the old string
    ' with the new and store it back.
    Dim i As Integer

    Select Case dlg.ID1
    Case 0 'PID
        For i = 1 To lyr.Selection.Count
            If Len(lyr.Selection.Item(i).PIDName) > 0 Then
                lyr.Selection.Item(i).PIDName = Replace(lyr.Selection.Item(i).PIDName,dlg.OldStr,dlg.NewStr)
            End If
    Case 1 'SID
        For i = 1 To lyr.Selection.Count
            If Len(lyr.Selection.Item(i).SIDName) > 0 Then
                lyr.Selection.Item(i).SIDName = Replace(lyr.Selection.Item(i).SIDName,dlg.OldStr,dlg.NewStr)
            End If
    Case 2 'ID3
        For i = 1 To lyr.Selection.Count
        	If Len(lyr.Selection.Item(i).Attrib1) > 0 Then
                lyr.Selection.Item(i).Attrib1 = Replace(lyr.Selection.Item(i).Attrib1,dlg.OldStr,dlg.NewStr)
            End If
        ' refresh the OM
    Case 3 'ID4
        For i = 1 To lyr.Selection.Count
            If Len(lyr.Selection.Item(i).Attrib2) > 0 Then
                lyr.Selection.Item(i).Attrib2 = Replace(lyr.Selection.Item(i).Attrib2,dlg.OldStr,dlg.NewStr)
            End If
        ' refresh the OM
    Case 4 'Hyperlink
        For i = 1 To lyr.Selection.Count
            If Len(lyr.Selection.Item(i).HyperlinkStr) > 0 Then
                lyr.Selection.Item(i).HyperlinkStr = Replace(lyr.Selection.Item(i).HyperlinkStr,dlg.OldStr,dlg.NewStr)
            End If
        ' refresh the OM
    End Select

End Sub


Updated November 2, 2016

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.