SOLVED
Home

'Calling' a function from within a Word document

%3CLINGO-SUB%20id%3D%22lingo-sub-774544%22%20slang%3D%22en-US%22%3E'Calling'%20a%20function%20from%20within%20a%20Word%20document%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-774544%22%20slang%3D%22en-US%22%3E%3CP%3EI%20know%20this%20is%20probably%20really%20basic%20but%20this%20will%20be%20my%20first%20real%20dive%20into%20Word%20VBA%2C%20I%20am%20quite%20experienced%20in%20Excel%20VBA%2C%20though.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20having%20difficulty%20getting%20my%20head%20around%20how%20Word%20'knows'%20that%20a%20'block%20of%20text'%20is%20code%2C%20rather%20than%20just%20text%20of%20the%20document.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20my%20first%20'excursion'%2C%20within%20Word%20VBA%2C%20I%20am%20seeking%20to%20split%20a%20phrase%2C%20when%20it%20is%20passed%2C%20through%20mail%20merge%2C%20from%20an%20Excel%20spreadsheet%20to%20a%20Word%20document.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20apologies%20for%20i.%20the%20fact%20that%20my%20interface%20to%20this%20forum%20does%20not%20permit%20me%20to%20see%20all%20the%20proper%20formatting%20options%20for%20code%20and%20ii.%20the%20following%20is%20more%20of%20a%20'storyboard'%20version%20of%20code%2C%20rather%20than%20the%20proper%20thing.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20relevant%20Excel%20spreadsheet%20is%20used%20to%20support%20a%20group%20of%20mail%20merge%20letters%20and%20this%20'issue'%20is%20only%20relevant%20to%20one%20letter%2C%20so%20I%20do%20not%20wish%20to%20redesign%20the%20spreadsheet%3B%20I%20would%20prefer%20to%20handle%20the%20'issue'%20within%20Word%2C%20through%20VBA%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20focus%20on%20two%20mail%20merge%20fields%2C%20within%20this%20question.%26nbsp%3B%20The%202%20mail%20merge%20fields%20are%20called%2C%20for%20the%20sake%20of%20simplicity%20here%2C%20i)%20Relative_Name%20and%20ii)%20Relative_Relationship.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERelative_Relationship%20contains%2C%20for%20example%2C%20the%20value%20%22Second%20Cousin%20Twice%20Removed%20of%20the%20Husband%20of%20my%20Second%20Cousin%20Once%20Removed%22%2C%20and%2C%20just%20to%20make%20up%20a%20name%2C%20Relative_Name%20contains%20%22Walter%20Gerard%20Montague%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20when%20I%20simply%20include%20the%20fields%20in%20the%20other%20letters%20I%20simply%20type%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20reviewing%20the%20online%20tree%20which%20has%20brought%20you%20to%20my%20attention%2C%20I%20can%20see%20that%20your%20tree%20includes%20%C2%ABRelative_Name%C2%BB.%26nbsp%3B%20Your%20relationship%20to%20me%3A%26nbsp%3B%C2%ABRelative_Relationship%C2%BB.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20I%20would%20end%20up%20with%20a%20final%20merged%20text%20that%20reads%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20reviewing%20the%20online%20tree%2C%20which%20has%20brought%20you%20to%20my%20attention%2C%20I%20can%20see%20that%20your%20tree%20includes%20Walter%20Gerard%20Montague.%26nbsp%3B%20Your%20relationship%20to%20me%3A%20Second%20Cousin%20Twice%20Removed%20of%20the%20Husband%20of%20my%20Second%20Cousin%20Once%20Removed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20this%20occasion%2C%20though%20I%20want%20to%20split%20the%20contents%20of%20the%20field%20Relative_Relationship%20into%20two%20halves%2C%20through%20passing%20it%20to%20a%20VBA%20macro%2C%20so%20that%20I%20would%20end%20up%20with%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20reviewing%20the%20online%20tree%2C%20which%20has%20brought%20you%20to%20my%20attention%2C%20I%20can%20see%20that%20your%20tree%20includes%20Walter%20Gerard%20Montague.%26nbsp%3B%20Walter%20Gerard%20Montague%20is%20the%20Husband%20of%20my%20Second%20Cousin%20Once%20Removed%20and%20you%20are%20his%20Second%20Cousin%20Twice%20Removed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20for%20the%20sake%20of%20the%20following%20let's%20call%20the%20VBA%20function%2C%20which%20I%20propose%20to%20write%2C%20%3CSTRONG%3ERelationship_Phrase_Split.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20here%20is%20the%20nub%20of%20my%20question%2C%20how%20does%20Word%20know%20I%20am%20invoking%20the%20subroutine%20when%20I%20write%2C%20in%20the%20mail%20merge%20document%2C%20something%20that%20maybe%20looks%20like%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBased%20on%20reviewing%20the%20online%20tree%2C%20which%20has%20brought%20you%20to%20my%20attention%2C%20I%20can%20see%20that%20your%20tree%20includes%20%C2%ABRelative_Name%C2%BB.%26nbsp%3B%20%3CEM%3ERelationship_Phrase_Split(%C2%ABRelative_Relationship%C2%BB%2C%20maybe%20some%20other%20fields%20from%20the%20Excel%20spreadsheet%20to%20control%20how%20the%20splitting%20is%20done%2C%20e.g.%20%C2%ABGender%C2%BB)%3C%2FEM%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20only%20italicised%2C%20in%20the%20above%2C%20just%20to%20highlight%20where%20I%20am%20needing%20assistance%20to%20understand%20how%20to%20cause%20Word%20to%20process%20the%20code.%26nbsp%3B%20I%20am%20not%20asking%20for%20help%20on%20the%20split%20function%2C%20only%20the%20correct%20syntax%20for%20calling%20it%2C%20please.%26nbsp%3B%20I%20have%20made%20the%20document%20a%20.docm%20extension.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20thanks%20in%20anticipation.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPhilip%3C%2FP%3E%3CP%3EBendigo%2C%20Vioctoria%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-774544%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E2016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESyntax%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVba%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWord%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-776682%22%20slang%3D%22en-US%22%3ERe%3A%20'Calling'%20a%20function%20from%20within%20a%20Word%20document%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-776682%22%20slang%3D%22en-US%22%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F313780%22%20target%3D%22_blank%22%3E%40PMHunt1955%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3E%3CP%3EI%20am%20having%20difficulty%20getting%20my%20head%20around%20how%20Word%20'knows'%20that%20a%20'block%20of%20text'%20is%20code%2C%20rather%20than%20just%20text%20of%20the%20document.%3C%2FP%3E%0A%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%0A%3CP%3EThe%20difficulty%20is%20that%20Word%20doesn't%20have%20any%20way%20to%20%22call%22%20a%20macro%20%3CEM%3Eautomatically%3C%2FEM%3Efrom%20the%20mere%20existence%20of%20a%20particular%20thing%20in%20the%20document%20text.%20The%20closest%20feature%20that%20exists%20is%20a%20MACROBUTTON%20field%20that%20contains%20the%20name%20of%20a%20macro%2C%20but%20it%20executes%20only%20when%20you%20double-click%20the%20field's%20display%20text.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20solution%20to%20this%20problem%20is%20to%20write%20a%20larger%20macro%20that%20(a)%20runs%20the%20mail%20merge%20and%20then%20(b)%20carries%20out%20the%20split%20or%20other%20functions%20on%20the%20resulting%20document.%20The%20merge%20main%20document%20would%20need%20to%20contain%20some%20%22marker%22%20to%20tell%20the%20macro%20where%20the%20merged%20data%20resides.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20solution%2C%20probably%20easier%20to%20handle%2C%20would%20be%20to%20modify%20the%20Excel%20source%20by%20adding%20new%20columns%20to%20contain%20the%20split%20data%2C%20with%20unique%20column%20names%20that%20would%20be%20used%20only%20in%20the%20affected%20document.%20The%20other%20documents%20wouldn't%20even%20%22see%22%20the%20added%20columns.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-776748%22%20slang%3D%22en-US%22%3ERe%3A%20'Calling'%20a%20function%20from%20within%20a%20Word%20document%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-776748%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Jay%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20very%20much%20for%20that%26nbsp%3B%20insight.%26nbsp%3B%20I%20was%20really%20wandering%20what%20I%20had%20missed%2C%20as%20I%20have%20been%20using%20Office%20for%20many%20years.%26nbsp%3B%20It%20leads%20me%20on%20to%20wonder%20about%20what%20good%20uses%20are%20available%2C%20for%20VBA%2C%20in%20Word%2C%20but%20that%20is%20a%20question%20for%20another%20day.%26nbsp%3B%20My%20nacent%20Word%20VBA%20skills%20are%20not%20yet%20at%20a%20stage%20where%20I%20can%20work%20out%20how%20to%20write%20a%20macro%20that%20calls%20mail%20merge%2C%20finds%20a%20'marker'%20in%20the%20document%20and%20then%20performs%20the%20necessary%20split%2C%20so%2C%20for%20now%2C%20I%20will%20go%20down%20the%20route%20of%20adding%20fields%20to%20the%20Excel%20source.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20anybody%20at%20Microsoft%20is%20reading%20this%2C%20maybe%20you%20could%20look%20into%20developing%20a%20system%2C%20within%20Word%2C%20that%20does%20provide%20an%20ability%20to%20simply%20call%20VBA%20functions%2C%20during%20a%20mail%20merge%2C%20in%20the%20way%20that%20I%20have%20suggested%20in%20my%20original%20post.%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPhilip%3C%2FP%3E%3CP%3EBendigo%2C%20Victoria%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
PMHunt1955
Contributor

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' that a 'block of text' is code, rather than just text of the document.

 

