Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) MailMerge Fetching Info (Read 2948 times)
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
MailMerge Fetching Info
Oct 22nd, 2010 at 5:34am
Print Post Print Post  
Hey all,

I'm struggling with the following issue I hope someone can help me with.

I am using WordMerge to build a form letter for either the current record or all retrieved records.  It goes and pulls LEList.txt which lists which elements in the current form are to be used.  It then goes through the loop of building the DataMerge.txt file.

If one of the fields is BW, for example, how can I go and retrieve from my Staff form that I want the letter to show "Blair Wettlaufer", "ext 681", and "bwettlaufer@gmail.com", or do I need to create some extra fields in my current form?

SAMPLE CODE BELOW
Code
Select All
var vNames as string		// Form LE names from vNamesPath text file


// PRINT CURRENT RECORD ONLY

vGoPrint = "Yes"

For n = 1 to @CountStringArray(vNames)
	vHeader = vHeader + @AccessStringArray(vNames,n) + "^"
        vData  = vData + @(@AccessStringArray(vNames,n)) + "^"
	WriteLn(vData)
Next

// PRINT ALL RETRIEVED RECORDS

SUBROUTINE PrintAllRetrievedRecords()

{

	vGoPrint = "Yes"

	For n = 1 to @CountStringArray(vNames)
		vHeader += @AccessStringArray(vNames,n) + "^"
	Next

	vFileHandle = fileOpen(vDataPath)
	fileSeek(vFileHandle, 0)
	fileWriteLn(vFileHandle, vHeader)

	For n = 1 to @ResultSetTotal()
		ResultSetCurrentPosition(n)

		For i = 1 to @CountStringArray(vNames)
			vData += @(@AccessStringArray(vNames,i)) + "^"
} 

  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: MailMerge Fetching Info
Reply #1 - Oct 22nd, 2010 at 12:10pm
Print Post Print Post  
Use a conditional to write custom behavior for certain element names. I'm not where I can check my syntax and I don't have your databases to get the right names of everything, but it would go something like this:

Code
Select All
// Other var declarations as needed
var vName as String
var vVals as String

For n = 1 to @CountStringArray(vNames)
	vName = @AccessStringArray(vNames, n)
	If vName = "Initials"
	{
		vHeader = vHeader + "FullName^Ext^Email^"
		vVals = @XLookupSourceList(@FN, Initials, "Users!Initials", "FullName;Ext;Email")
		If vVals <> ""
		{
			vData = vData + @AccessStringArray(vVals, 1) + "^"
				 + @AccessStringArray(vVals, 2) + "^"
				 + @AccessStringArray(vVals, 3) + "^"
		}
		Else
		{
			vData = vData + "^^^"
		}
	}
	Else
	{
		vHeader = vHeader + vName + "^"
        	vData  = vData + @(vName) + "^"
	}
	WriteLn(vData)
Next

 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #2 - Oct 22nd, 2010 at 9:40pm
Print Post Print Post  
Thanks!  That should be enough to get me on the right track ... I'll let you know how it goes!

Blair
  
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #3 - Dec 24th, 2010 at 1:38am
Print Post Print Post  
Well, I've been struggling with this, and it hasn't worked.  Gr!!

Either the LEList text file in the wordmrg directory bounces me because the field doesn't exist on the form, or it just ignores my request.

The form I'm trying to pull data from is called Staff, and I want StaffName, StaffTitle, and StaffEmail to be pulled when StaffID in that form matches DBStaff in my current form.

Here's the full code of my creaky old mail merge button's programming.

Code
Select All
// This is pasted from Inside Sesame 2005-04 & Modified - 2005-06-08 BW

var vFileHandle as Int	    // For writing merge data source file for Word
var vHeader as String	     // Data source file header line
var vData as String		 // Data source file data line
var n as Int			// General purpose counter var
var vSelectedDoc as String	// The selected Word doc
var vOneOrAll as String	   // Include record(s) option
var vGoPrint as String		// Printing flag
var vNames as string		// Form LE names from vNamesPath text file
var vStartingRec = @ResultSetCurrentPosition()
var vOK as Int 			// added var for new @Shell to do a DOS Dir, 11 Jan 2005
var vLttrInfo as string	  // letter data for DBLttrLog0 field
var vLttr as string		// selected document name without the .doc ext

Var vNewNoteNo as String
var vNewNote as String
var vCount as Int
var vNewFileID as Int

var vLEs as String = @StringArrayElementList()  // All LE's on current form

var vDocList as string		// The document list converted to stringarray if nec.

var vDataPath as String  = "C:\Sesame\WordMrg\DataMergeDB.txt"
var vNamesPath as String = "C:\Sesame\WordMrg\LEListDB.txt"
var vDocsPath as String = "C:\Sesame\WordMrg\"
var vDocListPath as String = "C:\Sesame\WordMrg\DocListDB.txt"


// Command Line for Directory command  // this does not need to be modified.

var vDir as string = "dir /b /o:n " + vDocsPath + "*.doc >" + vDocListPath

SUBROUTINE PrintCurrentRecordOnly()

var n as Int

// THIS MSGBOX NOT NECESSARY FOR PRINTING JUST ONE RECORD.
// If @Askuser("Merge print the " + vSelectedDoc + " for "
// + vOneOrAll + "?","","") Then

// {

vGoPrint = "Yes"

For n = 1 to @CountStringArray(vNames)
	vHeader = vHeader + @AccessStringArray(vNames,n) + "^"
	  vData  = vData + @(@AccessStringArray(vNames,n)) + "^"
	WriteLn(vData)
Next

// This next line gets rid of embedded carriage returns in the data fields
//  This is necessary otherwise the merge will fail
	vData   = @Replace(vData, @Newline(), "  ")

vFileHandle = fileOpen(vDataPath)
fileSeek(vFileHandle, 0)
fileWriteLn(vFileHandle, vHeader + @NewLine() + vData)
fileClose(vFileHandle)

// }   - COMMENTED OUT WITH THE CONFIRM @MSGBOX (NOT NEC FOR 1 RECORD)

END SUBROUTINE

SUBROUTINE PrintAllRetrievedRecords()

var n as Int
var i as Int

If @Askuser("Merge Print " + vSelectedDoc + " for these " + @ResultSetTotal() + " retrieved records?",
"( If not, click on NO to cancel this task, press F7 for a new search,"," and retrieve the records you DO want to print. )")
Then
{

	vGoPrint = "Yes"

	For n = 1 to @CountStringArray(vNames)
		vHeader += @AccessStringArray(vNames,n) + "^"
	Next

	vFileHandle = fileOpen(vDataPath)
	fileSeek(vFileHandle, 0)
	fileWriteLn(vFileHandle, vHeader)

	For n = 1 to @ResultSetTotal()
		ResultSetCurrentPosition(n)

		For i = 1 to @CountStringArray(vNames)
			vData += @(@AccessStringArray(vNames,i)) + "^"

// This next line gets rid of embedded carriage returns in the data fields
//  This is necessary otherwise the merge will fail
		    vData  = @Replace(vData, @Newline(), "  ")

		Next

		FileWriteLn(vFileHandle, vData)

		@Msg("Processed Record " + n + " of " + @ResultSetTotal())
		vData = ""

	Next

	fileClose(vFileHandle)

	// Rewind to record where it all started
	ResultSetCurrentPosition(vStartingRec)
}

END SUBROUTINE

/*
// THESE lines are commented out,
//  but can be activated if you need to DEBUG.

WriteLn("vDataPath = " + vDataPath)
WriteLn("vNamesPath = " + vNamesPath)
WriteLn("vDocsPath = " + vDocsPath)
WriteLn("vDocListPath = " + vDocListPath)
WriteLn("vDir = " + vDir)
*/


// BEGIN MAIN PROGRAM

// Get the list of specified LE's on this form (for the data source file).
// Turn it into String Array by replacing C/R's with semicolons

vNames = @Insert(vNamesPath)
vNames = @Replace(vNames, @NewLine(), ";")

// If file missing or empty, bring everything to grinding halt.

If vNames = "" Then
@Msgbox(vNamesPath + " file missing!","LE Names for merge file unavailable.","Can't proceed.")
Else
{

// Added by TJM 8 Jan 05 to report any LE names in
// vNames that are not represented on current form

For n = 1 to @CountStringArray(vNames)

	If @Instr(vLEs, @AccessStringArray(vNames, n)) = 0 Then
	{
	WriteLn("LE name '" + @AccessStringArray(vNames, n) + "' not in current form.")
	WriteLn("(Ref. File: " + vNamesPath + ")" + @NewLine())
	}

Next

// Build Word doc selection menu

// Set position of picklist to the left of and above the command button
PopupSelectPosition(4,@Xpos(ThisElement)-125,@Ypos(ThisElement)-50)

// generate current list of docs
vOK=@shell(vDir)

// Convert the document list from one-per-line to semi-colon separated if nec
vDocList = @insert(vDocListPath)
vDocList = @Replace(vDocList, @NewLine(), ";")

vSelectedDoc = @PopupMenu(vDocList, "SELECT MERGE DOCUMENT" )

If vSelectedDoc <> "" Then

{

// Build which record(s) menu
// Prevents Users from printing batch letters -- 2005/10/31 BW

vOneOrAll = @PopupMenu("THIS Record Only;ALL Retrieved Records","SELECT RECORD(S) TO MERGE PRINT")

if vOneOrAll = "All Retrieved Records" and @group = "Users" then
{
	@msgbox("You do not have access to batch letter printing!","","")
	vOneOrAll = ""
}

// Reset Popup position
PopupSelectPosition(0,99,99)

If vSelectedDoc <> "" and vOneOrAll <> "" Then
{

	// Delete exsting data file. New one will be generated.
	If FileExists(vDocsPath + vSelectedDoc) Then
	{
	fileDelete(vDataPath)

	If vOneOrAll = "THIS Record Only" Then
		PrintCurrentRecordOnly()
	Else
		PrintAllRetrievedRecords()

	If vGoPrint = "Yes" Then
//	n = @Shell("START Winword /mMerge_to_New_Doc " + @Chr(34) + vDocsPath + vselectedDoc + @Chr(34))

	n = @ASynchShell(vDocsPath + vselectedDoc)
	}

	Else
	@Msgbox(vDocsPath + vSelectedDoc + " doesn't exist.","","Aw shucks!")
}
}
}

// Log Letter sent

if vOneOrAll = "THIS Record Only" then
{
	vLttr = @replace(vSelectedDoc,".doc","")
	vLttrInfo = @Serverdate() + " -- " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
	if @Len(VLttrInfo) >60 then vLttrInfo = @left(vLttrInfo,60) + " ... "
	DBLttrLog0 = vLttrInfo + @NewLine() + DBLttrLog0

	DBNote0 = "Sent " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
}

vCount = @FormResultSetTotal("Note Data")

If DBNote0 <> "" then
{	If vCount = 1 Then
	{	If @FormFieldValue("Note Data", "Note", 1) <> "" Then
		{	vNewNoteNo = @FormNewRecord("Note Data")
		}
		Else
		{	vNewNoteNo = 1
		}
	}
	Else
	{	vNewNoteNo = @FormNewRecord("Note Data")
	}
}
Else
{	If DBNote0 = "" THEN @MsgBox ("", "You Cannot Enter a Blank Note!","")
}

