Call the macro referenced in the field in a record

S

slickdock

I would like my vb code to Call the macro referenced in the field in a
record:

My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:

There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].

In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.

If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.

TIA.
 
D

Doug Robbins - Word MVP

It sounds like you are just "merging" one record, in which case, mail merge
is probably not the best way to go.

I would start with a Word template in which I had DOCVARIABLE fields in
place of the MERGEFIELDS, then I would have the Access code create a new
document from that template, set the values of variables in that document to
the values of the associated fields in the Access record and then executes
the code if applicable based on the entry in the relevant field in the
record. Whether it was a Word macro that was executed or the optional code
was in Access would probably depend upon exactly what it was I wanted to do,
but it would probably be all done by code from Access.

Even if it was more than one record that was being "merged", I would
probably do it the same way.

--
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
 
M

macropod

Hi SlickDoc,

You could put your mergefield inside a MACROBUTTON field, then use code to trigger any MACROBUTTON fields in the document (deleting
the fields as you go, if you want).
 
S

slickdock

It sounds like you are just "merging" one record, in which case, mail merge
is probably not the best way to go.

I would start with a Word template in which I had DOCVARIABLE fields in
place of the MERGEFIELDS, then I would have the Access code create a new
document from that template, set the values of variables in that documentto
the values of the associated fields in the Access record and then executes
the code if applicable based on the entry in the relevant field in the
record.  Whether it was a Word macro that was executed or the optional code
was in Access would probably depend upon exactly what it was I wanted to do,
but it would probably be all done by code from Access.

Even if it was more than one record that was being "merged", I would
probably do it the same way.

--
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




I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file.  Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName].  It may or may not be blank.  If it is not
blank, I would like to call a macro after finishing the merge.  The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -

- Show quoted text -

Thank you very much, but there are many reasons I have not gone this
route. Can you or anyone else answer my original questions?
 
S

slickdock

Hi SlickDoc,

You could put your mergefield inside a MACROBUTTON field, then use code to trigger any MACROBUTTON fields in the document (deleting
the fields as you go, if you want).

--
Cheers
macropod
[MVP - Microsoft Word]



I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file.  Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName].  It may or may not be blank.  If it is not
blank, I would like to call a macro after finishing the merge.  The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -

- Show quoted text -

How would I use code to trigger the macrobutton field?
 
D

Doug Robbins - Word MVP

Use mailmerge events

--
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

It sounds like you are just "merging" one record, in which case, mail
merge
is probably not the best way to go.

I would start with a Word template in which I had DOCVARIABLE fields in
place of the MERGEFIELDS, then I would have the Access code create a new
document from that template, set the values of variables in that document
to
the values of the associated fields in the Access record and then executes
the code if applicable based on the entry in the relevant field in the
record. Whether it was a Word macro that was executed or the optional code
was in Access would probably depend upon exactly what it was I wanted to
do,
but it would probably be all done by code from Access.

Even if it was more than one record that was being "merged", I would
probably do it the same way.

--
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




I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -

- Show quoted text -

Thank you very much, but there are many reasons I have not gone this
route. Can you or anyone else answer my original questions?
 
S

slickdock

Use mailmerge events

--
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


It sounds like you are just "merging" one record, in which case, mail
merge
is probably not the best way to go.
I would start with a Word template in which I had DOCVARIABLE fields in
place of the MERGEFIELDS, then I would have the Access code create a new
document from that template, set the values of variables in that document
to
the values of the associated fields in the Access record and then executes
the code if applicable based on the entry in the relevant field in the
record. Whether it was a Word macro that was executed or the optional code
was in Access would probably depend upon exactly what it was I wanted to
do,
but it would probably be all done by code from Access.
Even if it was more than one record that was being "merged", I would
probably do it the same way.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge..
TIA.- Hide quoted text -
- Show quoted text -

Thank you very much, but there are many reasons I have not gone this
route. Can you or anyone else answer my original questions?- Hide quoted text -

- Show quoted text -

Thank you, but I guess I'm not experienced enough to have that help
me. I found examples of it, but I don't know how to use it.
Thank you anyway.
 
M

macropod

Hi SlickDoc,

Like this:
Sub Demo()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldMacroButton Then oFld.DoClick
Next
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Hi SlickDoc,

You could put your mergefield inside a MACROBUTTON field, then use code to trigger any MACROBUTTON fields in the document
(deleting
the fields as you go, if you want).

--
Cheers
macropod
[MVP - Microsoft Word]



I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -

- Show quoted text -

How would I use code to trigger the macrobutton field?
 
S

slickdock

Hi SlickDoc,

Like this:
Sub Demo()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
  If oFld.Type = wdFieldMacroButton Then oFld.DoClick
Next
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Hi SlickDoc,
You could put your mergefield inside a MACROBUTTON field, then use codeto trigger any MACROBUTTON fields in the document
(deleting
the fields as you go, if you want).
I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge..
TIA.- Hide quoted text -
- Show quoted text -

