[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS Word, create new formField and rename

Posted on 2006-05-01
10
Medium Priority
?
399 Views
Last Modified: 2008-03-10
Hi,

I have a row of formFields:

txt_item_01
txt_quantity_01
txt_description_01
txt_each_01
txt_total_01

on Exit macro of txt_total asks the user if they want to add another item.  If yes then i want to replicate the above fields under this row with a carriage return.  

txt_item_02
txt_quantity_02
txt_description_02
txt_each_02
txt_total_02

and then assign the relevant macros on exit of certain fields so we can ask the user again

Thanks

Ross
0
Comment
Question by:rossh1977
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16576550
Two points before we get into the detail.
First, I'd advise posting Word questions in the Word area.
Secondly, I recommend that the fields are placed in a table. Are they, or could they be?
0
 

Author Comment

by:rossh1977
ID: 16576606
Hi They are in a table at the moment.  however i only have one row which fills the height of the document, but i could work something out.  Do you want me to close this question and put it in the Word area?

Thanks

R
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16576839
No. It was just advice about getting the best response. Not many VB experts know the Word  object model, while mosr Word experts know VBA.

I have been working on a solution, but I have that worst of situations, in that the code works OK if I step through it, but fails at a run.

For you interest, here is the code. It creates a new row and adds new formfields, but applies the naming and exit macro to the first cell in the new row, instead of each one. I will resume work after I'm back from the local May Fair

Sub ExitMacro()
Dim tbl As Table
Dim rw2 As Row
Dim rw1 As Row
Dim cl1 As Cell
Dim cl2 As Cell
Dim ffld1 As FormField
Dim ffld2 As FormField
Dim strNameParts() As String

Set tbl = Selection.Tables(1)
Set rw1 = Selection.Rows(1)
If rw1.Index = tbl.Rows.Count Then
    ActiveDocument.Unprotect '"Password"
    Set rw2 = tbl.Rows.Add
    For Each cl1 In rw1.Cells
        If cl1.Range.FormFields.Count = 1 Then
            Set ffld1 = cl1.Range.FormFields(1)
            DoEvents
            Set cl2 = rw2.Cells(cl1.ColumnIndex)
            DoEvents
            Debug.Print cl2.ColumnIndex,
            Set ffld2 = ActiveDocument.FormFields.Add(cl2.Range, ffld1.Type)
            DoEvents
            strNameParts = Split(ffld1.Name, "_")
            strNameParts(1) = Format$(Val(strNameParts(1) + 1), "00")
            DoEvents
            ffld2.Name = Join(strNameParts, "_")
            ffld2.ExitMacro = ffld1.ExitMacro
            DoEvents
            Debug.Print ffld2.Range.Cells(1).ColumnIndex
        End If
    Next cl1
    ActiveDocument.Protect wdAllowOnlyFormFields, True ', "Password"
End If
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rossh1977
ID: 16577940
Hi thanks very much, this is great!  Not having much luck though!  I was trying to simplfiy your code 1 table 1 row create new row and add field, no renaming etc, but i couldn't get it to work

Thanks

R
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16578285
Tombola, morris and maypole dancing, coconut shy,  cake stall ... OK, I've managed to drag myself away.

It seems that the line
          Set ffld2 = ActiveDocument.FormFields.Add(cl2.Range, ffld1.Type)
does create a new formfield, but sets the ffld2 object to the formfield in the first cell of the row. This new version refinds the formfield that was just created.

Sub ExitMacroForTotalField()
    Dim tbl As Table
    Dim rw2 As Row
    Dim rw1 As Row
    Dim cl1 As Cell
    Dim cl2 As Cell
    Dim ffld1 As FormField
    Dim ffld2 As FormField
    Dim strNameParts() As String
   
    Set tbl = Selection.Tables(1)
    Set rw1 = Selection.Rows(1)
    If rw1.Index = tbl.Rows.Count Then
        ActiveDocument.Unprotect '"Password"
        Set rw2 = tbl.Rows.Add
        For Each cl1 In rw1.Cells
            If cl1.Range.FormFields.Count = 1 Then
                Set ffld1 = cl1.Range.FormFields(1)
                Set cl2 = rw2.Cells(cl1.ColumnIndex)
                ActiveDocument.FormFields.Add cl2.Range, ffld1.Type
                Set ffld2 = tbl.Cell(tbl.Rows.Count, cl1.ColumnIndex).Range.FormFields(1)
                strNameParts = Split(ffld1.Name, "_")
                strNameParts(1) = Format$(Val(strNameParts(1) + 1), "00")
                ffld2.Name = Join(strNameParts, "_")
                ffld2.ExitMacro = ffld1.ExitMacro
                DoEvents
            End If
        Next cl1
        ActiveDocument.Protect wdAllowOnlyFormFields, True ', "Password"
    End If
End Sub



0
 

Author Comment

by:rossh1977
ID: 16579639
Good for you too, that stuff i'll get to you.  Can't have too much excitement in one day!  Tried this out but i think it's above me not really sure what's going on.  I'm thinking of using one form field and spliting the string on carriage returns trying it that way, thanks very much for your help so far.  I'll keep looking at it and try and understand what's happening.

Cheers

R
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 16579828
OK. To test this out, I created a one-row table, put a formfield in each cell and named the formfields like *_01. I put the code in a module in the Normal template, selected it as the exit macro for the last formfield in the row and protected the document.

To test, simply tab through the fields. When you tab out of the last field in the table, an extra row is created.
I'l add a few comments.

Sub ExitMacroForTotalField()
    Dim tbl As Table
    Dim rw2 As Row
    Dim rw1 As Row
    Dim cl1 As Cell
    Dim cl2 As Cell
    Dim ffld1 As FormField
    Dim ffld2 As FormField
    Dim strNameParts() As String
   
    Set tbl = Selection.Tables(1)                          'find the table that we are in
    Set rw1 = Selection.Rows(1)                          'catch the row of the field that we have just left
    If rw1.Index = tbl.Rows.Count Then                'make sure we are in the last row
        ActiveDocument.Unprotect '"Password"
        Set rw2 = tbl.Rows.Add                             'add a new row at the bottom of the table
        For Each cl1 In rw1.Cells                            'step through each cell in the row
            If cl1.Range.FormFields.Count = 1 Then  'check that there is a formfield to copy
                Set ffld1 = cl1.Range.FormFields(1)    'make an object variable of the existing form field
                Set cl2 = rw2.Cells(cl1.ColumnIndex)  'make an object of the cell below
                ActiveDocument.FormFields.Add cl2.Range, ffld1.Type   'create a new formfiled of the same type
                Set ffld2 = tbl.Cell(tbl.Rows.Count, cl1.ColumnIndex).Range.FormFields(1) 'get the new formfield into an object
                strNameParts = Split(ffld1.Name, "_")  
                strNameParts(1) = Format$(Val(strNameParts(1) + 1), "00") 'increment the formfield name
                ffld2.Name = Join(strNameParts, "_")

                ffld2.ExitMacro = ffld1.ExitMacro 'copy the Exit macro
                DoEvents 'let other proccesses happen (just in case)
            End If
        Next cl1
        ActiveDocument.Protect wdAllowOnlyFormFields, True ', "Password"
    End If
End Sub
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

834 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