Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get text from Word (MS Office) window or file
#1
examples

Macro Get text from MS Office Word
Code:
Copy      Help
;/exe 1
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application a._getactive ;;connect to Word. On Vista/7, macro process must run as User. QM normally runs as Admin. The /exe 1 tells QM to run the macro in separate process, as User.
Word.Range r

;get all text
r=a.ActiveDocument.Range
str sAllText=r.Text
out sAllText

;get selected text
r=a.Selection.Range
str sSelText=r.Text
out sSelText

;BEGIN PROJECT
;main_function  Get text from MS Office Word
;END PROJECT

Macro Get text from a Word document file
Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Document d._getfile(_s.expandpath("$documents$\document1.doc"))
Word.Range r

;get all text
r=d.Range
str sAllText=r.Text
out sAllText
#2
This makes for a great member function:
Member function str.WordText
Code:
Copy      Help
function ~WordDoc
;EXAMPLE
;
;str Text.WordText("$documents$\document.doc")
;out Text

typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Document d._getfile(_s.expandpath(WordDoc))
Word.Range r

;get all text
r=d.Range
this=r.Text
#3
When I try to get text from multiple files in the file path, the program executes normally and retrieves text from all of the files. However, after retrieving the text from the last file, I get the following RT error: Error (RT) in DocSearchTest: 0x800401EA, Moniker cannot open file. ?



Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
str files
if(!inp(files "Please specify the location of the files:" "TestDocRetrieve" "C:\test\*.doc")) ret

Dir d1
foreach(d1 files FE_Dir) ;;for each file that matches the pattern.
(`i),
str sPath=d1.FileName(1)

,Word.Document d._getfile(_s.expandpath(sPath))
,Word.Range r

;get all text
,r=d.Range
,lpstr sAllText=r.Text
;;out sAllText
#4
Macro Macro1754
Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
str files
if(!inp(files "Please specify the location of the files:" "TestDocRetrieve" "C:\test\*.doc")) ret

Dir d1
foreach(d1 files FE_Dir) ;;for each file that matches the pattern.
,str sPath=d1.FileName(1)
,out sPath
,
,Word.Document d._getfile(sPath)
,Word.Range r
,
,;get all text
,r=d.Range
,lpstr sAllText=r.Text
,;out sAllText
#5
It is breaking on a temporary Word file like "~$cument1.doc".

It's "parent", document1.doc is the first file from which text is retrieved.

So, I think QM is having Word keep this file open during the loop. Once the code breaks, the file is deleted and any search, hidden files included, finds no such file.

I think I should be able to work around it with a test for "~$" in the filename.
#6
At first create list of files, then open files from the list.

Macro Macro1754
Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
str files
if(!inp(files "Please specify the location of the files:" "TestDocRetrieve" "C:\test")) ret

ARRAY(str) a
GetFilesInFolder a files "*.doc"

int i
for i 0 a.len
,out a[i]
,
,Word.Document d._getfile(a[i])
,Word.Range r
,
,;get all text
,r=d.Range
,lpstr sAllText=r.Text
,;out sAllText
#7
how can I get this to get the formatted text from a file and then paste that text into a doc?
An old blog on QM coding and automation.

The Macro Hook
#8
Try function Copy of the range variable r.

http://msdn.microsoft.com/en-us/library ... 37718.aspx
#9
Gintaras Wrote:At first create list of files, then open files from the list.

Macro Macro1754
Code:
Copy      Help
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
str files
if(!inp(files "Please specify the location of the files:" "TestDocRetrieve" "C:\test")) ret

ARRAY(str) a
GetFilesInFolder a files "*.doc"

int i
for i 0 a.len
,out a[i]
,
,Word.Document d._getfile(a[i])
,Word.Range r
,
,;get all text
,r=d.Range
,lpstr sAllText=r.Text
,;out sAllText

I use the above code, test failed, where is the problem ?


Attached Files Image(s)
   


Forum Jump:


Users browsing this thread: 1 Guest(s)