gFileID = @ToNumber(@GlobalValue("gFileID")) +1
vNewFileID = gFileID
GlobalValue("gFileID", vNewFileID)

FormFieldValue("Note Data","NoteRef",vNewNoteNo,DBID)
FormFieldValue("Note Data","NoteDate",vNewNoteNo,@ServerDate())
FormFieldValue("Note Data","NoteTime",vNewNoteNo,@ServerTime())
FormFieldValue("Note Data","Note",vNewNoteNo,DBNote0)
FormFieldValue("Note Data","NoteID",vNewNoteNo,vNewFileID)
FormFieldValue("Note Data","NoteSig",vNewNoteNo,@UserID)

vNewNote = @FormFieldValue("Note Data","Note",vNewNoteNo)

ForceRedraw()

gPosted = 1

DBNote0 = ""
 

  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: MailMerge Fetching Info
Reply #4 - Dec 24th, 2010 at 2:48pm
Print Post Print Post  
Hello Blair,

Try:

Code
Select All
// This is pasted from Inside Sesame 2005-04 & Modified - 2005-06-08 BW

var vFileHandle as Int	    // For writing merge data source file for Word
var vHeader as String	     // Data source file header line
var vData as String		 // Data source file data line
var n as Int			// General purpose counter var
var vSelectedDoc as String	// The selected Word doc
var vOneOrAll as String	   // Include record(s) option
var vGoPrint as String		// Printing flag
var vNames as string		// Form LE names from vNamesPath text file
var vStartingRec = @ResultSetCurrentPosition()
var vOK as Int 			// added var for new @Shell to do a DOS Dir, 11 Jan 2005
var vLttrInfo as string	  // letter data for DBLttrLog0 field
var vLttr as string		// selected document name without the .doc ext

Var vNewNoteNo as String
var vNewNote as String
var vCount as Int
var vNewFileID as Int

var vLEs as String = @StringArrayElementList()  // All LE's on current form

var vDocList as string		// The document list converted to stringarray if nec.

var vDataPath as String  = "C:\Sesame\WordMrg\DataMergeDB.txt"
var vNamesPath as String = "C:\Sesame\WordMrg\LEListDB.txt"
var vDocsPath as String = "C:\Sesame\WordMrg\"    
var vDocListPath as String = "C:\Sesame\WordMrg\DocListDB.txt"

// Command Line for Directory command  // this does not need to be modified.

var vDir as string = "dir /b /o:n " + vDocsPath + "*.doc >" + vDocListPath

//RY ADDED
Var vStaffLEs as String = "StaffName;StaffTitle;StaffEmail"

Function AddStaffInfo(vDBStaffID as String) as String
Var vVals as String
Var vReturn as String

	vVals = @XLookupSourceList(@FN, vDBStaffID, "Staff!StaffID", vStaffLEs)
	If vVals <> ""
	{
		vReturn = vReturn + @AccessStringArray(vVals, 1) + "^"
			 + @AccessStringArray(vVals, 2) + "^"
			 + @AccessStringArray(vVals, 3) + "^"
	}
	Else
	{
		vReturn = vReturn + "^^^"
	}
	Return(vReturn)

End Function

SUBROUTINE PrintCurrentRecordOnly()

var n as Int
Var i as Int
Var vLEName as String

vGoPrint = "Yes"

For n = 1 to @CountStringArray(vNames)
	vLEName = @AccessStringArray(vNames,n)
	vHeader = vHeader + vLEName + "^"
	vData  = vData + @(vLEName) + "^"
	If vLEName = "DBStaff" Then
	{
		For i = 1 to @CountStringArray(vStaffLEs)
			vHeader += @AccessStringArray(vStaffLEs, i) + "^"
		Next
		vData = vData + AddStaffInfo(@(vLEName))
	}
Next

vData   = @Replace(vData, @Newline(), "  ")

vFileHandle = fileOpen(vDataPath)
fileSeek(vFileHandle, 0)
fileWriteLn(vFileHandle, vHeader + @NewLine() + vData)
fileClose(vFileHandle)

END SUBROUTINE

SUBROUTINE PrintAllRetrievedRecords()

var n as Int
var i as Int
Var vLEName as String

If @Askuser("Merge Print " + vSelectedDoc + " for these " + @ResultSetTotal() + " retrieved records?",
"( If not, click on NO to cancel this task, press F7 for a new search,"," and retrieve the records you DO want to print. )")
Then
{

	vGoPrint = "Yes"

	For n = 1 to @CountStringArray(vNames)
		vHeader += @AccessStringArray(vNames,n) + "^"
	Next

	For n = 1 to @CountStringArray(vStaffLEs)
		vHeader += @AccessStringArray(vStaffLEs,n) + "^"
	Next

	vFileHandle = fileOpen(vDataPath)
	fileSeek(vFileHandle, 0)
	fileWriteLn(vFileHandle, vHeader)

	For n = 1 to @ResultSetTotal()
		ResultSetCurrentPosition(n)

		For i = 1 to @CountStringArray(vNames)
			vLEName = @AccessStringArray(vNames,i)
			vData += @(vLEName) + "^"
			If vLEName = "DBStaff" Then
			{
				For i = 1 to @CountStringArray(vStaffLEs)
					vHeader += @AccessStringArray(vStaffLEs, i) + "^"
				Next
				vData = vData + AddStaffInfo(@(vLEName))
			}

		Next

		vData  = @Replace(vData, @Newline(), "  ")
		FileWriteLn(vFileHandle, vData)

		@Msg("Processed Record " + n + " of " + @ResultSetTotal())
		vData = ""

	Next

	fileClose(vFileHandle)

	// Rewind to record where it all started
	ResultSetCurrentPosition(vStartingRec)
}

END SUBROUTINE

