Word COM
I have modified the function below to operate under PB 9, using the example in the Samples section.
This previously worked OK under PB8 using the old syntax. All works now OK except the font setting, which causes the program to fail. The section in question is between the two hash lines.
Can anyone help me to sort this out?
I have included the operating syntax in the unlikely event that anyone else wants to use it once the font is sorted out!
Iain Johnstone
I have modified the function below to operate under PB 9, using the example in the Samples section.
This previously worked OK under PB8 using the old syntax. All works now OK except the font setting, which causes the program to fail. The section in question is between the two hash lines.
Can anyone help me to sort this out?
I have included the operating syntax in the unlikely event that anyone else wants to use it once the font is sorted out!
Iain Johnstone
Code:
FUNCTION OutputToWord (message AS STRING, orientationtype AS LONG, fontname AS STRING, fontsize AS SINGLE, _ setupflag AS LONG,printflag AS LONG,closeflag AS LONG) EXPORT AS LONG 'Function usage:- 'Syntax (eg) = OutputToWord Message,0,"Courier New",10,1,1,0 ' where Message is formatted text to print ' orientation - 0=port, 1=land ' typeface name ' fontsize ' start - 1 to start up Word ' print - 1 to print text ' close - 1 to close Word 'so the above example will start Word and print the message in Courier New at 10 points ' OutputToWord Message,0,"Arial",12,0,1,0 will continue to print in Arial 12 pt ' OutputToWord Message,0,"Courier New",10,0,0,1 will close Word STATIC oWordApp AS Int__Application ' Application Interface STATIC oWordDoc AS Int__Document ' Document Interface STATIC oWordSel AS Selection ' Selection STATIC oWordFont AS Int__Font 'font or TextEffectFormat? STATIC oWordPageSetup AS PageSetup 'set orientation etc STATIC oWordOptions AS Options 'set orientation etc STATIC sProgID_Word AS STRING LOCAL vBool AS VARIANT LOCAL vText AS VARIANT LOCAL vFile AS VARIANT LOCAL vFileFmt AS VARIANT LOCAL vVnt AS VARIANT LOCAL result AS STRING LOCAL iDocCount AS LONG IF setupflag<>0 THEN ' Open an instance of MSWORD oWordApp = NEWCOM $PROGID_Application ' Could MSWORD be opened? If not, terminate this app IF ISFALSE ISOBJECT(oWordApp) THEN MSGBOX "Unable to open or start MSWORD!" EXIT FUNCTION END IF ' Make MSWORD visible in for example normal show state oWordApp.Visible = 1 oWordApp.WindowState = %wdWindowStateNormal ' Get the current document count from the "documents collection" iDocCount = oWordApp.Documents.Count oWordDoc = oWordApp.Documents.Add IF ISFALSE ISOBJECT(oWordDoc) THEN MSGBOX "MSWORD was not able to start a new document." & $CR & _ "Please ensure MSWord and VBA are installed." GOTO Terminate END IF ' Get the current document count from the "documents collection" - This should be one value higher than before iDocCount = oWordApp.Documents.Count 'set orientation and margins oWordDoc.PageSetup.SectionStart = %wdSectionNewPage oWordDoc.PageSetup.orientation= orientationtype ' from header oWordApp.Options.MeasurementUnit=3 'points 'measurements in points oWordDoc.PageSetup.TopMargin=72 oWordDoc.PageSetup.BottomMargin=90 oWordDoc.PageSetup.LeftMargin=36 oWordDoc.PageSetup.RightMargin=36 oWordDoc.PageSetup.Gutter=0 IF orientationtype=1 THEN 'landscape A4 converted to points oWordDoc.PageSetup.PageWidth = (72*11.66) oWordDoc.PageSetup.PageHeight = (72*8.25) ELSE oWordDoc.PageSetup.PageHeight = (72*11.66) oWordDoc.PageSetup.PageWidth = (72*8.25) END IF END IF '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IF printflag<>0 THEN 'set font - face may change through document '########################################################################### '?"setting font" ' ''fails here ' oWordFont.Name=ucode$(fontname) 'string from header '?"fontname set" ' oWordFont.Size=fontsize 'single from header '?"fontsize set" '########################################################################## 'printing starts here oWordSel = oWordApp.Selection oWordSel.TypeText(UCODE$(message)) ' Set a paragraph marker (end of paragraph) oWordSel.TypeParagraph END IF '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~## IF closeflag <>0 THEN ' print footer - should be centred oWordSel.TypeText(UCODE$("Printed from Masterdrain")) oWordSel.TypeParagraph ' ' Close the current document and then close MSWORD completely if required templong=MSGBOX ("Close MSWORD?",%MB_YESNO OR %MB_TASKMODAL,"Masterdrain") IF templong=%IDYES THEN 'save file result=INPUTBOX$("Enter filename to save","About to save the document...", "Output.doc") 'this is untidy IF RIGHT$(result,4)<>".doc" THEN result=result+".doc" vFile = result vFileFmt = %wdFormatDocument oWordDoc.SaveAs(vFile, vFileFmt) oWordApp.ActiveWindow.CLOSE oWordApp.Quit ' Close all of the Interfaces oWordSel = NOTHING oWordDoc = NOTHING oWordApp = NOTHING oWordFont = NOTHING oWordPageSetup = NOTHING END IF END IF FUNCTION =1:EXIT FUNCTION Terminate: oWordApp.ActiveWindow.CLOSE oWordApp.Quit ' Close all of the Interfaces oWordSel = NOTHING oWordDoc = NOTHING oWordApp = NOTHING oWordFont = NOTHING oWordPageSetup = NOTHING FUNCTION =1 END FUNCTION '=======================================================
Comment