I'm having the weirdest experience trying to make excel fill in some fields in a template form and then copy/paste it on another WorkSheet. Then it must repeat the process for a limited number of times, always pasting the forms beneath the previous ones. When the page is full, it must copy the header of page #1 and paste it at the top of the next page, then proceed the form copy/paste process.
This routine is part of my VB6 little calculation software I'm currently (trying to)develop.
The thing is: excel fails to copy the data I want and paste it in the correct places in the 2nd worksheet. Sometimes it doesn't copy the data at all, and I get a blank sheet when I export it as pdf; sometimes it does copy everything, but paste the filled in template forms in the wrong cell.
The weird behavior: that only happens when Excel workbook is set to not visible. If I pause the execution and set it to visible, the routine runs just fine and I get a perfect pdf report at the end.
Any ideas of the reasons of this being happening and/or solutions to fix it?
Thanks in advance.
I thought it could be due to the VB6 exe doing things too fast for excel to process, so I added a couple of "Sleep 1000" in between lines of code hoping excel to be able to catch up. Didn't work.
    With xlsm.Worksheets("Template Cabos")
        .Activate
        Sleep 1000
        ActiveSheet.Range(Cells(1, 1), Cells(5, 36 + ONS)).Copy 'Copies the filled in form
        Sleep 1000
    End With
    With xlsm.Worksheets("Mem. Cabos")
        .Activate
        Cells(lin, 1).Select
        .Paste                         'Pastes it in the report workbook
        lin = lin + 6
        If (lin + 6) > (pag * 33) Then          'Checks if the next form would fit into the page    Checa se cabe outra ficha na página
            lin = pag * 33 + 1                  'If not, the marker "lin" goes to the top of page 2   Se não couber, o marcador "lin" vai pro topo da pág. 2
            .Range("A1:AJ5").Copy               'Copies the header of page 1                        Copia o cabeçalho da pág. 1
            Cells(lin, 1).Select
            .Paste                              'Pastes on the top of page 2                        Cola no topo da pág. 2
            pag = pag + 1                       'Acknowledges that now the report has one more page       Sinaliza que agora o relatório tem 2 páginas
            Cells(lin + 4, 35).Value = pag      'Types "2" in the field "page" of last page's header   Escreve "2" no campo "Pág." do cabeçalho da pág. 2
            lin = lin + 7                       'Moves the marker 7 line down to receive the next form    Move o marcador 7 linhas abaixo para receber a próx. ficha
        End If
    End With
 
     
    