/*
// THESE lines are commented out,
//  but can be activated if you need to DEBUG.

WriteLn("vDataPath = " + vDataPath)
WriteLn("vNamesPath = " + vNamesPath)
WriteLn("vDocsPath = " + vDocsPath)
WriteLn("vDocListPath = " + vDocListPath)
WriteLn("vDir = " + vDir)
*/


// BEGIN MAIN PROGRAM

// Get the list of specified LE's on this form (for the data source file).
// Turn it into String Array by replacing C/R's with semicolons

vNames = @Insert(vNamesPath)
vNames = @Replace(vNames, @NewLine(), ";")

// If file missing or empty, bring everything to grinding halt.

If vNames = "" Then
@Msgbox(vNamesPath + " file missing!","LE Names for merge file unavailable.","Can't proceed.")
Else
{

// Added by TJM 8 Jan 05 to report any LE names in
// vNames that are not represented on current form

For n = 1 to @CountStringArray(vNames)

	If @Instr(vLEs, @AccessStringArray(vNames, n)) = 0 Then
	{
	WriteLn("LE name '" + @AccessStringArray(vNames, n) + "' not in current form.")
	WriteLn("(Ref. File: " + vNamesPath + ")" + @NewLine())
	}

Next

// Build Word doc selection menu

// Set position of picklist to the left of and above the command button
PopupSelectPosition(4,@Xpos(ThisElement)-125,@Ypos(ThisElement)-50)

// generate current list of docs
vOK=@shell(vDir)

// Convert the document list from one-per-line to semi-colon separated if nec
vDocList = @insert(vDocListPath)
vDocList = @Replace(vDocList, @NewLine(), ";")

vSelectedDoc = @PopupMenu(vDocList, "SELECT MERGE DOCUMENT" )

If vSelectedDoc <> "" Then

{

// Build which record(s) menu
// Prevents Users from printing batch letters -- 2005/10/31 BW

vOneOrAll = @PopupMenu("THIS Record Only;ALL Retrieved Records","SELECT RECORD(S) TO MERGE PRINT")

if vOneOrAll = "All Retrieved Records" and @group = "Users" then
{
	@msgbox("You do not have access to batch letter printing!","","")
	vOneOrAll = ""
}

// Reset Popup position
PopupSelectPosition(0,99,99)

If vSelectedDoc <> "" and vOneOrAll <> "" Then
{

	// Delete exsting data file. New one will be generated.
	If FileExists(vDocsPath + vSelectedDoc) Then
	{
	fileDelete(vDataPath)

	If vOneOrAll = "THIS Record Only" Then
		PrintCurrentRecordOnly()
	Else
		PrintAllRetrievedRecords()

	If vGoPrint = "Yes" Then
//	n = @Shell("START Winword /mMerge_to_New_Doc " + @Chr(34) + vDocsPath + vselectedDoc + @Chr(34))

	n = @ASynchShell(vDocsPath + vselectedDoc)
	}

	Else
	@Msgbox(vDocsPath + vSelectedDoc + " doesn't exist.","","Aw shucks!")
}
}
}

// Log Letter sent

if vOneOrAll = "THIS Record Only" then
{
	vLttr = @replace(vSelectedDoc,".doc","")
	vLttrInfo = @Serverdate() + " -- " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
	if @Len(VLttrInfo) >60 then vLttrInfo = @left(vLttrInfo,60) + " ... "
	DBLttrLog0 = vLttrInfo + @NewLine() + DBLttrLog0

	DBNote0 = "Sent " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
}

vCount = @FormResultSetTotal("Note Data")

If DBNote0 <> "" then
{	If vCount = 1 Then
	{	If @FormFieldValue("Note Data", "Note", 1) <> "" Then
		{	vNewNoteNo = @FormNewRecord("Note Data")
		}
		Else
		{	vNewNoteNo = 1
		}
	}
	Else
	{	vNewNoteNo = @FormNewRecord("Note Data")
	}
}
Else
{	If DBNote0 = "" THEN @MsgBox ("", "You Cannot Enter a Blank Note!","")
}

gFileID = @ToNumber(@GlobalValue("gFileID")) +1
vNewFileID = gFileID
GlobalValue("gFileID", vNewFileID)

FormFieldValue("Note Data","NoteRef",vNewNoteNo,DBID)
FormFieldValue("Note Data","NoteDate",vNewNoteNo,@ServerDate())
FormFieldValue("Note Data","NoteTime",vNewNoteNo,@ServerTime())
FormFieldValue("Note Data","Note",vNewNoteNo,DBNote0)
FormFieldValue("Note Data","NoteID",vNewNoteNo,vNewFileID)
FormFieldValue("Note Data","NoteSig",vNewNoteNo,@UserID)

vNewNote = @FormFieldValue("Note Data","Note",vNewNoteNo)

ForceRedraw()

gPosted = 1

DBNote0 = "" 



You will need DBStaff in your LEListDB.txt but do not put your Staff elements in there. Let me know if this works for you.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #5 - Dec 24th, 2010 at 3:05pm
Print Post Print Post  
Wow .. thanks!  I'll give it a go over Xmas.

Wish everyone there Merry Christmas for me!

Blair
  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: MailMerge Fetching Info
Reply #6 - Dec 24th, 2010 at 3:08pm
Print Post Print Post  
Will do. And a Merry Christmas to you too Blair.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #7 - Jan 5th, 2011 at 12:36am
Print Post Print Post  
Hmmm ... didn't work ...

I first ran it, and nothing changed.  Then I noticed the code required if vLEName = "DBStaff", which is actually "DBColl#", and when I changed that, the headers started showing up, but it didn't populate from the record.  So there must be something wrong with the lookup subroutine?

I tried fiddling a bit, but I can't get it to populate the record ...

