Jump to content

Microsoft Word Help


Recommended Posts

Is there any way I can read the number in input box on the tool bar named RichEdit20W1? Basically I want to make a script that goes to the last number... reads its value, store it and the goes back to the beginning and then print each page until it reachs the store number.

EDIT* This part is want to read is where the "10" is located on the bottom toolbar... I forgot to mark it.

post-59549-12864774791304_thumb.jpg

Edited by JayFran
Link to comment
Share on other sites

If you're in a mail merge...

MsgBox(0, "", "Active record is " & $oWord.Activedocument.MailMerge.DataSource.ActiveRecord)

should work, just change $oWord to whatever your word object variable is.

Edit/Add: To move on to the next/last record:

you could add constants:

Const $wdNextRecord = -2
Const $wdLastRecord = -5

Then,

;To go to the next record
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdNextRecord ;Next record

;OR to go to the last record then store the record number in $lastRecord

$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdLastRecord ;Last record
$lastRecord = $oWord.Activedocument.MailMerge.DataSource.ActiveRecord   ;Store number of last record
Edited by MrMitchell
Link to comment
Share on other sites

If you're in a mail merge...

MsgBox(0, "", "Active record is " & $oWord.Activedocument.MailMerge.DataSource.ActiveRecord)

should work, just change $oWord to whatever your word object variable is.

Edit/Add: To move on to the next/last record:

you could add constants:

Const $wdNextRecord = -2
Const $wdLastRecord = -5

Then,

;To go to the next record
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdNextRecord ;Next record

;OR to go to the last record then store the record number in $lastRecord

$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdLastRecord ;Last record
$lastRecord = $oWord.Activedocument.MailMerge.DataSource.ActiveRecord   ;Store number of last record

Thanks. It is mail merge I am working with. But Im still having some issues nothing is happening when I focus on the input. And try to read the number and when I try to get it to look at the last record. Here is what I have so far.

#include <GuiMenu.au3>
#include <Array.au3>
#include <IE.au3>
#Include <GuiTreeView.au3>
#include <GuiToolbar.au3>
Opt("WinTitleMatchMode", 2) 
Opt("WinDetectHiddenText", 1)


$handle = WinGetHandle("BuildSheetMergeALL.doc (Read-Only) - Microsoft Word", "")
WinActivate($handle)
Sleep(1000)
WinWaitActive($handle)
$t_print = ControlFocus($handle, "", "[CLASS:MsoCommandBar; INSTANCE:1]")
Sleep(1000)
$try = ControlSend("Build", "", "[CLASS:MsoCommandBar; INSTANCE:1]", "{Right 2}") 
Sleep(1500)

This gets me to the mail merge toolbar and I made it go to the right twice just to make sure. I tried to add your snippet to my code with no luck.

;<---------------------Mail Merge Input Hotfix------------------------>
WinWaitActive($handle)
$t_print = ControlFocus($handle, "", "[CLASS:RichEdit20W; INSTANCE:2]")
Sleep(1000)
$try = ControlSend("Build", "", "[CLASS:RichEdit20W; INSTANCE:2]", "") ; Jump first instance to beginning of text
Sleep(1500)
;<---------------------End Hotfix-------------------------->

And this is the part of the code where it actually focuses on the mail merge input to read the value inside. Samething I tries to use the msgbox to display the number but i received error messages with all the combinations i tried.

Link to comment
Share on other sites

You should be using Word COM instead of keystroke manipulation for this...

Check out this link: http://dundats.mvps.org/help/, on the left nav bar, scroll down and expand "User Defined Functions Reference", then scroll down and go to Word Management. Look at the function _WordAttach(). For an intro to COM, look on the nav bar for Obj/COM Reference.

Below your line:

$handle = WinGetHandle("BuildSheetMergeALL.doc (Read-Only) - Microsoft Word", "")

Try adding this just after:

$oWord = _WordAttach($handle, "HWND")

Now you can manipulate Word using $oWord and those commands I provided should work. It might need tweaking but we'll help ya out. The more of your code you can provide the better...just the part where you deal with Word, that is.

Using this method, instead of actually focusing the text field of the record number, then reading the value, you're basically talking directly to word and saying "hey, what record am I on?". Likewise, you can send commands directly to word to control which record to go to, and so on...

