Jump to content

Get Word heading structure


Recommended Posts

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?

 

Link to comment
Share on other sites

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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I'm talking about this thread

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...