Solved

Add Row Macro with counts, formatting and data - Word 2007

Posted on 2013-01-15
7
256 Views
Last Modified: 2013-04-01
Hi people,

I'm trying to create a macro which will insert a new row, before the last one (I can manage this thus far), into a protected document with form fields in the three right cells, along with the dollar sign infront as in the picture.

In the first left cell, I need the table to count the items upon adding the new row.

Thanks!

table example
Here is the code I'm using elsewhere in my document successfully however there is no need to count or include other text too in the cell.

Sub AddTableRow1()
    Dim rw As Row
    Dim cl As Cell
    Dim ffld As FormField
   
    ActiveDocument.Unprotect Password:="000"
    Set rw = ActiveDocument.Tables(1).Rows.Add
    For Each cl In rw.Cells
      Set ffld = ActiveDocument.FormFields.Add(cl.Range, wdFieldFormTextInput)
    Next cl
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

________________________________________________________________________
Similarly, deletion of the row above the last would be helpful.

Below is the code I'm using to simply delete the very bottom row so I'm hoping it's only a minor change from this.
________________________________________________________________________
Sub DelTableRow1()
 
 Dim intTable As Integer, intRow As Integer
 
 ActiveDocument.Unprotect Password:="000"
 
 intRow = ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Rows(intRow).Delete
 
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
0
Comment
Question by:RThreeOne
  • 4
7 Comments
 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
ID: 38781870
Hi,

This should help

Sub AddTableRow1()
    Dim rw As Row
    Dim cl As Cell
    Dim ffld As FormField
    
 '  ActiveDocument.Unprotect Password:="000"
    ActiveDocument.Tables(1).Rows(ActiveDocument.Tables(1).Rows.Count).Select
    Selection.InsertRowsAbove 1
'    For Each cl In rw.Cells
'      Set ffld = ActiveDocument.FormFields.Add(cl.Range, wdFieldFormTextInput)
'    Next cl
'    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub



Sub DelTableRow1()


'
' Dim intTable As Integer, intRow As Integer
'
' ActiveDocument.Unprotect Password:="000"
'
intLastRow = ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Rows(intLastRow - 1).Delete
'
'    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
'

Sub AddTextToTableCells()
    Dim intCell As Integer
    Dim oCell As Cell
    
    ActiveDocument.Tables(1).Cell(3, 1).Select
    Selection.MoveDown Unit:=wdLine, Count:=(ActiveDocument.Tables(1).Rows.Count - 4), Extend:=wdExtend
    intCell = 1

    For Each oCell In Selection.Cells
        oCell.Range.Text = "Cell " & intCell
        intCell = intCell + 1
    Next oCell
End Sub

Open in new window


Regards
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 38782208
You could do those things like this.
Sub InsertTableRow()
     ActiveDocument.Tables(1).Rows.Add ActiveDocument.Tables(1).Rows.Last
End Sub

Sub DelPenultimateTableRow()
    ActiveDocument.Tables(1).Rows.Last.Previous.Delete
End Sub

Sub AddItemCount()
    ActiveDocument.Tables(1).Cell(3, 1).Range.Text = ActiveDocument.Tables(1).Rows.Count - 3
End Sub

Open in new window


I would advise against putting ActiveX buttons on the document. They won't look appropriate on the printout.
0
 

Author Comment

by:RThreeOne
ID: 38803645
Thanks guys and sorry about the delay, I'm only at the workplace 2 days a week (engineering internship; and yes, I have basically no macro experience prior to this).

Unfortunately neither of the attempts solves the problem yet;

GrahamSkan, Your solution adds a row to the second last column, however with no formfields within the cells, and the count only updates the number in cell A3 (Part 1 in the photo). I was after every new row to resemble row 3 in the diagram; formfields in each row, $, and column A to have counting part numbers, eg 1, 2, 3...
I am also after a separate delete which will delete the second last row please.

RGonzo, your solution looks closer to what I'm after, but upon using it, first time around the following line creates an error;

ActiveDocument.Unprotect Password:="000"

which I'm finding confusing. Trying again, this line gives an error;

Selection.InsertRowsAbove 1

Regarding the delete function, using it prior to inserting a new row unsuccessfully, the document errors on this line;

ActiveDocument.Tables(3).Rows(intLastRow - 1).Delete

Thanks again for your time and help.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:RThreeOne
ID: 39023822
Digging this back up sorry guys;

I have changed the original task and removedthe need for counting anything within the cells.

I am purely after a new penultimate row with form fields within each cell in the new row.

Currently using this code I get the folliwing error;

"Object variable or With block variable not set"

Visually, the new row is added but then the error follows. If i completely remove the bold section, rows are both added and deleted (with another macro) as desired, of course, without form fields.

Sub AddTableRow2(control As IRibbonControl)

    Dim rw As Row
    Dim cl As Cell
    Dim ffld As FormField
 
    ActiveDocument.Unprotect Password:="000"
    ActiveDocument.Tables(3).Rows(ActiveDocument.Tables(3).Rows.Count).Select
    Selection.InsertRowsAbove
 
       For Each cl In rw.Cells
      Set ffld = ActiveDocument.FormFields.Add(cl.Range, wdFieldFormTextInput)
    Next cl

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub

Once again, thanks for the help!
0
 

Accepted Solution

by:
RThreeOne earned 0 total points
ID: 39027414
Ok so it seems I solved this myself with a combination of information from this thread. Here is the solution, now just to work on the cell formatting. Thanks guys!

Sub AddTableRow2(control As IRibbonControl)

    Dim rw As Row
    Dim cl As Cell
    Dim ffld As FormField
 

    ActiveDocument.Unprotect Password:="000"
    ActiveDocument.Tables(3).Rows(ActiveDocument.Tables(3).Rows.count).Select
   
    Selection.InsertRowsAbove
 
   Set rw = ActiveDocument.Tables(3).Rows.Last.Previous
    For Each cl In rw.Cells
      Set ffld = ActiveDocument.FormFields.Add(cl.Range, wdFieldFormTextInput)
    Next cl

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
0
 

Author Closing Comment

by:RThreeOne
ID: 39036809
The only solution that worked 100% correctly, however it was completely built of the other suggestions. Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now