Script Vfp to WordPerfect to Vfp

If you are a VFP developer and have any questions about using our products from VFP here is the place to post them.
Post Reply
tmurp
Posts: 3
Joined: Mon Sep 19 2005

Script Vfp to WordPerfect to Vfp

Post by tmurp » Wed Sep 28 2005

This is not quite a VFP issue, but close. My VFP App needs to run some scripts using the client's wordprocessor to generate documents used by the application. Some of our clients are still using WordPerfect (specifically version 8) and I am having a difficult time getting this script to run. The error is from WordPerfect and tells me "Error initializing output device" when the print command in called. It seems the AnyUni printer is not accepting the stream even though all API calls return expected results (See MessageBox calls).

If I pause the script at the point just before printing, and examine the printer properties all appears as it should but even printing from the IDE causes the same issue.

It seems it should be working, but I need help!
Sequence:
DriverInit
EnablePrinter
SetFileNameOptions
SetDefaultFileName
WordPerfect - Set Printer
WordPerfect - Print Document (ERRORS HERE)
WordPerfect - Reset Printer

I know this is more a WordPerfect implementation, but any insight would be greatly appreciated!

Code: Select all

[size=7]// Macro to install print WordPerfect documents to PDF 
// BMSW Version = 8.00

Application (WordPerfect; "WordPerfect"; Default!)

Global HKEY_CURRENT_USER = -2147483647
Global CH7_REG_KEY = "Software\OurRegistryPath"
Global REG_SZ = 1
Global MAX_PATH = 260
Global PDF_PRINTER_NAME = "Our PDF Output"
Global PDF_OPTIONS = 1+2
Global LockSemaphore = 0	//0=False 1=True - Is this a multiuser environment?
Global LIC_NAME = "Our License Name"

Global LIC_KEY = "Our very long license key..."
 

CH7FilePath = GetCH7Path()
MessageBox(rtn;"CH7FilePath";CH7FilePath; IconInformation!)   

DLLCall Prototype DriverInit ("cdintf210.dll"; "DriverInit"; Integer!; {AnsiString(PrinterName)})

DLLCall Prototype PDFDriverInit ("cdintf210.dll"; "PDFDriverInit"; Integer!; {AnsiString(PrinterName)})

DLLCall Prototype DriverEnd ("cdintf210.dll"; "DriverEnd"; Integer!; {DWORD(hPrinter)})

DLLCall Prototype SetDefaultFileName ("cdintf210.dll"; "SetDefaultFileName"; Integer!; {DWORD(hPrinter); AnsiString(Filename)})

DLLCall Prototype SetFileNameOptions ("cdintf210.dll"; "SetFileNameOptions"; Integer!; {DWORD(hPrinter); AnsiString(Options)})

DLLCall Prototype SetDocFileProps ("cdintf210.dll"; "SetDocFileProps"; Integer!; {DWORD(hPrinter); AnsiString(LockName); AnsiString(Options); AnsiString(Path); AnsiString(FileName)})

DLLCall Prototype EnablePrinter ("cdintf210.dll"; "EnablePrinter"; Integer!; {DWORD(hPrinter); AnsiString(Site); String(Keys)})

DLLCall Prototype PDFLock ("cdintf210.dll"; "Lock"; Integer!; {DWORD(hPrinter); AnsiString(LockName)})

DLLCall Prototype PDFUnlock ("cdintf210.dll"; "Unlock"; Integer!; {DWORD(hPrinter); AnsiString(LockName); LoWord(Timeout)})

DLLCall Prototype SetDefaultPrinter ("cdintf210.dll"; "SetDefaultPrinter"; Integer!; {DWORD(hPrinter)})

DLLCall Prototype RestoreDefaultPrinter ("cdintf210.dll"; "RestoreDefaultPrinter"; Integer!; {DWORD(hPrinter)})

DLLCall Prototype Sleep ("kernel32"; "Sleep"; Integer!; {DWORD(dwMilliseconds)})


TempFileName=GetTempFilename()
MessageBox(rtn;"TempFileName";TempFileName; IconInformation!)   
hPrinter=PDFStart(TempFilename)
MessageBox(rtn;"PdfStart Finished";hPrinter; IconInformation!)   
if (hPrinter>0)
	oldPrinter=PrinterSelectByName(PDF_PRINTER_NAME)
	MessageBox(rtn;"OldPrinterName";oldPrinter; IconInformation!)   
	MessageBox(rtn;"Ready to Print";"Print"; IconInformation!)   
	MessageBox(rtn;"Printer";PrinterSelectByName(); IconInformation!)   
	Print(Action:FullDocument!) //Errors Here
	MessageBox(rtn;"ResetPrinterName";oldPrinter; IconInformation!)   
	PrinterSelectByName(PrinterName:oldPrinter)
	MessageBox(rtn;"AmyUni";"Ready to call PDFFinish"; IconInformation!)   
	PDFFinish(hPrinter)
	DocMgrImport(TempFileName)
endif 



Function GetTempFilename()
	Filename=RegGet("Document Management\New Defaults";"FilePath")
	return (Filename)
EndFunc