Here's what my DataMergeDB.txt file looks like ...
DBID^DBName^DBAdd1^DBAdd2^DBCity^DBProv^DBPCod^DBCnt^DBList^DBRef#^DBColl#^Staff
Name^StaffTitle^StaffEmail^
326023.000000^ABC Company^123 Test Drive #7^^Hamilton^ON^L5T 1K4^Fred^525.000000^290310-3^BW^^^^



Code
Select All
// This is pasted from Inside Sesame 2005-04 & Modified - 2005-06-08 BW

var vFileHandle as Int		   // For writing merge data source file for Word
var vHeader as String		   // Data source file header line
var vData as String		 // Data source file data line
var n as Int		 	 // General purpose counter var
var vSelectedDoc as String 	 // The selected Word doc
var vOneOrAll as String		   // Include record(s) option
var vGoPrint as String		 // Printing flag
var vNames as string		 // Form LE names from vNamesPath text file
var vStartingRec = @ResultSetCurrentPosition()
var vOK as Int 			 // added var for new @Shell to do a DOS Dir, 11 Jan 2005
var vLttrInfo as string		   // letter data for DBLttrLog0 field
var vLttr as string		 // selected document name without the .doc ext

Var vNewNoteNo as String
var vNewNote as String
var vCount as Int
var vNewFileID as Int

var vLEs as String = @StringArrayElementList()  // All LE's on current form

var vDocList as string		// The document list converted to stringarray if nec.

var vDataPath as String  = "C:\Sesame\WordMrg\DataMergeDB.txt"
var vNamesPath as String = "C:\Sesame\WordMrg\LEListDB.txt"
var vDocsPath as String = "C:\Sesame\WordMrg\"
var vDocListPath as String = "C:\Sesame\WordMrg\DocListDB.txt"

// Command Line for Directory command  // this does not need to be modified.

var vDir as string = "dir /b /o:n " + vDocsPath + "*.doc >" + vDocListPath

// Ray ADDED 2010-12-24

Var vStaffLEs as String = "StaffName;StaffTitle;StaffEmail"

Function AddStaffInfo(vDBStaffID as String) as String
Var vVals as String
Var vReturn as String

	vVals = @XLookupSourceList(@FN, vDBStaffID, "Staff!StaffRef", vStaffLEs)
	If vVals <> ""
	{
		vReturn = vReturn + @AccessStringArray(vVals, 1) + "^"
			 + @AccessStringArray(vVals, 2) + "^"
			 + @AccessStringArray(vVals, 3) + "^"
	}
	Else
	{
		vReturn = vReturn + "^^^"
	}
	Return(vReturn)

End Function

SUBROUTINE PrintCurrentRecordOnly()

var n as Int
Var i as Int
Var vLEName as String

vGoPrint = "Yes"

For n = 1 to @CountStringArray(vNames)
	vLEName = @AccessStringArray(vNames,n)
	vHeader = vHeader + vLEName + "^"
	vData  = vData + @(vLEName) + "^"
	If vLEName = "DBColl#" Then
	{
		For i = 1 to @CountStringArray(vStaffLEs)
			vHeader += @AccessStringArray(vStaffLEs, i) + "^"
		Next
		vData = vData + AddStaffInfo(@(vLEName))
	}
Next

vData   = @Replace(vData, @Newline(), "  ")

vFileHandle = fileOpen(vDataPath)
fileSeek(vFileHandle, 0)
fileWriteLn(vFileHandle, vHeader + @NewLine() + vData)
fileClose(vFileHandle)

END SUBROUTINE

SUBROUTINE PrintAllRetrievedRecords()

var n as Int
var i as Int
Var vLEName as String

If @Askuser("Merge Print " + vSelectedDoc + " for these " + @ResultSetTotal() + " retrieved records?",
"( If not, click on NO to cancel this task, press F7 for a new search,"," and retrieve the records you DO want to print. )")
Then
{

	vGoPrint = "Yes"

	For n = 1 to @CountStringArray(vNames)
		vHeader += @AccessStringArray(vNames,n) + "^"
	Next

	For n = 1 to @CountStringArray(vStaffLEs)
		vHeader += @AccessStringArray(vStaffLEs,n) + "^"
	Next

	vFileHandle = fileOpen(vDataPath)
	fileSeek(vFileHandle, 0)
	fileWriteLn(vFileHandle, vHeader)

	For n = 1 to @ResultSetTotal()
		ResultSetCurrentPosition(n)

		For i = 1 to @CountStringArray(vNames)
			vLEName = @AccessStringArray(vNames,i)
			vData += @(vLEName) + "^"
			If vLEName = "DBColl#" Then
			{
				For i = 1 to @CountStringArray(vStaffLEs)
					vHeader += @AccessStringArray(vStaffLEs, i) + "^"
				Next
				vData = vData + AddStaffInfo(@(vLEName))
			}

		Next

		vData  = @Replace(vData, @Newline(), "  ")
		FileWriteLn(vFileHandle, vData)

		@Msg("Processed Record " + n + " of " + @ResultSetTotal())
		vData = ""

	Next

	fileClose(vFileHandle)

	// Rewind to record where it all started
	ResultSetCurrentPosition(vStartingRec)
}

END SUBROUTINE

/*
// THESE lines are commented out,
//  but can be activated if you need to DEBUG.

WriteLn("vDataPath = " + vDataPath)
WriteLn("vNamesPath = " + vNamesPath)
WriteLn("vDocsPath = " + vDocsPath)
WriteLn("vDocListPath = " + vDocListPath)
WriteLn("vDir = " + vDir)
*/


// BEGIN MAIN PROGRAM

// Get the list of specified LE's on this form (for the data source file).
// Turn it into String Array by replacing C/R's with semicolons

