Forum Discussion

ranjank3010's avatar
ranjank3010
Copper Contributor
Mar 13, 2022

Insert a text

In a word document I need to tag “AAA” at the beginning of the table (Above to the Table) and “BBB” at the end of the table (Next to the end of a table).

 

After running a macro at the beginning of the table it should get tagged as “AAA” then it should be Ask for confirmation at the end of the table as “wish you want to continue the table “Yes” or “No”. if we select yes then it should be move to the next table and again it should be asked for confirmation Yes or No until we select No. if we select No then it should be tagged as “BBB” at the ending of the table.

Although I'm not quite sure if we could automate this work or not, if it doesn't seem realistic, please excuse me…

I really appreciate your help and my sincere gratitude goes out to you for your support.

 

Doc Type: ".docx" , ".rtf"

This is actual requirement.

Tagged AAA at the beginning of the active table, then if we select "Yes" It should moved to next table, again if we select "Yes" it should moved to next table, if we select "No" then it should stop moving to next table and tagged BBB at the end of that table.

 

I have posted this in the below forum and would like to modify the code to suit my needs but dont seem to be getting any responses.
This could be because of the previous post being unclear.
Please dont considered as "Cross Posting"

https://www.msofficeforums.com/word-tables/48540-table-tagging.html#post166102

 

Please modify the below code.


Sub TagTables()
Dim aTbl As Table, bSkip As Boolean, iCount As Integer, i As Integer
iCount = ActiveDocument.Tables.Count
For i = 1 To iCount
Set aTbl = ActiveDocument.Tables(i)
aTbl.Select
If Not bSkip Then
aTbl.Range.Previous(Unit:=wdCharacter, Count:=1).InsertBefore vbCr & "AAA"
bSkip = MsgBox("Yes to Extend tag, No to close tag", vbYesNo) = vbYes
If Not bSkip Then aTbl.Range.Next(Unit:=wdCharacter, Count:=1).InsertBefore "BBB" & vbCr
Else
bSkip = MsgBox("Yes to Extend tag, No to close tag", vbYesNo) = vbYes
If bSkip = False Then
aTbl.Range.Next(Unit:=wdCharacter, Count:=1).InsertBefore "BBB" & vbCr
End If
If i = iCount - 1 Then bSkip = False
End If
Next i
End Sub


I would appreciate if you could review the code once based on the above requirements.
Despite my best efforts, I could not modify the code.
I greatly appreciate you helping me and I thank you very much...

PFA.

 

4 Replies

  • ranjank3010 Use

     

    Dim i As Long, j As Long
    Dim rng As Range
    If Selection.Information(wdWithInTable) = False Then
        MsgBox "The selection is not inside a table."
        Exit Sub
    End If
    With ActiveDocument
        For i = 1 To .Tables.Count
            If Selection.Range.Start > .Tables(i).Range.Start And Selection.Range.End < .Tables(i).Range.End Then
                Exit For
           End If
           Next i
           Set rng = .Tables(i).Range
           rng.Collapse wdCollapseStart
           rng.MoveStart wdCharacter, -1
           rng.InsertBefore vbCr & "AAA"
           For j = i + 1 To .Tables.Count
              If MsgBox("Do you wish to insert the End tag after this table", vbQuestion + vbYesNo) = vbYes Then
                  Set rng = .Tables(j).Range
                  rng.Collapse wdCollapseEnd
                  rng.InsertAfter "BBB" & vbCr
                 Exit For
             End If
        Next j
    End With

    • ranjank3010's avatar
      ranjank3010
      Copper Contributor
      Thanks for your prompt response, but while running am getting a " run time error 5941:", (The requested member of the collection does not exists.)
      Could you plese review the code once and do the needful.

      Thanks a lot....