Solved

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

Posted on 2013-01-15
7
253 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 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:RThreeOne
Comment Utility
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
Comment Utility
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
Comment Utility
The only solution that worked 100% correctly, however it was completely built of the other suggestions. Thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

8 Experts available now in Live!

Get 1:1 Help Now