Page 1 of 1

Printing to a word document

Posted: Wed Sep 18, 2013 2:06 pm
by omni
Roger,

We have a user that wants to modify invoices for some customers on demand in word. He wants to print an invoice to something that can be saved as a .doc file, or either we can print to something where he can copy and paste to an empty work document. We cannot find a conversion tool that does this on a print job, and the print/preview does not have a method to copy and paste.

Do we have something that does that, or any suggestions?

Thanks

Fred
Omni

Re: Printing to a word document

Posted: Wed Sep 18, 2013 3:57 pm
by Auge_Ohr
omni wrote:Do we have something that does that, or any suggestions?
you can use activeX to create a Word Document.

copy / paste from Preview is not possible while Grafic ... not Text

Re: Printing to a word document

Posted: Wed Sep 18, 2013 4:19 pm
by rdonnay
I can't think of any easy solution for you.

Re: Printing to a word document

Posted: Thu Sep 19, 2013 12:15 am
by skiman
Hi,

I'm doing the following in some situations.
- Create a RTF file with the layout you want.
- Use memoread to read this.
- Use some kind of tags as <[ alltrim(customer->name)]> in the header and replace them.
- Use a <[start]> and <[stop]> for the body. Define one line in your RTF.
- Read the 'body' a use a loop to fill the body. In the body use tags as <[ str(invoice->quantity,8,2)]> and so on.
- In the footer you can use <[ str(totalinvoice,9,2) ]> and so on.
- Save it with the DOC extension.
- You have your Word document.

I use the same technique with HTML templates which were saved as XLS.

Printing to RTF and saving this with DOC extension will also work.

Re: Printing to a word document

Posted: Thu Sep 26, 2013 4:51 am
by c-tec
Hello,
a littel sample with using bookmarks in the Word document
regards
Rudolf

Code: Select all



//////////////////////////////////////////////////////////////////////
// Open a MS Word document and replace the bookmarks with the values
// passed in the array aData. The resulting document is then saved
// back to the hard drive.
//////////////////////////////////////////////////////////////////////
static FUNCTION WordFillDocument(cFile,aData,cSaveAs,nBearb)
  LOCAL oWord,oBM,oDoc,lWordFinished := .f.,x
  default nBearb to 0
  // Create a Word ActiveX component
  oWord := CreateObject("Word.Application")
  IF Empty( oWord )
    MsgBox( "Microsoft Word not installed" )
    return .f.
  ENDIF
  oWord := oWord:dynamicCast(ActiveXObject())
  oWord:Quit := {||lWordFinished := .T.}
  oWord:visible := .T.
  lWordFinished := .f.

  // Open a Word document and retrieve the bookmarks
  // collection.
  oWord:documents:open( cFile )
  oDoc := oWord:ActiveDocument
  oBM  := oDoc:Bookmarks

  // Replace the Bookmark with a new value
  ReplaceBookmark(oBM , "COMPANY"     , aData[1] )
  ReplaceBookmark(oBM , "TO"          , aData[2] )
  ReplaceBookmark(oBM , "FAX"         , aData[3] )
  ReplaceBookmark(oBM , "FROM"        , aData[4] )
  ReplaceBookmark(oBM , "TOTAL_PAGES" , "1" )
  ReplaceBookmark(oBM , "CARBON_COPY" , "" )
  ReplaceBookmark(oBM , "SUBJECT"     , aData[5] )
  ReplaceBookmark(oBM , "SALUTATION"  , aData[6] )
  ReplaceBookmark(oBM , "TEXT"        , aData[7] )
  ReplaceBookmark(oBM , "DATE"        , DToC(Date()) )

  ReplaceBookmark(oBM , "NAME1"       , aData[15] )
  ReplaceBookmark(oBM , "NAME2"       , aData[16] )
  ReplaceBookmark(oBM , "STRASSE"     , aData[17] )
  ReplaceBookmark(oBM , "LKZ"         , aData[18] )
  ReplaceBookmark(oBM , "PLZ"         , aData[19] )
  ReplaceBookmark(oBM , "ORT"         , aData[20] )
  ReplaceBookmark(oBM , "Z_HD"        , aData[21] )
  ReplaceBookmark(oBM , "LAND"        , getland(aData[18]) )


  // Save the resulting Word document
  IF(ValType(cSaveAs)=="C")
    oDoc:saveas(cSaveAs)
  ENDIF

  // Optional print out of document to standard
  // printer
  *IF nBearb = 2
    *oDoc:PrintOut()
  *ENDIF

  // Close the document and destroy the ActiveX
  // object
  if nBearb # 1
     //oDoc:sendfax()
     oDoc:close()
     oWord:Quit()
  endif
  sleep(200)
  oWord:destroy()
  set_msg()
  if nBearb = 2
     WinAPIPrint(cFile)
  endif

RETURN .t.

static FUNCTION ReplaceBookmark(oBM,cBM,cValue)
  LOCAL lRet := oBM:Exists(cBM)
  LOCAL oF
  IF(lRet)
    set_msg("Replace " + cBM + " with " + var2char(cValue))
    oF := oBM:Item(cBM)
    oF:Range:Text := cValue
    oF:Destroy()
  ENDIF
RETURN(lRet)