vNames = @Insert(vNamesPath)
vNames = @Replace(vNames, @NewLine(), ";")

// If file missing or empty, bring everything to grinding halt.

If vNames = "" Then
@Msgbox(vNamesPath + " file missing!","LE Names for merge file unavailable.","Can't proceed.")
Else
{

// Added by TJM 8 Jan 05 to report any LE names in
// vNames that are not represented on current form

For n = 1 to @CountStringArray(vNames)

	If @Instr(vLEs, @AccessStringArray(vNames, n)) = 0 Then
	{
	WriteLn("LE name '" + @AccessStringArray(vNames, n) + "' not in current form.")
	WriteLn("(Ref. File: " + vNamesPath + ")" + @NewLine())
	}

Next

// Build Word doc selection menu

// Set position of picklist to the left of and above the command button
PopupSelectPosition(4,@Xpos(ThisElement)-125,@Ypos(ThisElement)-50)

// generate current list of docs
vOK=@shell(vDir)

// Convert the document list from one-per-line to semi-colon separated if nec
vDocList = @insert(vDocListPath)
vDocList = @Replace(vDocList, @NewLine(), ";")

vSelectedDoc = @PopupMenu(vDocList, "SELECT MERGE DOCUMENT" )

If vSelectedDoc <> "" Then

{

// Build which record(s) menu
// Prevents Users from printing batch letters -- 2005/10/31 BW

vOneOrAll = @PopupMenu("THIS Record Only;ALL Retrieved Records","SELECT RECORD(S) TO MERGE PRINT")

if vOneOrAll = "All Retrieved Records" and @group = "Users" then
{
	@msgbox("You do not have access to batch letter printing!","","")
	vOneOrAll = ""
}

// Reset Popup position
PopupSelectPosition(0,99,99)

If vSelectedDoc <> "" and vOneOrAll <> "" Then
{

	// Delete exsting data file. New one will be generated.
	If FileExists(vDocsPath + vSelectedDoc) Then
	{
	fileDelete(vDataPath)

	If vOneOrAll = "THIS Record Only" Then
		PrintCurrentRecordOnly()
	Else
		PrintAllRetrievedRecords()

	If vGoPrint = "Yes" Then
//	n = @Shell("START Winword /mMerge_to_New_Doc " + @Chr(34) + vDocsPath + vselectedDoc + @Chr(34))

	n = @ASynchShell(vDocsPath + vselectedDoc)
	}

	Else
	@Msgbox(vDocsPath + vSelectedDoc + " doesn't exist.","","Aw shucks!")
}
}
}

// Log Letter sent

if vOneOrAll = "THIS Record Only" then
{
	vLttr = @replace(vSelectedDoc,".doc","")
	vLttrInfo = @Serverdate() + " -- " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
	if @Len(VLttrInfo) >60 then vLttrInfo = @left(vLttrInfo,60) + " ... "
	DBLttrLog0 = vLttrInfo + @NewLine() + DBLttrLog0

	DBNote0 = "Sent " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
}

vCount = @FormResultSetTotal("Note Data")

If DBNote0 <> "" then
{	If vCount = 1 Then
	{	If @FormFieldValue("Note Data", "Note", 1) <> "" Then
		{	vNewNoteNo = @FormNewRecord("Note Data")
		}
		Else
		{	vNewNoteNo = 1
		}
	}
	Else
	{	vNewNoteNo = @FormNewRecord("Note Data")
	}
}
Else
{	If DBNote0 = "" THEN @MsgBox ("", "You Cannot Enter a Blank Note!","")
}

gFileID = @ToNumber(@GlobalValue("gFileID")) +1
vNewFileID = gFileID
GlobalValue("gFileID", vNewFileID)

FormFieldValue("Note Data","NoteRef",vNewNoteNo,DBID)
FormFieldValue("Note Data","NoteDate",vNewNoteNo,@ServerDate())
FormFieldValue("Note Data","NoteTime",vNewNoteNo,@ServerTime())
FormFieldValue("Note Data","Note",vNewNoteNo,DBNote0)
FormFieldValue("Note Data","NoteID",vNewNoteNo,vNewFileID)
FormFieldValue("Note Data","NoteSig",vNewNoteNo,@UserID)

vNewNote = @FormFieldValue("Note Data","Note",vNewNoteNo)

ForceRedraw()

gPosted = 1

DBNote0 = ""
 

  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: MailMerge Fetching Info
Reply #8 - Jan 5th, 2011 at 6:47pm
Print Post Print Post  
Hello Blair,

Try the following and let me know the output.

Code
Select All
// This is pasted from Inside Sesame 2005-04 & Modified - 2005-06-08 BW

var vFileHandle as Int		   // For writing merge data source file for Word
var vHeader as String		   // Data source file header line
var vData as String		 // Data source file data line
var n as Int		 	 // General purpose counter var
var vSelectedDoc as String 	 // The selected Word doc
var vOneOrAll as String		   // Include record(s) option
var vGoPrint as String		 // Printing flag
var vNames as string		 // Form LE names from vNamesPath text file
var vStartingRec = @ResultSetCurrentPosition()
var vOK as Int 			 // added var for new @Shell to do a DOS Dir, 11 Jan 2005
var vLttrInfo as string		   // letter data for DBLttrLog0 field
var vLttr as string		 // selected document name without the .doc ext

Var vNewNoteNo as String
var vNewNote as String
var vCount as Int
var vNewFileID as Int

var vLEs as String = @StringArrayElementList()  // All LE's on current form

var vDocList as string		// The document list converted to stringarray if nec.

var vDataPath as String  = "C:\Sesame\WordMrg\DataMergeDB.txt"
var vNamesPath as String = "C:\Sesame\WordMrg\LEListDB.txt"
var vDocsPath as String = "C:\Sesame\WordMrg\"
var vDocListPath as String = "C:\Sesame\WordMrg\DocListDB.txt"

