none
Adding and Editing a row in word with a macro RRS feed

  • Question

  • I am new to macros, but I have created a macro that adds and deletes rows in a protected document in word.  It keeps the form formatting in the cells that I need it to, but I need to be able to edit the cells that I just added with new text and keep the form formatting.  Is this possible?

    Thanks

    L

    • Moved by Max Meng Thursday, March 15, 2012 3:08 AM moving to a more appropriate forum (From:Office IT Pro General Discussions)
    Wednesday, March 14, 2012 8:15 PM

Answers

  • The simplest way is use Ctrl+H and paste replace text. Problem only with new line.

    You can use this simple code - try in empty document.

    Sub Add_text()
    ActiveDocument.Select
    Selection.Text = "Ala ma kota" & vbNewLine & "a kot ma Ale" & vbNewLine & "itd"

    Call dodaj_tresc("ma kota", "dupa ", True, False)
    End Sub

    Sub dodaj_tresc(co_szukamy$, co_wsadzamy$, _
        Optional z_tylu As Boolean, Optional z_przodu As Boolean)
    Dim doc As Document, gdzie&
    Set doc = ActiveDocument
    gdzie = InStr(1, doc.Range.Text, co_szukamy) + Len(co_szukamy)
     If gdzie > 0 Then
        doc.Range(gdzie, gdzie).Select
        If z_tylu = True Then
            If z_przodu = True Then
                Selection.TypeText Text:=vbNewLine & co_wsadzamy & vbNewLine
            Else
                Selection.TypeText Text:=co_wsadzamy & vbNewLine
            End If
        Else
            If z_przodu = True Then
                Selection.TypeText Text:=vbNewLine & co_wsadzamy
            Else
                Selection.TypeText Text:=co_wsadzamy
            End If
        End If
        Exit Sub
     End If
    End Sub

    If you want to add new row in table cells tat try this method:
    Private Sub Wstawienie_wierszy_do_zaznaczonej_tabeli()
    'MVP OShon from VBATools.pl
    Dim Count%, InsertRows%, tbl As Word.Table
    If Selection.Information(wdWithInTable) = False Then _
    MsgBox "You do not select table", vbExclamation, "VBATools.pl": Exit Sub
    InsertRows = InputBox("How meny rows you want to add in tabe", _
    "Add new rows", 1)
    If IsNumeric(InsertRows) = False Then Exit Sub
    
    On Error GoTo blad
    Set tbl = Selection.Tables(1)
    With ActiveDocument.Tables(GetTableIndex(tbl))
     .Rows(.Rows.Count).Select
     Selection.Copy
    End With
    While Count < InsertRows
     Selection.Paste
     Count = Count + 1
    Wend
    Set tbl = Nothing
    Exit Sub
    blad:
    MsgBox Err.Number & " " & Err.Description
    End Sub
    
    Function GetTableIndex(tbl As Word.Table) As Long
    'Cindy Meister MVP
     Dim rng As Word.Range
     Set rng = tbl.Range
     rng.Start = tbl.Parent.Content.Start
     GetTableIndex = CLng(rng.Tables.Count)
    End Function 
    Regards

    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved


    Friday, March 16, 2012 8:30 AM
    Answerer
  • Presumably you are talking about a row of a table in a protected form? In that case you will find two examples of how to add rows to such tables at http://www.gmayor.com/word_vba_examples.htm

    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, March 17, 2012 9:28 AM

