Solved

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

Posted on 2013-01-15
7
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 52

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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

632 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