Forum Discussion

PMHunt1955's avatar
PMHunt1955
Brass Contributor
Jul 26, 2019
Solved

'Calling' a function from within a Word document

I know this is probably really basic but this will be my first real dive into Word VBA, I am quite experienced in Excel VBA, though.   I am having difficulty getting my head around how Word 'knows'...
  • Jay Freedman's avatar
    Jul 28, 2019

    PMHunt1955 wrote:

    I am having difficulty getting my head around how Word 'knows' that a 'block of text' is code, rather than just text of the document.


    The difficulty is that Word doesn't have any way to "call" a macro automatically from the mere existence of a particular thing in the document text. The closest feature that exists is a MACROBUTTON field that contains the name of a macro, but it executes only when you double-click the field's display text. 

     

    The solution to this problem is to write a larger macro that (a) runs the mail merge and then (b) carries out the split or other functions on the resulting document. The merge main document would need to contain some "marker" to tell the macro where the merged data resides.

     

    Another solution, probably easier to handle, would be to modify the Excel source by adding new columns to contain the split data, with unique column names that would be used only in the affected document. The other documents wouldn't even "see" the added columns.

Resources