// Command Line for Directory command  // this does not need to be modified.

var vDir as string = "dir /b /o:n " + vDocsPath + "*.doc >" + vDocListPath

// Ray ADDED 2010-12-24

Var vStaffLEs as String = "StaffName;StaffTitle;StaffEmail"

Function AddStaffInfo(vDBStaffID as String) as String
Var vVals as String
Var vReturn as String

Writeln("Adding Staff Info for ID: " + vDBStaffID)
	vVals = @XLookupSourceList(@FN, vDBStaffID, "Staff!StaffRef", vStaffLEs)
Writeln(@Error)
Writeln("XLookupSourceList returned: " + vVals)
	If vVals <> ""
	{
		vReturn = vReturn + @AccessStringArray(vVals, 1) + "^"
			 + @AccessStringArray(vVals, 2) + "^"
			 + @AccessStringArray(vVals, 3) + "^"
	}
	Else
	{
		vReturn = vReturn + "^^^"
	}
	Return(vReturn)

End Function

SUBROUTINE PrintCurrentRecordOnly()

var n as Int
Var i as Int
Var vLEName as String

vGoPrint = "Yes"

For n = 1 to @CountStringArray(vNames)
	vLEName = @AccessStringArray(vNames,n)
	vHeader = vHeader + vLEName + "^"
	vData  = vData + @(vLEName) + "^"
	If vLEName = "DBColl#" Then
	{
		For i = 1 to @CountStringArray(vStaffLEs)
			vHeader += @AccessStringArray(vStaffLEs, i) + "^"
		Next
		vData = vData + AddStaffInfo(@(vLEName))
	}
Next

vData   = @Replace(vData, @Newline(), "  ")

vFileHandle = fileOpen(vDataPath)
fileSeek(vFileHandle, 0)
fileWriteLn(vFileHandle, vHeader + @NewLine() + vData)
fileClose(vFileHandle)

END SUBROUTINE

SUBROUTINE PrintAllRetrievedRecords()

var n as Int
var i as Int
Var vLEName as String

If @Askuser("Merge Print " + vSelectedDoc + " for these " + @ResultSetTotal() + " retrieved records?",
"( If not, click on NO to cancel this task, press F7 for a new search,"," and retrieve the records you DO want to print. )")
Then
{

	vGoPrint = "Yes"

	For n = 1 to @CountStringArray(vNames)
		vHeader += @AccessStringArray(vNames,n) + "^"
	Next

	For n = 1 to @CountStringArray(vStaffLEs)
		vHeader += @AccessStringArray(vStaffLEs,n) + "^"
	Next

	vFileHandle = fileOpen(vDataPath)
	fileSeek(vFileHandle, 0)
	fileWriteLn(vFileHandle, vHeader)

	For n = 1 to @ResultSetTotal()
		ResultSetCurrentPosition(n)

		For i = 1 to @CountStringArray(vNames)
			vLEName = @AccessStringArray(vNames,i)
			vData += @(vLEName) + "^"
			If vLEName = "DBColl#" Then
			{
				For i = 1 to @CountStringArray(vStaffLEs)
					vHeader += @AccessStringArray(vStaffLEs, i) + "^"
				Next
				vData = vData + AddStaffInfo(@(vLEName))
			}

		Next

		vData  = @Replace(vData, @Newline(), "  ")
		FileWriteLn(vFileHandle, vData)

		@Msg("Processed Record " + n + " of " + @ResultSetTotal())
		vData = ""

	Next

	fileClose(vFileHandle)

	// Rewind to record where it all started
	ResultSetCurrentPosition(vStartingRec)
}

END SUBROUTINE

/*
// THESE lines are commented out,
//  but can be activated if you need to DEBUG.

WriteLn("vDataPath = " + vDataPath)
WriteLn("vNamesPath = " + vNamesPath)
WriteLn("vDocsPath = " + vDocsPath)
WriteLn("vDocListPath = " + vDocListPath)
WriteLn("vDir = " + vDir)
*/


// BEGIN MAIN PROGRAM

// Get the list of specified LE's on this form (for the data source file).
// Turn it into String Array by replacing C/R's with semicolons

vNames = @Insert(vNamesPath)
vNames = @Replace(vNames, @NewLine(), ";")

// If file missing or empty, bring everything to grinding halt.

If vNames = "" Then
@Msgbox(vNamesPath + " file missing!","LE Names for merge file unavailable.","Can't proceed.")
Else
{

// Added by TJM 8 Jan 05 to report any LE names in
// vNames that are not represented on current form

For n = 1 to @CountStringArray(vNames)

	If @Instr(vLEs, @AccessStringArray(vNames, n)) = 0 Then
	{
	WriteLn("LE name '" + @AccessStringArray(vNames, n) + "' not in current form.")
	WriteLn("(Ref. File: " + vNamesPath + ")" + @NewLine())
	}

Next

// Build Word doc selection menu

// Set position of picklist to the left of and above the command button
PopupSelectPosition(4,@Xpos(ThisElement)-125,@Ypos(ThisElement)-50)

// generate current list of docs
vOK=@shell(vDir)

// Convert the document list from one-per-line to semi-colon separated if nec
vDocList = @insert(vDocListPath)
vDocList = @Replace(vDocList, @NewLine(), ";")

vSelectedDoc = @PopupMenu(vDocList, "SELECT MERGE DOCUMENT" )

If vSelectedDoc <> "" Then

{

// Build which record(s) menu
// Prevents Users from printing batch letters -- 2005/10/31 BW

vOneOrAll = @PopupMenu("THIS Record Only;ALL Retrieved Records","SELECT RECORD(S) TO MERGE PRINT")

if vOneOrAll = "All Retrieved Records" and @group = "Users" then
{
	@msgbox("You do not have access to batch letter printing!","","")
	vOneOrAll = ""
}

// Reset Popup position
PopupSelectPosition(0,99,99)

If vSelectedDoc <> "" and vOneOrAll <> "" Then
{

	// Delete exsting data file. New one will be generated.
	If FileExists(vDocsPath + vSelectedDoc) Then
	{
	fileDelete(vDataPath)

	If vOneOrAll = "THIS Record Only" Then
		PrintCurrentRecordOnly()
	Else
		PrintAllRetrievedRecords()

	If vGoPrint = "Yes" Then
//	n = @Shell("START Winword /mMerge_to_New_Doc " + @Chr(34) + vDocsPath + vselectedDoc + @Chr(34))

	n = @ASynchShell(vDocsPath + vselectedDoc)
	}

	Else
	@Msgbox(vDocsPath + vSelectedDoc + " doesn't exist.","","Aw shucks!")
}
}
}

