Adding a row (macro)

S

shanbones

I am using the following macro (provided by a newsgroup poster) to try and
have a row automatically added to a table in a protected form when the field
is tabbed out of:

' To add a new row to a table containing formfields in every column

' automatically on exit from the last cell in the present last row of the
table

Dim rownum As Integer, i As Integer

ActiveDocument.Unprotect

ActiveDocument.Tables(1).Rows.Add

rownum = ActiveDocument.Tables(1).Rows.Count

For i = 1 To ActiveDocument.Tables(1).Columns.Count

ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,
Type:=wdFieldFormTextInput

Next i

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro =
"addrow"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.FormFields(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True


However, I received an error message when I try to run it. The := of the
following section was hightlighted and the error message says "Compile Error:
Expected: expression"

Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,

So I altered the above line by replacing the = with a space. Now the error
hightlighting does not highlight that section but has moved to the line above
it (ActiveDocument.FormFields.Add) highlighting the .Add section and giving
me the following error message "Compile error: Argument not optional."

Any assistance that can be provided would be of great help!
shanbones
 
S

shanbones

One additional item of interest. When I try to run the macro with the form in
an unprotected/unlocked state it works just fine. I only have the error when
I run it from a protected state.

shanbones
 
J

Jay Freedman

You've been caught by a bad line break in the newsgroup posting. The
line

ActiveDocument.FormFields.Add

and the following line that starts with Range:=ActiveDocument.Tables
are supposed to be just one statement. Within the VBA editor you can
put those two lines on one line -- just delete the carriage return
after the .Add -- but it doesn't fit within the margins of the
newsgroup reader.

Alternatively, you can have pieces of the single statement on two
lines by inserting a "continuation character" at the end of the first
line. That's an underscore, which must be preceded by a space an
immediately followed by the carriage return. See
http://word.mvps.org/FAQs/MacrosVBA/_AtEndOfLine.htm.
 
S

shanbones

Thank you for the responce. I have put both lines on the same line. It works
when I have the form unprotected, but it gives me the following error message
when I try to run it with the form protected:

Compile error: Method or data member not found
Once this message pops up it takes me into VBA editor and highlights
".AddRange:" portion of the same line in question as my earlier post.

I tried putting a "." in front of "Range" but when I try to run the macro
(in protected form) I get a message that says "Can't execute code in break
mode." (As before, it works fine when it is in unprotected mode.)

shanbones
 
D

Doug Robbins

Do you have a password set on the document? The first command in the macro
unprotects the document, but it will fail if the password is not supplied.
Put the selection after Unprotect and press F1 to access the VBA Help file
and see how to use code to supply the password.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

shanbones

No, it is not password protected.

Doug Robbins said:
Do you have a password set on the document? The first command in the macro
unprotects the document, but it will fail if the password is not supplied.
Put the selection after Unprotect and press F1 to access the VBA Help file
and see how to use code to supply the password.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins

Well, as I said, the first command in the macro unprotects the document, so
it should (and does for me and others) work when the document is protected.
However now I see that you may have the words Add and Range as one word
AddRange. You need to have a space between Add and Range.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

shanbones

That took care of the main error message...thank you. Now, however, I get
taken to the very first part of the macro "Sub addnewline ()" and I see the
error message "Can't execute in break mode."

I have also noticed that when the macro does run (in unprotected mode) it
will add a new line to the table, but it will not copy the text form fields
that are in the original row.

shanbones
 
D

Doug Robbins

From the Run menu in the VBA editor, select Reset.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

shanbones

Ok, I have done that. I now get an error message that says "Compile error:
Argument not optional."

This is getting quite frustrating.
shanbones
 
D

Doug Robbins

And what line of code is highlighted when that happens?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

shanbones

I really appreciate your time in helping me to resolve this matter!

Primarily the ".Add" section of the following portion is highlighted:

ActiveDocument.FormFields.Add Range: Selection.Tables(1).Cell(rownum,
i).Range , Type:=wdFieldFormTextInput

Secondarily, the "Sub addanewline ()" line is highlighted yellow.

Thanks again,
shanbones
 
D

Doug Robbins

If you take a look at the macro that you started with, you will see that
there is an = between Range: and Selection

[othercode] Add Range: = Selection[other code]

In your email message, there is a space between Sub addanewline and the (.
There should be no space

Sub addanewline()

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

shanbones

IT WORKS!!!

Thank you so very, very much!
shanbones

Doug Robbins said:
If you take a look at the macro that you started with, you will see that
there is an = between Range: and Selection

[othercode] Add Range: = Selection[other code]

In your email message, there is a space between Sub addanewline and the (.
There should be no space

Sub addanewline()

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
shanbones said:
I really appreciate your time in helping me to resolve this matter!

Primarily the ".Add" section of the following portion is highlighted:

ActiveDocument.FormFields.Add Range: Selection.Tables(1).Cell(rownum,
i).Range , Type:=wdFieldFormTextInput

Secondarily, the "Sub addanewline ()" line is highlighted yellow.

Thanks again,
shanbones
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top