hexagon logo

VB code help

Hi Hi
Just dabbling with some VB and being a complete novice I need some helpSmiley

I tried the following code to insert three standard comments we use into programs, saves typing the same thing over and over again but it will only insert the comments at the end of the program and I need it to insert at the current cursor position.

What have I missed out?

Sub Main

Dim DmisApp As Object
Dim DmisPart As Object
Dim DmisCommands As Object
Dim DmisCommand As Object

  Set DmisApp = CreateObject("PCDLRN.Application")
  Set DmisPart = DmisApp.ActivePartProgram
  Set DmisCommands = DmisPart.Commands
  CommandCount = DmisCommands.Count
  Set DmisCommand = DmisCommands.Item(CommandCount)
  DmisCommands.InsertionPointAfter DmisCommand
  
  Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Id  = C1
    retval = DmisCommand.PutText ("C1", ID, 0)
  ' Set Comment Type  = INPUT
    retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
  ' Set Comment Item 1 = 'WORKS ORDER NO'
    retval = DmisCommand.PutText ("WORKS ORDER NO", COMMENT_FIELD, 1)
  ' Set Report  = YES
    retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
  
  Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Id  = C2
    retval = DmisCommand.PutText ("C2", ID, 0)
  ' Set Comment Type  = INPUT
    retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
  ' Set Comment Item 1 = 'CUSTOMERS ORDER NO'
    retval = DmisCommand.PutText ("CUSTOMERS ORDER NO", COMMENT_FIELD, 1)
  ' Set Report  = YES
    retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
  
  Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Id  = C3
    retval = DmisCommand.PutText ("C3", ID, 0)
  ' Set Comment Type  = INPUT
    retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
  ' Set Comment Item 1 = 'SERIAL NO & DATE'
    retval = DmisCommand.PutText ("SERIAL NO & DATE", COMMENT_FIELD, 1)
  ' Set Report  = YES
    retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
  
End Sub


Any help appreciated and be gentle as i'm a bit fuzzy after last nights beer Nauseated face

Cheers
  • Look at you "InsertionPointAfter" second block...last line

  • Sub Main
    
    Dim DmisApp As Object
    Dim DmisPart As Object
    Dim DmisCommands As Object
    Dim DmisCommand As Object
    
      Set DmisApp = CreateObject("PCDLRN.Application")
      Set DmisPart = DmisApp.ActivePartProgram
      Set DmisCommands = DmisPart.Commands
      CommandCount = DmisCommands.Count
      Set DmisCommand = DmisCommands.Item(CommandCount)
      [COLOR="Red"]DmisCommands.InsertionPointAfter DmisCommand[/COLOR]
      
      Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
        DmisCommand.Marked = TRUE
      ' Set Id  = C1
        retval = DmisCommand.PutText ("C1", ID, 0)
      ' Set Comment Type  = INPUT
        retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
      ' Set Comment Item 1 = 'WORKS ORDER NO'
        retval = DmisCommand.PutText ("WORKS ORDER NO", COMMENT_FIELD, 1)
      ' Set Report  = YES
        retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
        DmisCommand.Marked = TRUE
      ' Set Id  = C2
        retval = DmisCommand.PutText ("C2", ID, 0)
      ' Set Comment Type  = INPUT
        retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
      ' Set Comment Item 1 = 'CUSTOMERS ORDER NO'
        retval = DmisCommand.PutText ("CUSTOMERS ORDER NO", COMMENT_FIELD, 1)
      ' Set Report  = YES
        retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
        DmisCommand.Marked = TRUE
      ' Set Id  = C3
        retval = DmisCommand.PutText ("C3", ID, 0)
      ' Set Comment Type  = INPUT
        retval = DmisCommand.SetToggleString (3, COMMENT_TYPE, 0)
      ' Set Comment Item 1 = 'SERIAL NO & DATE'
        retval = DmisCommand.PutText ("SERIAL NO & DATE", COMMENT_FIELD, 1)
      ' Set Report  = YES
        retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0)
      
    End Sub
    


    This is (probably) messing it up for you:

    CommandCount = DmisCommands.Count  [COLOR="Blue"]// This is the total amount of commands in the active partprogram[/COLOR]
    Set DmisCommand = DmisCommands.Item(CommandCount) [COLOR="Blue"]// Set the DMISCommand to be at the last command (see the line above)[/COLOR]
    DmisCommands.InsertionPointAfter DmisCommand [COLOR="Blue"]// Set the insertionpoint after DMISCommand = at the end of the program[/COLOR]


    Try exchanging the red-marked line in your current code above to this:

    DmisCommands.InsertionPointAfter DmisCommands.CurrentCommand


    ...and see if that helps.
  • Thanks
    changing the red highlighted line to:
    DmisCommands.InsertionPointAfter DmisCommands.CurrentCommand
    As suggested by vpt.se did the trick.
  • Not forgetting JM222 lol

    Big thanks to you both SmileySmiley