// Log Letter sent

if vOneOrAll = "THIS Record Only" then
{
	vLttr = @replace(vSelectedDoc,".doc","")
	vLttrInfo = @Serverdate() + " -- " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
	if @Len(VLttrInfo) >60 then vLttrInfo = @left(vLttrInfo,60) + " ... "
	DBLttrLog0 = vLttrInfo + @NewLine() + DBLttrLog0

	DBNote0 = "Sent " + vLttr + " -- " + DBAdd1 + " " + DBAdd2 + " " + DBCity + " " + DBProv + " " + DBPCod
}

vCount = @FormResultSetTotal("Note Data")

If DBNote0 <> "" then
{	If vCount = 1 Then
	{	If @FormFieldValue("Note Data", "Note", 1) <> "" Then
		{	vNewNoteNo = @FormNewRecord("Note Data")
		}
		Else
		{	vNewNoteNo = 1
		}
	}
	Else
	{	vNewNoteNo = @FormNewRecord("Note Data")
	}
}
Else
{	If DBNote0 = "" THEN @MsgBox ("", "You Cannot Enter a Blank Note!","")
}

gFileID = @ToNumber(@GlobalValue("gFileID")) +1
vNewFileID = gFileID
GlobalValue("gFileID", vNewFileID)

FormFieldValue("Note Data","NoteRef",vNewNoteNo,DBID)
FormFieldValue("Note Data","NoteDate",vNewNoteNo,@ServerDate())
FormFieldValue("Note Data","NoteTime",vNewNoteNo,@ServerTime())
FormFieldValue("Note Data","Note",vNewNoteNo,DBNote0)
FormFieldValue("Note Data","NoteID",vNewNoteNo,vNewFileID)
FormFieldValue("Note Data","NoteSig",vNewNoteNo,@UserID)

vNewNote = @FormFieldValue("Note Data","Note",vNewNoteNo)

ForceRedraw()

gPosted = 1

DBNote0 = ""
 



-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #9 - Jan 8th, 2011 at 6:12pm
Print Post Print Post  
Well, the letter still came out without the crosslookup info, but the text box says:

Code
Select All
Adding Staff Info for ID: BW
yes
XLookupSourceList returned:

 



So it looks like vVals = @XLookupSourceList(@FN, vDBStaffID, "Staff!StaffRef", vStaffLEs) has vDBStaffID right, but for some reason is either not accessing the Staff table, or having a hard time looking up the vStaffLEs ...
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: MailMerge Fetching Info
Reply #10 - Jan 10th, 2011 at 2:38pm
Print Post Print Post  
Blair,

You need to check the spellings on everything, including spaces and suchlike. Remember that the XLookup family uses Form/Element names, not Database/Field names. Something is not spelled right. make sure you have a database called Staff with a Form called StaffRef. Make sure that the StaffRef form has an element whose name exactly matches each of the names in your vStaffLEs list.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #11 - Jan 10th, 2011 at 2:54pm
Print Post Print Post  
Aha!  I checked the spellings, and they are all correct, but your email made me think ...

Does that mean my current line of:

Var vStaffLEs as String = "StaffName;StaffTitle;StaffEmail"

Should really be:

Var vStaffLEs as String = "Staff!StaffName;Staff!StaffTitle;Staff!StaffEmail"
  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: MailMerge Fetching Info
Reply #12 - Jan 10th, 2011 at 4:00pm
Print Post Print Post  
Hello Blair,

Leave the Writelns in your programming and swap out the vStaffLEs line for the following.

Code
Select All
Var vStaffLEs as String = "StaffRef" 



I want to know what the Writeln window shows

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: MailMerge Fetching Info
Reply #13 - Jan 10th, 2011 at 5:58pm
Print Post Print Post  
BWETTLAUFER wrote on Jan 10th, 2011 at 2:54pm:
Aha!  I checked the spellings, and they are all correct, but your email made me think ...

Does that mean my current line of:

Var vStaffLEs as String = "StaffName;StaffTitle;StaffEmail"

Should really be:

Var vStaffLEs as String = "Staff!StaffName;Staff!StaffTitle;Staff!StaffEmail"


No, you don't need the form name on all the elements. Do what Ray asks above and let us know the results.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BWETTLAUFER
Full Member
***
Offline



Posts: 216
Location: Cambridge, Ontario
Joined: Apr 9th, 2010
Re: MailMerge Fetching Info
Reply #14 - Jan 10th, 2011 at 8:12pm
Print Post Print Post  
Hi Ray,

I made the change, and the WriteLn window still shows the same info:

Code
Select All
Adding Staff Info for ID: BW
yes
XLookupSourceList returned:
 

  
Back to top
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print