All replies

  • You mentioned cells and form formatting.

    Are you doing this in a UserForm? What do you exactly mean with cells, text fields?

    Please give is some more information on what exactly you are trying to accomplish, an example will be helpful.

    Thanks! :)


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer as an answer when your question is being answered.

    Thursday, March 15, 2012 4:14 PM
    Moderator
  • In addition, is this a table? Easiest way to maintain consistent formatting is to use Styles. Create a style for teh format you want then record a macro of you selecting relevant text and applying the style.

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management

    Thursday, March 15, 2012 9:34 PM
  • Thanks for responding.  I have a two page word document with nine tables in it for various packages and their cost.  In each table some of the columns are formatted with legacy forms for currency (the cost columns).  Those columns calculate totals.  I had the document protected so that the only information that could be changed was the currency columns based on location (some locations have different cost).  Now we need to be able to add and delete rows in the document and add text to the new rows added.  I have a macro that unprotects the document allows the user to delete or add a row and re-protects the document, but it is only copying the row from above, so it is copying text from above as well.  I need it to copy the formatting the legacy forms formatting for the currency cells, but not for the other cells.  Does this make sense?  It this something that can be done or is this beyond the scope of a macro.  I am new to macros, so I am not sure what there limitations are.

    Thanks

    L

    Friday, March 16, 2012 1:52 AM
  • The simplest way is use Ctrl+H and paste replace text. Problem only with new line.

    You can use this simple code - try in empty document.

    Sub Add_text()
    ActiveDocument.Select
    Selection.Text = "Ala ma kota" & vbNewLine & "a kot ma Ale" & vbNewLine & "itd"

    Call dodaj_tresc("ma kota", "dupa ", True, False)
    End Sub

    Sub dodaj_tresc(co_szukamy$, co_wsadzamy$, _
        Optional z_tylu As Boolean, Optional z_przodu As Boolean)
    Dim doc As Document, gdzie&
    Set doc = ActiveDocument
    gdzie = InStr(1, doc.Range.Text, co_szukamy) + Len(co_szukamy)
     If gdzie > 0 Then
        doc.Range(gdzie, gdzie).Select
        If z_tylu = True Then
            If z_przodu = True Then
                Selection.TypeText Text:=vbNewLine & co_wsadzamy & vbNewLine
            Else
                Selection.TypeText Text:=co_wsadzamy & vbNewLine
            End If
        Else
            If z_przodu = True Then
                Selection.TypeText Text:=vbNewLine & co_wsadzamy
            Else
                Selection.TypeText Text:=co_wsadzamy
            End If
        End If
        Exit Sub
     End If
    End Sub

    If you want to add new row in table cells tat try this method:
    Private Sub Wstawienie_wierszy_do_zaznaczonej_tabeli()
    'MVP OShon from VBATools.pl
    Dim Count%, InsertRows%, tbl As Word.Table
    If Selection.Information(wdWithInTable) = False Then _
    MsgBox "You do not select table", vbExclamation, "VBATools.pl": Exit Sub
    InsertRows = InputBox("How meny rows you want to add in tabe", _
    "Add new rows", 1)
    If IsNumeric(InsertRows) = False Then Exit Sub
    
    On Error GoTo blad
    Set tbl = Selection.Tables(1)
    With ActiveDocument.Tables(GetTableIndex(tbl))
     .Rows(.Rows.Count).Select
     Selection.Copy
    End With
    While Count < InsertRows
     Selection.Paste
     Count = Count + 1
    Wend
    Set tbl = Nothing
    Exit Sub
    blad:
    MsgBox Err.Number & " " & Err.Description
    End Sub
    
    Function GetTableIndex(tbl As Word.Table) As Long
    'Cindy Meister MVP
     Dim rng As Word.Range
     Set rng = tbl.Range
     rng.Start = tbl.Parent.Content.Start
     GetTableIndex = CLng(rng.Tables.Count)
    End Function 
    Regards

    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved


    Friday, March 16, 2012 8:30 AM
    Answerer
  • Presumably you are talking about a row of a table in a protected form? In that case you will find two examples of how to add rows to such tables at http://www.gmayor.com/word_vba_examples.htm

    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, March 17, 2012 9:28 AM
  • Thanks for your response.  I have added the row in the protected form.  That part is working correctly.  My problem is that the text that is protected in the new row needs to be edited.  The new row is copied from the row above.  Which I did because I need the form formatting copied, but the text needs to be edited.  Can this be done with a prompt?  One that asks if they would like to edit the text in the protected cells after it adds the row, allows them to enter the new text in prompt and then moves through the row until the user says no?  Would this work?
    Monday, March 26, 2012 5:23 PM