Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-15
7
Medium Priority
?
263 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 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
New style of hardware planning for Microsoft Exchange server.
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…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

715 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