Function PDFStart(Filename)
	PDFStart = 0
	PdfFileName=GetTempFilename()
	LockName = GetLockName()
	MessageBox(rtn;"LockName";LockName; IconInformation!)
	hPrinter=DriverInit(PDF_PRINTER_NAME)
	MessageBox(rtn;"hPrinter";hPrinter; IconInformation!)   
	if (hPrinter=0)
		MessageBox(rtn;"AmyUni";"Unable to initialize PDF Driver - PDF not created."; IconInformation!)   
		PDFStart = 0
		return (PDFStart)
	endif
	if (LockSemaphore=1) //Is Locking Turned On
		MessageBox(rtn;"AmyUni";"Ready to Lock."; IconInformation!)   
		while (PDFLock(hPrinter;LockName)<>0)
			MessageBox(rtn;"AmyUni";"Sleeping."; IconInformation!)   
			Sleep(1000)
		endwhile
	endif
	MessageBox(rtn;"AmyUni";"Ready to Enable Printer."; IconInformation!)   
	i = EnablePrinter(hPrinter; LIC_NAME; LIC_KEY)
	MessageBox(rtn;"EnablePrinter";i; IconInformation!)   
	if (i<>1)
		MessageBox(rtn;"AmyUni";"Unable to enable PDF Driver - PDF not created."; IconInformation!)   
		if (LockSemaphore=1) //Is Locking Turned On
			i = PDFUnlock(hPrinter;LockName;1000)
			i=DriverEnd(hPrinter)
		endif 
		return(0)
	endif 
	if (LockSemaphore=1) //Is Locking Turned On
		MessageBox(rtn;"AmyUni";"Ready to Set Doc File Props."; IconInformation!)   
		i=SetDocFileProps(hPrinter;LockName;1+2;"";PdfFileName)
		if (i<>0)
			MessageBox(rtn;"AmyUni";"Unable to enable PDF Driver - PDF not created."; IconInformation!)   
			i = PDFUnlock(hPrinter;LockName;1000)
			i=DriverEnd(hPrinter)
			return(0)
		endif
	endif
	if (LockSemaphore=0)
		MessageBox(rtn;"SetFileNameOptions";PdfFileName; IconInformation!)   
		i=SetFileNameOptions(hPrinter;1+2)
		MessageBox(rtn;"SetFileNameOptions";i; IconInformation!)   
		MessageBox(rtn;"SetDefaultFileName";PdfFileName; IconInformation!)   
		i=SetDefaultFileName(hPrinter;PdfFileName)
		MessageBox(rtn;"SetDefaultFileName";i; IconInformation!)
	endif  
	PDFStart=hPrinter
	return (PDFStart)
EndFunc

Procedure PDFFinish(hPrinter)
	if (LockSemaphore=1) //Is Locking Turned On
		LockName=GetLockName()
		i=PDFUnlock(hPrinter;LockName;1000)
	endif
	//MessageBox(rtn;"DriverEnd";hPrinter; IconInformation!)
	//i=DriverEnd(hPrinter)
	//MessageBox(rtn;"PDFFinish";i; IconInformation!)   
EndProc

Function GetLockName()
	return (?Name())
EndFunc

Function GetCH7Path()
	// Just get it from the Registry (CH7 program puts it there)
	return (RegGet(""; "Program Path"))
EndFunc

Function RegGet(SubKey; ValueName)
	local RegKey; ValueData; i
	// Build the full subkey by prepending the standard path
	if (SubKey = "")
		SubKey = CH7_REG_KEY
	else
		SubKey = CH7_REG_KEY + "\" + SubKey
	endif
	// Open the SubKey
	RegKey = RegistryOpenKey( Hkey:CurrentUser!; SubKey: Subkey)
	if (RegKey = 0)
		return ("")
	endif
	// Extract the data value
	ValueData = RegistryQueryValue(Hkey:RegKey; Value:ValueName)
	// Close the SubKey
	i = RegistryCloseKey(RegKey)
	return (ValueData)
EndFunc

Function RegPut(SubKey; ValueName; ValueData)
	local RegKey; i; j
	// Build the full subkey by prepending the standard path
	if (SubKey = "")
		SubKey = CH7_REG_KEY
	else
		SubKey = CH7_REG_KEY + "\" + SubKey
	endif
	// Open the SubKey
	RegKey = RegistryOpenKey( Hkey:CurrentUser!; SubKey: Subkey)
	if (RegKey = 0)
		return ("")
	endif
	// Store the data value
	i = RegistrySetValue(RegKey; ValueName; ValueData; String!)
	// Close the SubKey
	j = RegistryCloseKey(RegKey)
	return (i)
EndFunc

Function DocMgrImport(Filename)
        //Imports the document into our VFP application.
EndFunc[/size]

Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan » Fri Sep 30 2005

Hello,

Did you try to call EnablePrinter right before the print command?
Print(Action:FullDocument!) //Errors Here

Thanks.

tmurp
Posts: 3
Joined: Mon Sep 19 2005

Post by tmurp » Fri Sep 30 2005

Yes I did. That is part the PDFStart function.

Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan » Wed Oct 05 2005

Hello,

You are calling EnablePrinter() way beyond the actual printing specially that tere are message boxes between your call to PDFStart and your call the the Print command.

I suggest that you call EnablePrinter() right before the Print() command or when catching the EnablePre() event fired by the driver.

Hope this helps.

Post Reply