How would I use code to trigger the macrobutton field?- Hide quoted text -

- Show quoted text -

Wow! Thank you! You are the best!!!
It works GREAT in my normal.dot.
But...One last thing...I'm actually running the code from msAccess.
There is some fine tuning to be done that I don't quite understand to
make it work from Access.
I knew enough to add this:
Dim wdFieldMacroButton As Object
But now when I try to compile in Access, I get method or data member
not found on this line, with wdFieldMacroButton highlighted in yellow:
If oFld.Type = wdFieldMacroButton Then oFld.DoClick
 
D

Doug Robbins - Word MVP

For an example of the use of mail merge events,sSee the "Individual Merge
Letters" item on fellow MVP Graham Mayor's website at:

http://www.gmayor.com/individual_merge_letters.htm


--
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

Use mailmerge events

--
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


It sounds like you are just "merging" one record, in which case, mail
merge
is probably not the best way to go.
I would start with a Word template in which I had DOCVARIABLE fields in
place of the MERGEFIELDS, then I would have the Access code create a new
document from that template, set the values of variables in that
document
to
the values of the associated fields in the Access record and then
executes
the code if applicable based on the entry in the relevant field in the
record. Whether it was a Word macro that was executed or the optional
code
was in Access would probably depend upon exactly what it was I wanted to
do,
but it would probably be all done by code from Access.
Even if it was more than one record that was being "merged", I would
probably do it the same way.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -
- Show quoted text -

Thank you very much, but there are many reasons I have not gone this
route. Can you or anyone else answer my original questions?- Hide quoted
text -

- Show quoted text -

Thank you, but I guess I'm not experienced enough to have that help
me. I found examples of it, but I don't know how to use it.
Thank you anyway.
 
M

macropod

Hi SlickDoc,
There is some fine tuning to be done that I don't quite understand to make it
work from Access.
I knew enough to add this:
Dim wdFieldMacroButton As Object
But now when I try to compile in Access, I get method or data member not
found on this line, with wdFieldMacroButton highlighted in yellow:
If oFld.Type = wdFieldMacroButton Then oFld.DoClick

Do you have your references to the Word object Library set (in the VBE, Tools|References|Word # Object Library)?
If so, you should then be able to run the routine like:
Sub Demo()
Dim wdApp As Word.Application
Set wdApp = Word.Application
Dim wdDoc As Word.Document
Set wdDoc = wdApp.ActiveDocument
wdApp.Visible = True
With wdDoc
Dim oFld As Object
For Each oFld In wdDoc.Fields
If oFld.Type = wdFieldMacroButton Then oFld.DoClick
Next
End With
End Sub
 
M

macropod

Minor variation - there's probably no need for 'wdApp.Visible = True', plus it's probably better to use 'Dim oFld As Field'. The
code below also deletes the macrobutton field after the macro's been run:
Sub Demo()
Dim wdApp As Word.Application
Set wdApp = Word.Application
Dim wdDoc As Word.Document
Set wdDoc = wdApp.ActiveDocument
With wdDoc
Dim oFld As Field
For Each oFld In wdDoc.Fields
If oFld.Type = wdFieldMacroButton Then
oFld.DoClick
oFld.Delete
End If
Next
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Hi SlickDoc,

Like this:
Sub Demo()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldMacroButton Then oFld.DoClick
Next
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Hi SlickDoc,
You could put your mergefield inside a MACROBUTTON field, then use code to trigger any MACROBUTTON fields in the document
(deleting
the fields as you go, if you want).
I would like my vb code to Call the macro referenced in the field in a
record:
My vb code starts in MS Access. It saves a record as a Word merge data
file and merges it with a Word merge form file. Once the merge is
complete, I would like the code to make this determination:
There will always be a field in the merge data file called
[OptionalChainedMacroName]. It may or may not be blank. If it is not
blank, I would like to call a macro after finishing the merge. The
macro to be called is equal to the contents of the
[OptionalChainedMacroName].
In this example, lets say the contents of the field
[OptionalChainedMacroName] is "Macro_For_Form_A."
Sub Macro_For_Form_A() exists in the normal.dot.
If the field in the merge data file called [OptionalChainedMacroName]
is not blank, call Macro "Macro_For_Form_A" after finishing the merge.
TIA.- Hide quoted text -
- Show quoted text -

How would I use code to trigger the macrobutton field?- Hide quoted text -

- Show quoted text -

Wow! Thank you! You are the best!!!
It works GREAT in my normal.dot.
But...One last thing...I'm actually running the code from msAccess.
There is some fine tuning to be done that I don't quite understand to
make it work from Access.
I knew enough to add this:
Dim wdFieldMacroButton As Object
But now when I try to compile in Access, I get method or data member
not found on this line, with wdFieldMacroButton highlighted in yellow:
If oFld.Type = wdFieldMacroButton Then oFld.DoClick
 

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