MS Office Forum / Word / Mailmerge and Fax / November 2003
How to insert if, skipif fields in vb?
|
|
Thread rating:  |
Feng - 10 Nov 2003 18:36 GMT In my document, I want to add a macro that would insert a picture (say "c:\blah.gif") if the merge field "LName" is empty. How would I do this in vb?
Also, if I want to add a merge field, but at the same time I want to skip that field if the merge record # is divisble by 4, how would I do that?
Thanks
Cindy M -WordMVP- - 11 Nov 2003 13:35 GMT Hi =?Utf-8?B?RmVuZw==?=,
> In my document, I want to add a macro that would insert a picture (say "c:\blah.gif") if the merge field "LName" is empty. How would I do this in vb? > You're trying to generate a main merge document from scratch using VB? Or do you have the option of prooviding a mail merge document (or template) for the user to open and use? Are you already familiar with using nested field constructs in the Word user interface?
> Also, if I want to add a merge field, but at the same time I want to skip that field if the merge record # is divisble by 4, how would I do that? Merge record number as in the value of a merge field, or literally every fourth record, no matter which it might be?
And, just of the sake of completeness, which version of Word?
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.mvps.org/word
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Feng - 11 Nov 2003 14:36 GMT Hi Cindy I'm kind of familiar with inserting nested fields from VB. I have done this with an includepicture field and a merge field. However in this case, I don't even know the syntax of inserting a normal if field or a skipif field using VB. I'm currently trying to automate this on Word 97
Is there somewhere that has the syntax of how to insert all the word fields and what each field does
Thank ----- Cindy M -WordMVP- wrote: ---- Hi =?Utf-8?B?RmVuZw==?=, > In my document, I want to add a macro that would insert a picture (say "c:\blah.gif") if the merge field "LName" is empty. How would I do this in vb > You're trying to generate a main merge document from scratch using VB? Or do you have the option of prooviding a mail merge document (or template) for the user to open and use? Are you already familiar with using nested field constructs in the Word user interface > Also, if I want to add a merge field, but at the same time I want to skip that field if the merge record # is divisble by 4, how would I do that Merge record number as in the value of a merge field, or literally every fourth record, no matter which it might be And, just of the sake of completeness, which version of Word Cindy Meiste INTER-Solutions, Switzerlan http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003 http://www.mvps.org/wor This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-
Cindy M -WordMVP- - 11 Nov 2003 15:07 GMT Hi =?Utf-8?B?RmVuZw==?=,
> I'm kind of familiar with inserting nested fields from VB. I have done this with an includepicture field and a merge field. However in this case, I don't even know the syntax of inserting a normal if field or a skipif field using VB. I'm currently trying to automate this on Word 97.
> Right, I just wanted to know the extent of your background with Word/fields.
> Is there somewhere that has the syntax of how to insert all the word fields and what each field does? What each field does: look it up in Word's Help files Inserting individual fields: the .Add method of the Fields collection. You can use the .Type argument with a wdField-constant (you can get a list of these from the Object Browser in Word's Help window by searching on wdField) OR you can simply include the field's name in the TEXT argument.
Nesting fields is a trickier proposition, however, as Microsoft didn't give us any way to do this directly using VBA. Usually, I use Word's Find functionality. I insert the outermost field code first, and put some kind of place holder text where the inserted field should go. I then use Find to get hold of the placeholder and insert the nested field in its place.
Here's a sample:
Sub CreateFieldInField() Dim fld As Word.Field Dim szQuotes As String szQuotes = Chr(34) Set fld = ActiveDocument.Fields.Add(Range:=Selection.Range, _ Type:=wdFieldIf, Text:="bkm <> bkm " & szQuotes & _ "Please change the bkmAUTHORbkm setting in File/Properties" _ & szQuotes, PreserveFormatting:=False) InsertFieldInFieldCode fld.Code, "bkm", "Author" InsertFieldInFieldCode fld.Code, "bkm", "UserName" InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34" InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34" fld.Update End Sub
Function InsertFieldInFieldCode( _ ByRef rng As Word.Range, _ ByRef szBkm As String, _ ByRef szField As String, _ Optional ByRef PF As Boolean = False) As Boolean InsertFieldInCode = False With rng.Find .Text = szBkm .Execute If .Found Then ActiveDocument.Fields.Add _ Range:=rng, Text:=szField, _ PreserveFormatting:=PF InsertFieldInCode = True End If End With End Function
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.mvps.org/word
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Peter Jamieson - 11 Nov 2003 16:35 GMT I think you can also do this, but I haven't tested it much so it may break down very easily:
Sub testinsert() ' a test routine to insert the IF field that Feng needs at a specified range Dim f As String Dim s As String s = "{ IF ""{ MERGEFIELD LName }"" = """" " & _ """{ INCLUDEPICTURE ""c:\\blah.gif"" }"" " & _ """"" }" Debug.Print s Dim r As Range Set r = Selection.Range Call insertfs(s, r) Set r = Nothing
End Sub
Sub insertfs(ByVal s As String, ByVal r As Range) ' Inserts a string + fields where ' "{" means "opening field brace" ' "}" means "closing field brace" ' "?{" means "ordinary opening brace" ' "?}" means "ordinary closing brace" ' "??" means "? character" ' "?" followed by any other character is discarded
Const EscapeChar = "?" Dim i As Long Dim esc As Boolean Dim buf As String esc = False buf = "" For i = 1 To Len(s) c = Mid(s, i, 1) Select Case c Case EscapeChar: If esc Then buf = buf + c esc = False Else esc = True End If Case "{" If esc Then buf = buf + c esc = False Else r.Text = buf buf = "" r.Collapse direction:=wdCollapseEnd r.Fields.Add Range:=r, Type:=wdFieldEmpty, Text:="", preserveformatting:=False Set r = ActiveDocument.Range(r.Start + 1, r.Start + 1) r.Delete unit:=wdCharacter, Count:=2 End If Case "}" If esc Then buf = buf + c esc = False Else r.InsertAfter Text:=buf buf = "" r.Collapse direction:=wdCollapseEnd Set r = ActiveDocument.Range(r.End + 1, r.End + 1) End If Case Else buf = buf + c End Select Next r.InsertAfter Text:=buf End Sub
If you were including strings with arbitrary content including "{", "}" or "?" characters, you would need to "escape" them using e.g.
Function escapefs(s As String) As String Const EscapeChar = "?" escapefs = Replace(Replace(Replace(s, EscapeChar, EscapeChar + EscapeChar), "{", EscapeChar + "{"), "}", EscapeChar + "}") End Function
I've avoided using the usual escape characters /, \, ^ on the grounds that they are more likely to be used in the strings you want to code. Well, \ is, I suppose
-- Peter Jamieson MS Word MVP
> Hi =?Utf-8?B?RmVuZw==?=, > [quoted text clipped - 64 lines] > This reply is posted in the Newsgroup; please post any follow question or reply > in the newsgroup and not by e-mail :-) Cindy M -WordMVP- - 12 Nov 2003 17:18 GMT Hi Peter,
> I think you can also do this, but I haven't tested it much so it may break > down very easily: OK, I can see what you're doing. Interesting approach... And definitely more "intuitive" to pass along the fully built nested field combination.
I'm a bit leary of the the r.Start+1 stuff, as I've generally not had very consistent results with that. Especially when fields are involved. It can get very messy when one inadvertently inserts fields into field brackets (which is quite possible with VBA, I've discovered) <g>
It would be interesting to find out whether it's faster to loop through the characters, or to use the Find/Replace method. Looping through the characters, probably, unless there's a LOT of plain text involved.
Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.mvps.org/word
This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
Peter Jamieson - 12 Nov 2003 19:45 GMT Hi Cindy,
> I'm a bit leary of the the r.Start+1 stuff, Me too. I'd generally look out for problems related to a. the current view Word happens to be in - in fact, this definitely needs more attention in the current routine, as the behaviour of the range method does depend (sad to say) on what is actually visible in the document. b. use of the Selection object and the location of the insertion point after fields have been inserted. Certainly a while back, its location post-insertion depended partly on whether the field you had inserted was valid or not.
In fact I was a bit surprised that this example worked at all :-)
-- Peter Jamieson MS Word MVP
> Hi Peter, > [quoted text clipped - 20 lines] > This reply is posted in the Newsgroup; please post any follow question or > reply in the newsgroup and not by e-mail :-) Feng - 11 Nov 2003 16:36 GMT Thanks a lot, this really cleared up a lot of my questions. ----- Cindy M -WordMVP- wrote: ----- Hi =?Utf-8?B?RmVuZw==?=, > I'm kind of familiar with inserting nested fields from VB. I have done this with an includepicture field and a merge field. However in this case, I don't even know the syntax of inserting a normal if field or a skipif field using VB. I'm currently trying to automate this on Word 97. > Right, I just wanted to know the extent of your background with Word/fields. > Is there somewhere that has the syntax of how to insert all the word fields and what each field does? > What each field does: look it up in Word's Help files Inserting individual fields: the .Add method of the Fields collection. You can use the .Type argument with a wdField-constant (you can get a list of these from the Object Browser in Word's Help window by searching on wdField) OR you can simply include the field's name in the TEXT argument. Nesting fields is a trickier proposition, however, as Microsoft didn't give us any way to do this directly using VBA. Usually, I use Word's Find functionality. I insert the outermost field code first, and put some kind of place holder text where the inserted field should go. I then use Find to get hold of the placeholder and insert the nested field in its place. Here's a sample: Sub CreateFieldInField() Dim fld As Word.Field Dim szQuotes As String szQuotes = Chr(34) Set fld = ActiveDocument.Fields.Add(Range:=Selection.Range, _ Type:=wdFieldIf, Text:="bkm <> bkm " & szQuotes & _ "Please change the bkmAUTHORbkm setting in File/Properties" _ & szQuotes, PreserveFormatting:=False) InsertFieldInFieldCode fld.Code, "bkm", "Author" InsertFieldInFieldCode fld.Code, "bkm", "UserName" InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34" InsertFieldInFieldCode fld.Code, "bkm", "Symbol 34" fld.Update End Sub Function InsertFieldInFieldCode( _ ByRef rng As Word.Range, _ ByRef szBkm As String, _ ByRef szField As String, _ Optional ByRef PF As Boolean = False) As Boolean InsertFieldInCode = False With rng.Find .Text = szBkm .Execute If .Found Then ActiveDocument.Fields.Add _ Range:=rng, Text:=szField, _ PreserveFormatting:=PF InsertFieldInCode = True End If End With End Function Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003) http://www.mvps.org/word This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-)
|
|
|