Solved

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

Posted on 2013-01-15
7
259 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 50

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

808 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