sparrow925

Microsoft Word Script: First Header Range Select

16 posts in this topic

Hopefully I'm overlooking a simple fix?

This simple find/replace script has hit a wrench: the script I'm using to find/replace the header/footer does not target the first page header/footer.

Any insights? Here's what I'm working with currently:

;Find/replace for new product name in the body of the document
   _Word_DocFindReplace($oDoc, "PROFILENAME", $aFileArray[$count])
   Sleep(500)

   ;Selects the header and finds/replaces there too
   $count_sections=$oDoc.Sections.Count()
   For $sec_i=1 To $count_sections
      ; select one header section
      $oDoc.Sections($sec_i).Headers(1).Range.Select()
      ; replace in header
      _Word_DocFindReplace($oDoc, "PROFILENAME", $aFileArray[$count], 2, -1)

      ; select one footer section
      $oDoc.Sections($sec_i).Footers(1).Range.Select()
      ; replace in footer
      _Word_DocFindReplace($oDoc, "PROFILENAME", $aFileArray[$count], 2, -1)
      Next

   ; restore print layout view
   $oDoc.Activewindow.View.Type = 6

Share this post


Link to post
Share on other sites



I'd bet that the sections.count is a zero based collection and you're starting at one.


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

Thanks for the idea!

Sadly, wasn't an insta-fix... switched it like so:

For $sec_i=0 To $count_sections

Share this post


Link to post
Share on other sites

According to MSDN the Sections(1).Header(wdHeaderFooterPrimary) will return a collection for all pages except the first one.You need to use the value for wdHeaderFooterFirstPage to get the first page's header.

 

Constant                                     Value
wdHeaderFooterEvenPages      3
wdHeaderFooterFirstPage         2
wdHeaderFooterPrimary            1


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

Looks like the solution! Thank you so much, out of my depth (I'm a novice+no mentor+not my code) and was at a loss as to where to even start finding the solution.

Share this post


Link to post
Share on other sites

It took forever to find what the values of those variables are, and they're not in the WordConstants.au3 file. MSDN doesn't document what the values are that I could find, found them on StackOverflow.

BTW, I find it really strange that you have to use 2 different function calls just to get all the headers/footers in a Word document, but that's Microsoft for you. :)


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

WordConstants.au3 only contains those enumerations which are referenced in the Word UDF.

Speaking of headers and footers.

Are there any functions you would like to see in the Word UDF coping with headers and footers?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Re: UDF for header/footer, just one that lets you search them all at the same time! :)

-----------------------------

So, this has been working great for me, and my project is running along swimmingly, batch editing Word files... and then, bam, this error appears about 10% of the time, with this message:

$oDoc.Sections(1).Headers(2).Range.Select()

$oDoc^ERROR

Error: Variable must be type of "Object".

I haven't been able to find anything distinguishing about the files that the program is working on when the error pops up. It will work some of the time, and then pop this error up at other points (wrt the same file). The error isn't really descriptive enough to let me know what I'm looking for, either. (I don't have a lot of experience custom coding message errors to give me useful info)

Does this look familiar to anyone?

Additionally, any techniques to troubleshoot this that I might apply here?

Edit to add: In  messing around trying to break the code, I found that this is the same error that comes up when one of my arrays has a value that renders a filepath invalid. Why would it display this error when trying to open a file that does not exist?

Edited by sparrow925

Share this post


Link to post
Share on other sites

I suspect something went wrong with _Word_DocOpen. 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Man, I am really struggling with this. After changing file names and just rewriting the _Word_DocOpen section because "Why not," I'm still getting this error on specific files. Maybe there is just something wrong with the specific Word files that I don't have the ability to detect. Still wondering why the error shows up at this particular point in the code.

 

Thanks for your time! If there are any troubleshooting techniques that might help, throw them my way.

 

...I suppose I could add my code. The error is showing at line 21 in this format.

 

Local $aFileArray1 = FileReadtoArray ("J:\SHARE\printnames.txt")
Local $aFileArray2 = FileReadtoArray ("J:\SHARE\printcodes.txt")
Local $aFileArray3 = FileReadtoArray ("J:\SHARE\profilenumber.txt")
Local $oWord = _Word_Create()

