FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Word and OLE
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Word and OLE
Posted: Mon Jul 13, 2015 12:14 AM

Hi All

I have an existing word document and I need to insert a table between some existing text

ie

We have please in providing a quotation for

Qty Description Price

1 Item 1 $100.00
2 Item 2 $300.00


Please note the above price etc etc

Thanks

Colin

Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Word and OLE
Posted: Mon Jul 13, 2015 03:52 AM
Please refer \Fwh\Samples\WordTable.Prg

Regards
Anser
Posts: 663
Joined: Mon Dec 05, 2005 11:22 PM
Re: Word and OLE
Posted: Mon Jul 13, 2015 01:40 PM

Don't forget to use macro recordings to help understand.
If I don't know how to do something in Excel or Word I just record a macro, do the steps I want, then look at the recorded macro for the VB code they used.

Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Re: Word and OLE
Posted: Mon Jul 13, 2015 11:32 PM

Hi Anser and Gale

Thanks for the replies.

Anser I saw this example before I posted the question but your solution puts the table at the bottom of
the document - I need to insert the table into an existing document - between existing lines on the document.

I was thinking of adding a single row table into the template - finding the table and inserting the data and adding more
rows as needed.

Thanks

Colin

Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Word and OLE
Posted: Tue Jul 14, 2015 04:40 AM
Colin Haig wrote:Hi Anser and Gale

Thanks for the replies.

Anser I saw this example before I posted the question but your solution puts the table at the bottom of
the document - I need to insert the table into an existing document - between existing lines on the document.

I was thinking of adding a single row table into the template - finding the table and inserting the data and adding more
rows as needed.

Thanks

Colin


Try this code :-)
Code (fw): Select all Collapse
TRY
    oWord := CreateObject("Word.Application")
CATCH
    MsgInfo("Word is not installed in this PC. Unable to continue further")
    Return NIL
END

TRY
    oDoc := oWord:Documents:Open(cWordFileName)  // 5th parameter can be the password
CATCH
    MsgInfo("Unable to open the template file "+cWordFileName)
    oWord:Quit()
    Return NIL        
END

oWord:Visible:=.T.
oDoc:Select()
oSel = oWord:Selection

// Need to insert a table in the Word file with the some details
oRange := oSel:Document:Content
cSrch:="Your text to be searched"

// Search and identify the location in the word file where you need to insert the Table. Use a unique keyword to identify the location
IF AT( cSrch, oRange:Text ) = 0; RETURN .F.; ENDIF

WHILE oRange:Find:Execute( cSrch )
    oRange:Text = ""
    oRange:Collapse( wdCollapseEnd )
ENDDO

// Add a table with required rows and columns
oTable:=oWord:ActiveDocument:Tables:Add(oRange,6,5,wdWord9TableBehavior,wdAutoFitContent)

// Whatever you need to do with the table goes here
WITH OBJECT oTable

   // Set up borders and shading
   // If u don't want borders then set the next 2 lines to .F.
   :Borders:InsideLineStyle:=.T.
   :Borders:OutsideLineStyle:=.T.

   // Shade first row for headings
   :Rows[1]:Shading:Texture = 100

   // Put the heading row text and set alignment
   :Cell(1,1):Range:ParagraphFormat:Alignment:=wdAlignParagraphRight
   :Cell(1,1):Range:InsertAfter("Sl.No")   
   
   
   :Cell(1,2):Range:ParagraphFormat:Alignment:=wdAlignParagraphLeft
   :Cell(1,2):Range:InsertAfter("Details")
   
   .....
   ......
   

   :Columns:Autofit()
END


Hope the above given code will serve your purpose or at least it will put you into the right direction. :-)

Regards
Anser
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Re: Word and OLE
Posted: Wed Jul 15, 2015 05:27 AM

Hi Anser

Thanks for you sample - I have now made it work.

Instead of using :Columns:AutoFit()
I used

oTable:Columns(1):Width = oWord:InchesToPoints(.5)
oTable:Columns(2):Width = oWord:InchesToPoints(5)
oTable:Columns(3):Width = oWord:InchesToPoints(1)

Cheers

Colin

Continue the discussion