Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Copy query to Word
Message
From
15/11/2006 11:29:25
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
15/11/2006 11:13:45
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01169664
Message ID:
01169912
Views:
11
>>>I am porting an Access application over to VFP. The Access code has a section which executes a query (SQL SELECT) and copies it to the clipboard, then pastes the query into a Word document where it appears as a table. Is this type of action possible in VFP? That is: how would you get the results of a SELECT into the clipboard so it can be pasted into the Word doc?
>>>
>>>Thanks for any hints. (or actual code)
>>
>>Don,
>>There are multiple ways as always. One of them:
>>
>>#Define wdSeparateByCommas  2
>>#Define wdSeparateByDefaultListSeparator  3
>>#Define wdSeparateByParagraphs  0
>>#Define wdSeparateByTabs  1
>>
>>#Define wdTableFormatClassic1  4
>>#Define wdTableFormatColorful1  8
>>#Define wdTableFormatColorful2  9
>>#Define wdTableFormatColorful3  10
>>#Define wdTableFormatContemporary  35
>>#Define wdTableFormatElegant  36
>>
>>lcTempFile = Sys(2015)+'.tmp'
>>Use employee
>>Copy To (lcTempFile) Fields First_Name, Last_Name, Title Type Delimited With Tab
>>_Cliptext = "First Name"+Chr(9)+"Last_Name"+Chr(9)+"Title"+;
>>  Chr(13)+Chr(10)+Chrtran(Filetostr(lcTempFile),'"','')
>>Erase (lcTempFile)
>>
>>oWordDocument=Createobject("word.application")	&& Create word object
>>With oWordDocument
>>  .documents.Add	&& New file
>>  With .ActiveDocument.Range
>>    .Paste      && Paste VFP data
>>    .ConvertToTable(wdSeparateByTabs,,,,wdTableFormatColorful2,,,,,,.F.,.F.,.F.) && Convert to table
>>  Endwith
>>  .Visible = .T.
>>Endwith
>>
Cetin
>
>Thanks Cetin I'm almost there.
>My code is:
>
>with oWord
>.ActiveDocument.Bookmarks("AMUTypes").select
>.selection.Paste
>.ActiveDocument.range.ConvertToTable(wdSeparateByTabs,,,,wdTableFormatColorful2,,,,,,.F.,.F.,.F.) && Convert to table
>endwith
>
>My problem is that the ConvertToTable is acting on the entire document rather than just the pasted data. Can you tell me how to select the pasted data so only it is converted to a table?
>
>Thanks

In short form instead of ActiveDocument.Range you use .Application.Selection. Here is a longer sample:
*#include wdconst.h
#Define wdWithInTable  12
#Define wdStory  6
#Define wdTable  15
#Define wdCollapseEnd  0
#Define wdSeparateByCommas  2
#Define wdSeparateByParagraphs  0
#Define wdSeparateByTabs  1
#Define wdTableFormatColorful1  8
#Define wdTableFormatColorful2  9
#Define wdTableFormatColorful3  10

#Define CHR_TAB	chr(9)
#Define CHR_CR	chr(13)+chr(10)
Local loWord, lcMyTable

lcMyTable1 = "Parameters" + CHR_TAB + "Limit" + CHR_TAB + "Average" + CHR_CR ;
  + "Chlorine"   + CHR_TAB + "13.5"  + CHR_TAB + "10"      + CHR_CR ;
  + "Boron"      + CHR_TAB + "55.8"  + CHR_TAB + "45"      + CHR_CR

lcTempFile = sys(2015)+'.tmp'
Use employee
Copy fields First_name, last_name, Title ;
  to (lcTempFile) type delimited with TAB
lcMyHeader = chrtran('First Name,Last Name,Job Title',',',CHR_TAB)+CHR_CR
lcMyTable2=lcMyHeader+ChrTran(FileToStr(lcTempFile),'"','')
Erase (lcTempFile)

loWord   = newobject('word.application')
With loWord
  .Documents.Add()
  With .Selection
    .EndKey(wdStory)
    .InsertParagraphAfter()
  Endwith
  .ActiveDocument.Bookmarks.Add('StringTable') && Bookmark table positions
  With .Selection
    .EndKey(wdStory)
    .InsertParagraphAfter()
  Endwith
  .ActiveDocument.Bookmarks.Add('EmployeeTable')

  PutWordTable(loWord,lcMyTable2, 'EmployeeTable') && Add in reverse order - using bookmarks
  PutWordTable(loWord,lcMyTable1, 'StringTable')
  Clear
  .Visible = .t.
Endwith

Function PutWordTable
  Lparameters toWord, tcTable, tcBookmarkName
  With toWord.ActiveDocument
    .Bookmarks(tcBookmarkName).Select
    With .Application.Selection
      .Collapse(wdCollapseEnd)	&& Go to end of bookmark
      If .Information(wdWithInTable) && If we're in a table
        .MoveEnd(wdTable)
        .Collapse(wdCollapseEnd)	&& Get out of table
        .InsertParagraph()			&& Split table
        .Collapse(wdCollapseEnd)	&& Clear selection
      Endif
      .Text = tcTable
      loTable = .ConvertToTable(wdSeparateByTabs,,,,wdTableFormatColorful2,.f.,,,,,,.f.,,.f.)
      .Collapse(wdCollapseEnd)
    Endwith
  Endwith
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform