Log in

View Full Version : Form automation in Word 2007



rcampbell
08-27-2011, 03:46 PM
Hello VBA Community. I am using Word 2007 and am attempting to automate several forms that I use fairly often. My goal is to have all of the form fields throughout the document auto-populate upon entering variable information on a "top page." Each macro is supposed to run upon exiting a form field.

I am receiving the following error when the macro attempts to run.
"Run Time Error 4605: This method or property is not available because the document is a protected document."

A sample of the code that was recorded is as follows:
Sub CopyStreetAddress()
'
' CopyStreetAddress Macro
'
'
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=83
Selection.MoveUp Unit:=wdLine, Count:=20
Selection.MoveDown Unit:=wdLine, Count:=8
Selection.MoveLeft Unit:=wdCharacter, Count:=11
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
ActiveWindow.ActivePane.SmallScroll Down:=36
Selection.MoveDown Unit:=wdLine, Count:=11
Selection.MoveUp Unit:=wdLine, Count:=4
Selection.MoveLeft Unit:=wdCharacter, Count:=19
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.SmallScroll Down:=1
Selection.MoveDown Unit:=wdLine, Count:=252
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=51
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=125
Selection.MoveUp Unit:=wdLine, Count:=10
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=38
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=205
Selection.MoveUp Unit:=wdLine, Count:=9
Selection.MoveLeft Unit:=wdCharacter, Count:=14
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=53
Selection.MoveUp Unit:=wdLine, Count:=11
Selection.MoveRight Unit:=wdCharacter, Count:=45
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=110
ActiveWindow.ActivePane.SmallScroll Down:=56
Selection.MoveUp Unit:=wdLine, Count:=195
End Sub

As I have virtually no experience in VBA, I'd appreciate the simplest solution/response to my problem.
Many thanks!

Frosty
08-29-2011, 12:18 PM
Sorry, I only give the most complex solutions/responses :)

Try recording a macro which unprotects your document. And also record a macro which protects your document.

Look at the contents of those recorded macros (they will be simple one line macros), and then put them at the beginning and end of each of the macros you want to run when you're exiting the text field.

Additional info:
A quick perusal of your macro seems like it will be incredibly breakable, as it is heavily based on moving your cursor up and down by lines (the number of which obviously change if you modify your document template -- i.e., moving down 110 lines, then moving back up 195 lines breaks if you add or remove a single line of text).

But to give you a more complex answer would require a deeper level of understanding of the process on your part. Maybe you should take a look at some of the Content Controls tips on Greg Maxey's web site: http://gregmaxey.mvps.org/word_tips.htm.

There are a lot of code samples there which can help point you in the right direction.

Talis
08-29-2011, 12:41 PM
The particular information you require is at http://gregmaxey.mvps.org/Repeating_Data.htm

As you are using Word 2007 pay particular attention to 7. Word2007 & Content Controls. at the above link.

Guess it depends how much you are already committed to your current document's set-up.