Ahile07

Opening word in background

15 posts in this topic

Hello Guys,

I'm trying to open a document with _WordDocOpen and replace some text in it with _Word_DocFindReplace and then print with _Word_DocPrint and finally close with _Word_DocClose and kill word with _Word_Quit. Works perfectly.

My question is (can't find an answer anywhere): Can i do those commands in the background so i don't see word opening and replacing everything? Can't find anything in the proprieties of "WordDocOpen".

Thank you.

Flo

Share this post


Link to post
Share on other sites



_Word_DocOpen doesn't include support for the "visible" parameter of the Documents.Open method that the function is wrapping. To add that you'd need to add 2 parameters to this line.

Local $oDoc = $oAppl.Documents.Open($sFilePath, $bConfirmConversions, $bReadOnly, $bAddToRecentFiles, _
            $sOpenPassword, "", $bRevert, $sWritePassword, "", $iFormat)

After the $iformat parameter you'd need to add the Encoding parameter, and then either True or False for the visible parameter.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

If you use _Word_Create function first, then you can call it the first parameter as false. Then you can open a doc with _Word_DocOpen. You never see an instance of MSword because the application object is hidden. :) 

1 person likes this

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Thank you guys :)

I'll give it a go!

Share this post


Link to post
Share on other sites

Works perfectly.

Thank you

Share this post


Link to post
Share on other sites

Can anyone tell me if you have limitations of how many characthers you can send with _Word_DocFindReplace ?

I try to replace something with a quite long text but it won't do it. It gaves me an error.

Thank you in advance for your wise answers. I use the following code for replacing and it gives me error replacing the text

The value for the $inspection_equipment is = "Milos Raonic (born 1990) is a Canadian professional tennis player. He reached a career-high world No. 4 singlesranking in May 2015, as ranked by the Association of Tennis Professionals (ATP). His career highlights include a Grand Slam final at the 2016 Wimbledon Championships and two Grand Slam semifinals at the 2014 Wimbledon Championships and 2016 Australian Open. " (just a dummy text from wikipedia)

_Word_DocFindReplace($oDoc, "Eechipament", $inspection_equipment, $WdReplaceAll, 0)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example","Error replacing text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Cheers

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

P.S.

Want to mention that it works perfect if the $inspection_equipment = "test equipment" for example.

If i make the text from above shorter....works :(

Edited by Ahile07

Share this post


Link to post
Share on other sites

There seems to be a limit of 255 characters: http://gregmaxey.mvps.org/word_tip_pages/find_replace_long_string.html

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thank you @water

Do you reckon i can make it work? (VBA bit)

Edited by Ahile07

Share this post


Link to post
Share on other sites

You could test with _Word_DocFindReplace and pass "^c" as the replacement text as suggested in the web article.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This seems to work:
 

#include <MsgBoxConstants.au3>
#include <Word.au3>

Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Local $oDoc = _Word_DocOpen($oWord, "C:\Temp\Test.docx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error opening 'Test.docx'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

ClipPut("This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!This is a test!12345678")
_Word_DocFindReplace($oDoc, "Line 1", "^c", $wdReplaceOne)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error replacing text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Paragraph control character successfully replaced.")

 

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You're fast :))

Let me test it.

Cheers

Share this post


Link to post
Share on other sites

@water you're the beste mate

Cheers

Works like a charm

i owe you one :)

Share this post


Link to post
Share on other sites

:) Then simply click on the "Like this" button

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

  • Similar Content

    • MrCheese
      Merge word documents
      By MrCheese
      Hi Guys,
      Firstly, thanks for your  help in the past.
      I have a new activity I need to accomplish.
      In summary:
      * need to read a cell in excel (containing a file name)
      * open the file name in word (as its a word document
      *copy the word document
      *paste the word document into the master document
      *read next cell in excel
      ... and repeat until you reach the bottom of the column.
       
      I can read cells open workbooks etc.
      But as far as copying and pasting in word - where is the best place to start, and what functions should I be looking at. Or even if autoit is the right system to use?
      Thanks
       
       
       
    • kcvinu
      How to make a newline in Word with COM
      By kcvinu
      Hi all (Especially @water)
      I wonder how to do this task in word from autoit.
      Assume that i pasted some text into word with this code.
      Local $oWord = ObjGet("","Word.Application") Local $wRangeObj = _Word_DocRangeSet($oWord, 0) Local $data = ClipGet() $wRangeObj.Text = $data $wRangeObj.Font.Bold = True So far so good. Now, i need to enter a new line in word and turn the Font.Bold = False.  
      Currently, i did it with activating the word window and Send() function. But i would like to do it with the help of COM object. How can i do that ? 
    • TheDcoder
      Cannot save word document - Strange/Unknown error
      By TheDcoder
      Hello, I am trying to save an word document but it won't work , This is my REALLY simple script:
      #include <Word.au3> Global $oWord = _Word_Create(False, True) _Word_DocSaveAs($oWord) MsgBox(0, @error, @extended) And my MsgBox:

      I know that its a COM error because @error is set 2... but what about the COM error code? I cannot find anything related to -2147352570!
    • aiter
      word com error trapping possible bug in udf
      By aiter
      I am testing intercepting com errors in Word and I have found a possible bug in the UDF, it might not be a bug it just means a workaround.
      I have found the com error bug in _Word_Create
      Func _Word_Create($bVisible = Default, $bForceNew = Default) Local $oAppl, $bApplCloseOnQuit = False If $bVisible = Default Then $bVisible = True If $bForceNew = Default Then $bForceNew = False If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application") <------- here is the line which causees com error This happens in the following example
      #include <Word.au3> main() func main() local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oWord = _Word_Create() $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True) $myrange = $oDoc.Range $myrange.Select sleep(3000) _Word_DocClose($oDoc) _Word_Quit($oWord) endfunc ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc This script causes a com error to occur and it has the following error
      test.au3 (53) : ==> COM Error intercepted ! err.number is: 0x80020006 err.windescription: Unknown name. err.description is: err.source is: err.helpfile is: err.helpcontext is: err.lastdllerror is: 0 err.scriptline is: 53 err.retcode is: 0x00000000 Line 53 is
      If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application") as I mentioned above.
      When I force a new instance of Word, no error occurs eg
      #include <Word.au3> main() func main() local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oWord = _Word_Create(True,True) $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True) $myrange = $oDoc.Range $myrange.Select sleep(3000) _Word_DocClose($oDoc) _Word_Quit($oWord) endfunc So it seems that if I want to open up  existing Word instance (use _Word_Create() ) I will have to create my own _Word_Open to bypass this problem.
      (If Word is already open the problem will not occur)
      Comment?
       
    • aiter
      _Excel_RangeFind fails
      By aiter
      I have tried to use _Excel_RangFind, but it fails. It even fails in the example in the help file (see example 1).
      This the error
      >Running AU3Check (3.3.14.2) from:C:\Program Files (x86)\AutoIt3 input:C:\Program Files (x86)\AutoIt3\Examples\Helpfile\_Excel_RangeFind.au3 +>12:09:10 AU3Check ended.rc:0 >Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\_Excel_RangeFind.au3" --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop "C:\Program Files (x86)\AutoIt3\Include\Excel.au3" (656) : ==> The requested action with this object has failed.: $aResult[$iIndex][1] = $oMatch.Name.Name $aResult[$iIndex][1] = $oMatch^ ERROR My own code fails with a similar message
      Local $aResult = _Excel_RangeFind($oWorkbook, "john") _ArrayDisplay($aResult) Help please.