Jump to content
Jewtus

Get Word heading structure

Recommended Posts

Jewtus

I'm trying to build a script that will grab the headers, headers, and content (normal text under a subheader) and I'm getting weird crashes with no idea why.

 

This is my code:

$oWord = ObjCreate("Word.Application")
    $oWord.Visible = True
    $oWordDoc=$oWord.Documents.Open($ddrTemplate)
    local $aFileStructure[1][3]=[["Section","Heading","Content"]]
    $Begin=False
    $BeginText=False
    $counter1=1
    $counter2=1
    For $x=1 to $oWordDoc.Paragraphs.Count
            ConsoleWrite($x&@TAB&$oWordDoc.Paragraphs($x).Range.Style.NameLocal&@TAB&$oWordDoc.Paragraphs($x).Range.Text&@CRLF)
            If $oWordDoc.Paragraphs($x).Range.Style.NameLocal = "Heading 1" then
                $Begin=True
                _ArrayAdd($aFileStructure,$counter1&"|"&$oWordDoc.Paragraphs($x).Range.Text&"|")
                $Counter1 = $Counter1 + 1
            ElseIf StringInStr($oWordDoc.Paragraphs($x).Range.Style.NameLocal,"Heading 2") and $Begin=True then ;Header2
                _ArrayAdd($aFileStructure,$counter1-1&"."&$counter2&"|"&$oWordDoc.Paragraphs($x).Range.Text&"|")
                $Counter2 = $Counter2 + 1
                $BeginText=True
            ElseIf StringInStr($oWordDoc.Paragraphs($x).Range.Style.NameLocal,"Normal") AND $BeginText=True then;Content(normal)
                _ArrayAdd($aFileStructure,$counter1-1&"."&$counter2-1&"|"&$aFileStructure[UBound($aFileStructure)-1][1]&"|"&$oWordDoc.Paragraphs($x).Range.Text)
            EndIf
    Next

I get the following types of errors:

"The requested action with this object has failed" (this is on the .NameLocal call)

"Bad variable type" (on this line: If $oWordDoc.Paragraphs($x).Range.Style.NameLocal = "Heading 1"... which I assume relates back to the .NameLocal as well)

 

Is there a simpler way to do this? Or a way to prevent the script from failing if the namelocal can't be called?

 

Share this post


Link to post
Share on other sites
water

To work with Microsoft Word I suggest to use the Word UDF.
IIRC there was a thread discussing this subject. I will have a look.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

I'm talking about this thread

 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
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

×