For $count = 0 To UBound($aFileArray1)-1
   ; define document varibale and open origional doc
   Local $sDocument = "J:\SHARE\"&$aFileArray3[$count]&" (2015) FINAL.doc"
   SendKeepActive("[CLASS:OpusApp]")
   Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, Default)
   Sleep(150)
   SendKeepActive("[CLASS:OpusApp]")
   Sleep(150)
   ; Find/replace for new product name in the body of the document
   _Word_DocFindReplace($oDoc, "XXXPRODUCTNAMEXXX", $aFileArray1[$count])
   Sleep(150)
   _Word_DocFindReplace($oDoc, "ZZZCODEZZZ", $aFileArray2[$count])
   Sleep(150)
   
   ; select the first header section
   $oDoc.Sections(1).Headers(2).Range.Select()
   ; replace in header
   _Word_DocFindReplace($oDoc, "XXXPRODUCTNAMEXXX", $aFileArray1[$count], 2, -1)
   Sleep(150)
   ; selects the first footer section
   $oDoc.Sections(1).Footers(2).Range.Select()
   ; replace in footer
   _Word_DocFindReplace($oDoc, "XXXPRODUCTNAMEXXX", $aFileArray1[$count], 2, -1)
   Sleep(150)
   _Word_DocFindReplace($oDoc, "ZZZCODEZZZ", $aFileArray2[$count], 2, -1)
   Sleep(150)

   ; Selects the secondary headers/footers and finds/replaces there too
   $count_sections=$oDoc.Sections.Count()
   For $sec_i=0 To $count_sections
      ; select one header section
      $oDoc.Sections($sec_i).Headers(1).Range.Select()
      ; replace in header
      _Word_DocFindReplace($oDoc, "XXXPRODUCTNAMEXXX", $aFileArray1[$count], 2, -1)
      Sleep(150)
      _Word_DocFindReplace($oDoc, "ZZZCODEZZZ", $aFileArray2[$count], 2, -1)
      Sleep(150)
      ; select one footer section
      $oDoc.Sections($sec_i).Footers(1).Range.Select()
      ; replace in footer
      _Word_DocFindReplace($oDoc, "XXXPRODUCTNAMEXXX", $aFileArray1[$count], 2, -1)
      Sleep(150)
      _Word_DocFindReplace($oDoc, "ZZZCODEZZZ", $aFileArray2[$count], 2, -1)
      Sleep(150)
      Next

   Sleep(150)
   ; restore print layout view
   $oDoc.Activewindow.View.Type = 6
   Sleep(150)
   ; save-as with new name, based on ReplaceCount, and close without saving changes to origional doc
   _Word_DocSaveAs ( $oDoc, "J:\SHARE\XNAMECHANGED\Profile "&$aFileArray3[$count]&"\"&$aFileArray2[$count]&"sdsEN.doc")
   _Word_DocSaveAs ( $oDoc, "J:\SHARE\XNAMECHANGED\PDFs\Profile "&$aFileArray3[$count]&"\"&$aFileArray2[$count]&"sdsEN.pdf", $wdFormatPDF)
   _Word_DocClose($oDoc, $WdDoNotSaveChanges)

 

Edited by sparrow925
Adding code

Share this post


Link to post
Share on other sites

Check the return value and @error after _Word_DocOpen. When @error <> 0 then there is a problem. 

Can you open this files manually?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The files open totally fine manually. Opening manually and then trying to run the code on the open file also triggers the error. But only on two specific files out of 20, all  named identially (but with varying two digit numbers)

Share this post


Link to post
Share on other sites

What is the value of @error when the file open fails?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This means: The document does not exist.

Is the document being created by another process?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Nope, I take template files created manually by a coworker and AutoIt makes ~300 versions from each initial templates. The template files are named "## (2015) FINAL" and the script cycles through the two-digit numbers from a .txt list. On 11 and 66, it fouls up. I've renamed the files to make sure that they are exactly in-pattern, even with different, arbitrary numbers, the specific files still error out. 

It's not the biggest problem in the world, I'll just do those by hand. But it is a MYSTERY! :sweating:

Edit to add: I'll keep working at problem solving on my end the next time this error comes up. I'm sure it's something simple that I am missing, as the case usually is. Thank you for your help! Now I know how to check @error. Learning = good.

Edited by sparrow925
Adding self-effacing comment

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