Word and OLE

Post Reply
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Word and OLE

Post by Colin Haig »

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
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Word and OLE

Post by anserkk »

Please refer \Fwh\Samples\WordTable.Prg

Regards
Anser
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Re: Word and OLE

Post by Gale FORd »

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.
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Word and OLE

Post by Colin Haig »

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
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Word and OLE

Post by anserkk »

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: Select all

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
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Word and OLE

Post by Colin Haig »

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
Post Reply