As my first 'excursion', within Word VBA, I am seeking to split a phrase, when it is passed, through mail merge, from an Excel spreadsheet to a Word document.

 

With apologies for i. the fact that my interface to this forum does not permit me to see all the proper formatting options for code and ii. the following is more of a 'storyboard' version of code, rather than the proper thing.

 

The relevant Excel spreadsheet is used to support a group of mail merge letters and this 'issue' is only relevant to one letter, so I do not wish to redesign the spreadsheet; I would prefer to handle the 'issue' within Word, through VBA code.

 

I will focus on two mail merge fields, within this question.  The 2 mail merge fields are called, for the sake of simplicity here, i) Relative_Name and ii) Relative_Relationship.

 

Relative_Relationship contains, for example, the value "Second Cousin Twice Removed of the Husband of my Second Cousin Once Removed", and, just to make up a name, Relative_Name contains "Walter Gerard Montague".

 

Now when I simply include the fields in the other letters I simply type -

 

Based on reviewing the online tree which has brought you to my attention, I can see that your tree includes «Relative_Name».  Your relationship to me: «Relative_Relationship».

 

and I would end up with a final merged text that reads -

 

Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes Walter Gerard Montague.  Your relationship to me: Second Cousin Twice Removed of the Husband of my Second Cousin Once Removed.

 