Link to comment
Share on other sites

You should be using Word COM instead of keystroke manipulation for this...

Check out this link: http://dundats.mvps.org/help/, on the left nav bar, scroll down and expand "User Defined Functions Reference", then scroll down and go to Word Management. Look at the function _WordAttach(). For an intro to COM, look on the nav bar for Obj/COM Reference.

Below your line:

$handle = WinGetHandle("BuildSheetMergeALL.doc (Read-Only) - Microsoft Word", "")

Try adding this just after:

$oWord = _WordAttach($handle, "HWND")

Now you can manipulate Word using $oWord and those commands I provided should work. It might need tweaking but we'll help ya out. The more of your code you can provide the better...just the part where you deal with Word, that is.

Using this method, instead of actually focusing the text field of the record number, then reading the value, you're basically talking directly to word and saying "hey, what record am I on?". Likewise, you can send commands directly to word to control which record to go to, and so on...

Thank you, I appreciate it alot.

I've written something up, it finds the window, activates, goes to the last record, stores it, then goes back to the first record and prints... Now when it goes to the next record and tries to print again I get an error from the _WordDocPrint function.

#include <Word.au3>

Opt("WinTitleMatchMode", 2) 
Opt("WinDetectHiddenText", 1)

Const $wdNextRecord = -2
Const $wdLastRecord = -5
Const $wdFirstRecord = 0

$handle = WinGetHandle("BuildSheetMergeALL.doc (Read-Only) - Microsoft Word", "")
$oWord = _WordAttach($handle, "HWND")
WinActivate($handle)
;$oIE = _IEAttach("BUILD INFORMATION", "text")
Sleep(1000)

$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdLastRecord ;Last record
$lastRecord = $oWord.Activedocument.MailMerge.DataSource.ActiveRecord   ;Store number of last record
Sleep (500)
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdFirstRecord ;First record
Sleep (500)
$oDoc = _WordAttach("BUILD INFORMATION", "text")
$oWordDoc = _WordDocGetCollection($oWord, 0)
Sleep (500)

Do
Sleep(1500)
$try = _WordDocPrint($oWordDoc)
Sleep (500)
$bp = WinWaitActive("Microsoft Office Word", "The margins of section 1 are set outside the printable area of the page.  Do you want to continue?")
Sleep (500)
ControlClick($bp, "", "[CLASS:Button; INSTANCE:1]")
WinActive($handle)
Sleep (500)

;To go to the next record
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdNextRecord ;Next record

Until $wdNextRecord = $wdLastRecord
Link to comment
Share on other sites

The code below seems to fix the problem... But it won't click on the "OK" button for it to print inside of the loop.

#include <Word.au3>

Opt("WinTitleMatchMode", 2) 
Opt("WinDetectHiddenText", 1)

Const $wdNextRecord = -2
Const $wdLastRecord = -5
Const $wdFirstRecord = 0

$handle = WinGetHandle("BuildSheetMergeALL.doc (Read-Only) - Microsoft Word", "")
$oWord = _WordAttach($handle, "HWND")
WinActivate($handle)
;$oIE = _IEAttach("BUILD INFORMATION", "text")
Sleep(1000)

$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdLastRecord ;Last record
$lastRecord = $oWord.Activedocument.MailMerge.DataSource.ActiveRecord   ;Store number of last record
Sleep (500)
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdFirstRecord ;First record
Sleep (500)

For $i = 1 To $lastrecord

Do
$oDoc = _WordAttach("BUILD INFORMATION", "text")
$oWordDoc = _WordDocGetCollection($oWord, 0)
$try = _WordDocPrint($oWordDoc)
Sleep (1500)
$bp = WinWaitActive("Microsoft Office Word", "The margins of section 1 are set outside the printable area of the page.  Do you want to continue?")
Sleep (800)
ControlClick("Microsoft Office Word", "", "[CLASS:Button; INSTANCE:1;]")
WinActive($handle)
Sleep (500)

;To go to the next record
$oWord.Activedocument.MailMerge.DataSource.ActiveRecord = $wdNextRecord ;Next record
$oWordDoc = _WordDocGetCollection($oWord, 0)
If $lastrecord = $lastrecord Then ExitLoop
Until MsgBox(0, "TICKETS", "DONE!")
Next
Edited by JayFran
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...