Remove bulk from macro if possible

J

Jean-Guy Marcil

Hi Tonya,

I think that if you delete this line:

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

It will prevent the last form field that is added to be connected with the
macro itself with the On Exit event.

200 a day?
Phew!
Never had that, even during the time I used to use my real email as the
"Reply to" address!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tonya Marshall

That fixed it. Beautiful!
Thank you so much, Jean-Guy.
It's just like I want it.
 
T

Tonya Marshall

Tonya said:
That fixed it. Beautiful!
Thank you so much, Jean-Guy.
It's just like I want it.
One more question (I'm full of them). I would like the toolbar to be
forced to open when creating a new document. I've tried AutoOpen and
AutoExec macros with unlock and lock and can't get it to work. Too many
people close the toolbar and then the next person to use the template
doesn't have a toolbar.
 
C

Charles Kenyon

That is close to a solution I used recently. Four tables have four slightly
different fields and structures. So I have AT enries for the different
fields. My macro unlocks the form, inserts the AutoText entries with the
proper fields in the right place, relocks the form, and selects the first
field just inserted.

Here is one of the macros (assigned a keyboard shortcut and on a toolbar)

Sub AddTimeRow()
'
' AddTimeRow Macro
' Macro written 12/01/2003 by Charles Kyle Kenyon
' Revised 01/16/2004 by Charles Kyle Kenyon
'
' Triggered by F2 key
'
UnprotectDocumentMacro
Dim sTime As String
sTime = ActiveDocument.Bookmarks("TotalIn").Range.Text
If sTime = "0.0" Then
sTime = ActiveDocument.Bookmarks("TotalOut").Range.Text
If sTime = "0.0" Then
ActiveDocument.Bookmarks("TimeTitle").Select
ProtectDocumentMacro
Exit Sub
End If
End If
' Unprotected document
'
'
Application.ScreenUpdating = False
Dim oTemplate As Template
Set oTemplate = Templates(ThisDocument.FullName)
With Selection
.GoTo What:=wdGoToBookmark, Name:="Total1"
.MoveUp Unit:=wdLine, Count:=1
#If VBA6 Then
' Procedure for later versions
.InsertRowsBelow 1
.HomeKey Unit:=wdLine
#Else
' Procedure for Word 97
.InsertRows 1
.HomeKey Unit:=wdLine
.MoveDown Unit:=wdLine, Count:=1
.HomeKey Unit:=wdLine
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
.Extend
.EndKey Unit:=wdLine
.MoveRight Unit:=wdCharacter, Count:=3
.Copy
.Delete Unit:=wdCharacter, Count:=1
.MoveUp Unit:=wdLine, Count:=1
.Paste
.MoveDown Unit:=wdLine, Count:=1
#End If
' oAutoText("TimeLine").Insert
Application.DisplayAutoCompleteTips = True
With AutoCorrect
.CorrectInitialCaps = True
.CorrectSentenceCaps = True
.CorrectDays = True
.CorrectCapsLock = True
.ReplaceText = True
End With
oTemplate.AutoTextEntries("zDateField").Insert Where _
:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeDescription").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeOutOfCourt").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeInCourt").Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
End With
Application.ScreenUpdating = True
Application.ScreenRefresh
ProtectDocumentMacro
End Sub

It calls the UnProtectDocumentMacro and ProtectDocumentMacro. It has
separate code for Word 97 and later versions.

The macro below inserts a row above the current row. It has to sense which
table it is in to choose the proper fields.

Sub InsertRowAboveMe()
'
' InsertRowAboveMe Macro
' Macro written 12/01/03 by Charles Kyle Kenyon
'
' Unprotect document
UnprotectDocumentMacro
'
'
Dim sAutoTextEntry1 As String
Dim sAutoTextEntry2 As String
Dim oTemplate As Template
Set oTemplate = Templates(ThisDocument.FullName)
'
With Selection
.SelectRow
' Test for Table 2
' -------------------------Table 2 - Time -----------------
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count = 2 Then
' Test for time row (4 columns)
If .Columns.Count = 4 Then
'
'' Add row if in a table
' If Selection.Information(wdWithInTable) = True Then
' Selection.Rows.Add BeforeRow:=Selection.Rows(1)
' End If
.InsertRows 1
.HomeKey Unit:=wdLine
Application.DisplayAutoCompleteTips = True
With AutoCorrect
.CorrectInitialCaps = True
.CorrectSentenceCaps = True
.CorrectDays = True
.CorrectCapsLock = True
.ReplaceText = True
End With
oTemplate.AutoTextEntries("zDateField").Insert Where _
:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeDescription").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeOutOfCourt").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeInCourt").Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
End If ' 4 Columns
End If ' Table 2
'
' Test for Table 3
' -------------------------Tables 3 & 4 -
Disbursements ----------------------
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count > 2 Then
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count = 3 Then
sAutoTextEntry1 = "zExpenseDescription"
sAutoTextEntry2 = "zExpenseAmount"
Else ' Table 4 - Payments
sAutoTextEntry1 = "zPaymentDescription"
sAutoTextEntry2 = "zPaymentAmount"
End If
' Test for entry row (3 columns)
If .Columns.Count = 3 Then
'
'' Add row if in a table
' If Selection.Information(wdWithInTable) = True Then
' Selection.Rows.Add BeforeRow:=Selection.Rows(1)
' End If
.InsertRows 1
.HomeKey Unit:=wdLine
Application.DisplayAutoCompleteTips = True
With AutoCorrect
.CorrectInitialCaps = True
.CorrectSentenceCaps = True
.CorrectDays = True
.CorrectCapsLock = True
.ReplaceText = True
End With
oTemplate.AutoTextEntries("zDateField").Insert Where _
:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries(sAutoTextEntry1).Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries(sAutoTextEntry2).Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
End If ' 3 Columns
End If ' Table 3

End With ' Selection
ProtectDocumentMacro
End Sub

Hope these help,
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

Jean-Guy Marcil said:
Hi Tonya,

Would it be possible to change your approach?
If you want a really short macro (thus easier to maintain and modify...) why
not create an autotext representing a row with all form fields already in
the autotext.

Then, all the macro would do is place the cursor under the table an insert
the autotext, then move up to select the first cell...
Is that possible for your context?

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Hi Tonya,

Try adding this to the ThisDocument module of your VBA project:
(Of course, change "My Toolbar" to the relevant name...)

'_______________________________________
Private Sub Document_New()

CommandBars("My Toolbar").Visible = True

End Sub
'_______________________________________

'_______________________________________
Private Sub Document_Open()

CommandBars("My Toolbar").Visible = True

End Sub
'_______________________________________

By the way, "AutoExec" macros are for Global templates that are loaded when
~Word~ loads, not when the document loads.

Also, there is nothing wrong with AutoOpen or AutoClose, but since you seem
to be making good progress with VBA, you should know that they are
deprecated.
Go to the ThisDocument module of a blank document (so as not to mess up your
good template!), notice the two dropdown lists at the top of the code
window?
On the left it says (General) and on the right it says (Declarations). The
one the right should be empty at this point.
Now, select "Document" in the one on the left, this should appear:
'_______________________________________
Private Sub Document_New()

End Sub
'_______________________________________
Leaving the cursor where it is, look at the content of the dropdown list on
the right...

In word there are only 3 events you can catch with this, but do the same in
Excel, there are more...

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tonya Marshall

I must not have done something I should have because there is no
"Document" in the dropdown list.
It says General on the left and Declarations on the right and the
dropdown has all the macros in the template listed.
So, I'm flummoxed as to how to enter the Private subs in the
ThisDocument module.

--
Tonya Marshall
tonz AT harborside DOT com

Jean-Guy Marcil said:
Hi Tonya,

Try adding this to the ThisDocument module of your VBA project:
(Of course, change "My Toolbar" to the relevant name...)

'_______________________________________
Private Sub Document_New()

CommandBars("My Toolbar").Visible = True

End Sub
'_______________________________________

'_______________________________________
Private Sub Document_Open()

CommandBars("My Toolbar").Visible = True

End Sub
'_______________________________________

By the way, "AutoExec" macros are for Global templates that are loaded when
~Word~ loads, not when the document loads.

Also, there is nothing wrong with AutoOpen or AutoClose, but since you seem
to be making good progress with VBA, you should know that they are
deprecated.
Go to the ThisDocument module of a blank document (so as not to mess up your
good template!), notice the two dropdown lists at the top of the code
window?
On the left it says (General) and on the right it says (Declarations). The
one the right should be empty at this point.
Now, select "Document" in the one on the left, this should appear:
'_______________________________________
Private Sub Document_New()

End Sub
'_______________________________________
Leaving the cursor where it is, look at the content of the dropdown list on
the right...

In word there are only 3 events you can catch with this, but do the same in
Excel, there are more...

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Hi Tonya,

Have you double-clicked on "ThisDocument" in the project pane on the left to
access the "ThisDocument" code window on the right?

With your cursor in the ThisDocument code window (which will give you access
to "Document" from the dropdown list on the left at the top of the said
window), you should see in the title bar (the blue bar at the top of the
window):

Microsoft Visual Basic - Document Name - [ThisDocument(code)]

What you are describing seems to be more like what would happen from a
regular module, let's call it NewMacros (the default when using the
recorder), or Module1 (the default when inserting modules from the project
pane):

Microsoft Visual Basic - Document Name - [NewMacros(code)]
or
Microsoft Visual Basic - Document Name - [Module1(code)]

From where you say you cannot access the "Document" choice from the dropdown
list, what do you see in the title bar?

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


Tonya Marshall said:
I must not have done something I should have because there is no
"Document" in the dropdown list.
It says General on the left and Declarations on the right and the
dropdown has all the macros in the template listed.
So, I'm flummoxed as to how to enter the Private subs in the
ThisDocument module.
 
T

Tonya Marshall

Hey Jean-Guy,,
Well I played around in the VBA editor and got it where It should go in
spite of myself. I right-clicked on ThisDocument and selected View Code
and there it was. And it works! :)
Thank you so much.
 
J

Jean-Guy Marcil

Which is equivalent to double-clicking on it...

Have a good weekend!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Thanks,

Like I wrote before, the damage has been done, but you got me thinking...
Why make it worse?

Thanks for the little push!

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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