On this occasion, though I want to split the contents of the field Relative_Relationship into two halves, through passing it to a VBA macro, so that I would end up with -

 

Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes Walter Gerard Montague.  Walter Gerard Montague is the Husband of my Second Cousin Once Removed and you are his Second Cousin Twice Removed.

 

Now for the sake of the following let's call the VBA function, which I propose to write, Relationship_Phrase_Split.

 

Now here is the nub of my question, how does Word know I am invoking the subroutine when I write, in the mail merge document, something that maybe looks like -

 

Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes «Relative_Name».  Relationship_Phrase_Split(«Relative_Relationship», maybe some other fields from the Excel spreadsheet to control how the splitting is done, e.g. «Gender»).

 

I have only italicised, in the above, just to highlight where I am needing assistance to understand how to cause Word to process the code.  I am not asking for help on the split function, only the correct syntax for calling it, please.  I have made the document a .docm extension.

 

With thanks in anticipation.

 

Philip

Bendigo, Vioctoria

2 Replies
Solution

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

Dear Jay

 

Thank you very much for that  insight.  I was really wandering what I had missed, as I have been using Office for many years.  It leads me on to wonder about what good uses are available, for VBA, in Word, but that is a question for another day.  My nacent Word VBA skills are not yet at a stage where I can work out how to write a macro that calls mail merge, finds a 'marker' in the document and then performs the necessary split, so, for now, I will go down the route of adding fields to the Excel source.

If anybody at Microsoft is reading this, maybe you could look into developing a system, within Word, that does provide an ability to simply call VBA functions, during a mail merge, in the way that I have suggested in my original post.     

 

Best regards

 

Philip

Bendigo, Victoria                                                                                                                         

Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
36 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies