none
I think I found bug on word vba. RRS feed

  • Question

  • Hello Everyone,

    I think I found bug on word vba.

    please take a look at below codes.

    Sub ExperimentForBug()
        Dim MyTable As Table
        Set MyTable = ThisDocument.Tables(6)
        Dim DocumentObject As Document
        Set DocumentObject = Documents.Open(ThisDocument.Path & "\Target.doc")
        Dim OriginalTable As Table
        Set OriginalTable = DocumentObject.Tables(1)
        Dim CounterForRepetitiveStatement As Integer
        For CounterForRepetitiveStatement = 2 To MyTable.Range.Rows.Count
            MyTable.Cell(CounterForRepetitiveStatement, 1).Range.Text = ""
        Next
        OriginalTable.Cell(6, 2).Range.Copy
        MyTable.Cell(2, 1).Range.Paste
        OriginalTable.Cell(9, 2).Range.Copy
        MyTable.Cell(3, 2).Range.Paste
        Dim MissRow As Long
        Dim RecurrenceRow As Long
        For CounterForRepetitiveStatement = 2 To MyTable.Rows.Count
            If InStr(1, MyTable.Cell(CounterForRepetitiveStatement, 2).Range.Text, "Miss", vbTextCompare) <> 0 Then
                MissRow = CounterForRepetitiveStatement
            ElseIf InStr(1, MyTable.Cell(CounterForRepetitiveStatement, 2).Range.Text, "Recurrence", vbTextCompare) <> 0 Then
                RecurrenceRow = CounterForRepetitiveStatement
            End If
        Next
        OriginalTable.Cell(10, 2).Range.Copy
        MyTable.Cell(MissRow + 1, 2).Range.Paste
        OriginalTable.Cell(11, 2).Range.Copy
        MyTable.Cell(RecurrenceRow, 2).Range.Paste
        For CounterForRepetitiveStatement = 3 To MyTable.Rows.Count
            If CounterForRepetitiveStatement <> MissRow And CounterForRepetitiveStatement <> RecurrenceRow Then
                MyTable.Cell(CounterForRepetitiveStatement, 2).Range.Text = ""
            End If
        Next
        DocumentObject.Close
        Set DocumentObject = Nothing
    End Sub

    this code copies data from original document's original tables

    and pastes to documents that running vba codes.

    and I didn't put any code to make changes to original tables and documents, 

    but original table's contents is changed after vba code runs. why this happens?

    thank you for your reply in advance

    and sorry for my bad english...

    Sunday, June 10, 2018 11:02 AM

All replies

  • Please be more specific about what the problem is.

    Also provide the minimum code necessary (and relevant data if necessary) to recreate the problem. You will need to do that to report the problem to Microsoft but the most likely result of reducing the code to a minimum is that you will find something that is not working the way you think it is. In other words, you are likely to be able to solve the problem yourself if you reduce the code to the minimum necessary. If you do not solve the problem yourself then posting the minimum code will make it easier for others to help you. 



    Sam Hobbs
    SimpleSamples.Info

    Sunday, June 10, 2018 5:29 PM
  • Hello Truth-,

    I could reproduce your issue once the value of RecureenceRow is 0 and then execute the code.

    MyTable.Cell(RecurrenceRow, 2).Range.Paste

    To be honestly, I failed to explain why it will do change to the original document.

    However, it is obviously that Table.Rows(0,2) does not exist. There is no row 0 of a table. I would suggest you do some operation to avoid this unexpected situation.

    For instance.

    OriginalTable.Cell(11, 2).Range.Copy
        If RecurrenceRow = 0 Then RecurrenceRow = 1
        MyTable.Cell(RecurrenceRow, 2).Range.Paste

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 11, 2018 5:41 AM