KickStarter15

[Solved]: Insert superscript numbers in word document before delimiters: Need Help!

15 posts in this topic

#1 ·  Posted (edited)

Hi Experts,

Good day!

I need your help in figure-outing some functions in Word UDF which I'm not good much. I tried searching for forums with the same issue I've got but sadly I could not find any.

Here is my problem:

First, I need to insert break in every section headings and replace with "hard-return/break".

Second, I want to insert numbers 1,2,3,4,5, etc... 500 in a word document paragraph under each section. But each paragraph and each word must have the same number. Example: First word1; Second word1; Third word1; and so on..... until the last word of a paragraph.

Here's what I have so far:

- Below code is to insert number "1" in first section and replace ", " with "hard-return/break".

;====================================================================================
; First Section - Insert numeric number 1 and insert Line Break before the first delimiter ", " found and then delete ", "
;====================================================================================
$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0, Default, 3) ; 3 is the word count, but i don't need to count the word how to use delimiters?
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
$oRange.Select
; Insert some text before the range
$oRange.InsertBefore("1")
$oRange.Select
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error inserting text." & @CRLF & "@error = " & @error & _
        ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Example", _
        "Nuber 1 inserted in first section.")
$oRangeFound = _Word_DocFind($oDoc, ", ")
$oRange = _Word_DocRangeSet($oDoc, $oRangeFound, Default, Default, $wdCharacter, 0)
$oRange.Delete
$oRange = _Word_DocRangeSet($oDoc, $oRangeFound, Default, Default, -1, Default)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
$oRange.InsertBreak($wdLineBreak)
MsgBox($MB_SYSTEMMODAL, "Example", "Inserted a break for first Section.")

- The following code will insert same number "1" in each word before the separator "; ". But not working in succeeding words within the same paragraph. It only added in first word.

;====================================================================================
; Paragraph Under First Section - Insert number 1 before the delimiter "; " found
;====================================================================================
$oRangeFound2 = _Word_DocFind($oDoc, "; ")
$oRange = _Word_DocRangeSet($oDoc, $oRangeFound2, Default, Default, $wdCharacter, 0)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Insert some text before the range
$oRange.InsertBefore("1")
$oRange.Select
MsgBox($MB_SYSTEMMODAL, "Example", _
        "Number 1 inserted in first word in paragraph under first Section.")


- The below code is just the same from the two above codes posted but this time, it will insert number "2". But again, number 2 will only insert in first word within the paragraph under second section.

;====================================================================================
; Second Section - Insert numeric number 2 and insert Line Break before the first delimiter ", " found and then delete ", "
;====================================================================================
$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 2, Default, 3)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Insert some text before the range
$oRange.InsertBefore("2")
$oRange.Select
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error inserting text." & @CRLF & "@error = " & @error & _
        ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Example", _
        "Number 2 inserted in start of second Section.")
$oRangeFound1 = _Word_DocFind($oDoc, ", ")
$oRange1 = _Word_DocRangeSet($oDoc, $oRangeFound1, $wdCharacter, 0)
$oRange1.Delete
$oRange1 = _Word_DocRangeSet($oDoc, $oRangeFound1, Default, Default, -1, Default)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
$oRange1.InsertBreak($wdLineBreak)
MsgBox($MB_SYSTEMMODAL, "Example", "Inserted a break for second Section.")

;====================================================================================
; Paragraph Under Second Section - Insert number 2 before the delimiter "; " found
;====================================================================================
$oRangeFound3 = _Word_DocFind($oDoc, "; ")
$oRange = _Word_DocRangeSet($oDoc, $oRangeFound3, 3, 2, Default, 2)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Example", _
        "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Insert some text after the range
$oRange.InsertAfter("2")
$oRange.Select
MsgBox($MB_SYSTEMMODAL, "Example", _
        "Number 2 inserted in first word in paragraph under second Section.")

 

Not sure how to explain this guys, so i added some screenshot for more information.

- This is the presentation of the document.

Originally presented in document.png

 

- This should be what i want to have.

Expected.png

 

Thanks in advance Experts, hope someone could help me with this.

KS15

 

Edited by KickStarter15

Share this post


Link to post
Share on other sites



#2 ·  Posted

Hi Experts,

Hope someone could explain to me if adding superscripted numbers before the delimiters if it is possible or not. As what i've found in my searching, might this function StringReplace() worked in word files and "_GUICtrlRichEdit_SetCharAttributes($oRange, "+nd")" might add superscript format in selected "$oRange". But i've tried on it and it's not working at all.

"StringReplace()" is only working in text file and "+nd" is not generating superscript in range selected.

I have another option on how to handle my problem and the only way that i could think is this StringReplace("$oDoc", "; ", "$InputNumber") and add "_GUICtrlRichEdit_SetCharAttributes($oRange, "+nd")" but I'm stacked.

 

Anyone? can someone explain to me if this is possible? Thanks in advance Experts.

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

This could be coded better but just put it together quickly, it should match your formatted document, it did for me anyway.  Hopefully the code will help.

#include <Word.au3>
Local $oWord = _Word_Create()
Local $sDocument = @ScriptDir & "\WordFile.docx"
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
Local $i = 0
For $j = 1 To $oDoc.Paragraphs.Count
    If StringInStr($oDoc.Paragraphs($j).Range.text, ", ") And $oDoc.Paragraphs($j).Range.Bold = True Then
        $i += 1
        $oRange = $oDoc.Paragraphs($j).Range.InsertBefore($i)
        $iStartRange = $oDoc.Paragraphs($j).Range.Start
        $iEndRange = $oDoc.Paragraphs($j).Range.Start + 1
        $oDoc.Range($iStartRange, $iEndRange).Font.Superscript = True

        $oRangeFind = _Word_DocFind($oDoc, ", ", -1, $oDoc.Paragraphs($j).Range)
        If Not @error Then
            $oRange = _Word_DocRangeSet($oDoc, $oRangeFind, Default, Default, $wdCharacter, 0)
            $oRange.Delete
            $oRange.InsertBreak($wdLineBreak)
        EndIf
    EndIf
    If StringInStr($oDoc.Paragraphs($j).Range.text, "; ") Then
        $oRangeFind = $oDoc.Paragraphs($j).Range
        $oRangeFind.Find.ClearFormatting
        $oRangeFind.Find.text = "; "
        $iEndRange = $oRangeFind.End
        Do
            $oRangeFind.Find.Execute
            If $oRangeFind.Find.Found Then
                $oRangeFind.Text = $i & $oRangeFind.Text
                $oRangeFind.Start = $oRangeFind.Start
                $oRangeFind.End = $oRangeFind.Start + 1
                $oRangeFind.Font.Superscript = True
                $oRangeFind.Start += 2
                $oRangeFind.End = $iEndRange
            EndIf
        Until $oRangeFind.Find.Found = False
        $oRange = $oDoc.Range($oDoc.Paragraphs($j).Range.Start, $oDoc.Paragraphs($j).Range.End - 1)
        $oRange.InsertAfter($i)
        $oRange = $oDoc.Range($oDoc.Paragraphs($j).Range.End -2, $oDoc.Paragraphs($j).Range.End)
        $oRange.Font.Superscript = True
    EndIf
Next

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

@Subz,

Thanks for the response and help Subz, it definitely worked as expected very appreciated.:D However,:sweating: if there are five paragraphs with different sections but has the same delimiters with other sections heads, it will not continue adding superscript numbers like "1, 2, 3, ..." and so on.

But I added a little bit in your code to continue the count and add numbers accordingly as sequenced from 1 to so on (depending on how many paragraphs are formatted in the document):> and it worked perfectly.

Your the best Subz, thank you so much for your time and checking my problem. Such a big help for me.

I have one more thing to ask Subz, can i copy all the section heads and paste it on the top of the document after all the insertion of numbers and formatting the document? like all in bold font will be Send("^cut") and Send("^paste") at the top of the document. I tried it but not my master peace.^_^

 

Thanks in advance.

KS15

Share this post


Link to post
Share on other sites

#5 ·  Posted

Quote

Thanks for the response and help Subz, it definitely worked as expected very appreciated.:D However,:sweating: if there are five paragraphs with different sections but has the same delimiters with other sections heads, it will not continue adding superscript numbers like "1, 2, 3, ..." and so on.

Can you give me an example?

Here is how to grab the Headings and add them to the top of the page, again messy code but hopefully it gives you something to go on.

#include <Array.au3>
#include <Word.au3>
Local $oWord = _Word_Create()
Local $sDocument = @ScriptDir & "\WordFile.docx"
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
Local $i = 0
$oWord.Selection.HomeKey($wdStory)
$oDoc.Content.InsertBefore(@CR)
Local $aHeadings[0]
For $j = 1 To $oDoc.Paragraphs.Count
    If StringInStr($oDoc.Paragraphs($j).Range.text, ", ") And $oDoc.Paragraphs($j).Range.Bold = True Then
        $i += 1
        $oRange = $oDoc.Paragraphs($j).Range.InsertBefore($i)
        $iStartRange = $oDoc.Paragraphs($j).Range.Start
        $iEndRange = $oDoc.Paragraphs($j).Range.Start + 1
        $oDoc.Range($iStartRange, $iEndRange).Font.Superscript = True

        $oRangeFind = _Word_DocFind($oDoc, ", ", -1, $oDoc.Paragraphs($j).Range)
        If Not @error Then
            _ArrayAdd($aHeadings, $oDoc.Range($iStartRange + 1, $oRangeFind.Start).Text)
            $oRange = _Word_DocRangeSet($oDoc, $oRangeFind, Default, Default, $wdCharacter, 0)
            $oRange.Delete
            $oRange.InsertBreak($wdLineBreak)
        EndIf
    EndIf
    If StringInStr($oDoc.Paragraphs($j).Range.text, "; ") Then
        $oRangeFind = $oDoc.Paragraphs($j).Range
        $oRangeFind.Find.ClearFormatting
        $oRangeFind.Find.text = "; "
        $iEndRange = $oRangeFind.End
        Do
            $oRangeFind.Find.Execute
            If $oRangeFind.Find.Found Then
                $oRangeFind.Text = $i & $oRangeFind.Text
                $oRangeFind.Start = $oRangeFind.Start
                $oRangeFind.End = $oRangeFind.Start + 1
                $oRangeFind.Font.Superscript = True
                $oRangeFind.Start += 2
                $oRangeFind.End = $iEndRange
            EndIf
        Until $oRangeFind.Find.Found = False
        $oRange = $oDoc.Range($oDoc.Paragraphs($j).Range.Start, $oDoc.Paragraphs($j).Range.End - 1)
        $oRange.InsertAfter($i)
        $oRange = $oDoc.Range($oDoc.Paragraphs($j).Range.End -2, $oDoc.Paragraphs($j).Range.End)
        $oRange.Font.Superscript = True
    EndIf
Next
$sHeadings = _ArrayToString($aHeadings, @CR)
$oWord.Selection.HomeKey($wdStory)
$oDoc.Content.InsertBefore($sHeadings)

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@Subz,

1 hour ago, Subz said:

Can you give me an example?expandpopup

 

In the document content, It was provided with five section heads with paragraph each section (sample only), if the first section heading with paragraph is "1" and the second section head with paragraph is "2" and the third section head with paragraph is "3" then the forth section and fifth section head both with paragraph must be "4" and "5" and so on.

The document contains not just five but it could reached more than 5 sections with paragraph. So, in your given code, it is working perfectly for 3 section heads with paragraph only (as what my problem raised in this forum) so I added "+1" in this code "$oRangeFind.Start += 2+1" and change the below code

From:

$oRangeFind = _Word_DocFind($oDoc, ", ", -1, $oDoc.Paragraphs($j).Range)

To: Just the number "1" was changed to "2"

$oRangeFind = _Word_DocFind($oDoc, ", ", -2, $oDoc.Paragraphs($j).Range)

And that do the trick.:sweating:

Moving forward, the sample code in copying section headings and paste it at the top, apology but it's not what i expected.:> Please see below sample:

It should something be like this:

 

Cut and Paste section heads.png

Actually, the last code sample you gave will also do the trick in adding continues numbers in each section heads with paragraph. Amazing Subz, however, for the cut and paste below is the output.:lol:

Paste.png

Edited by KickStarter15

Share this post


Link to post
Share on other sites

#7 ·  Posted

@Subz, I tried recording it in VBS macro and below is the result.

'
' AMacro1HeadingCut Macro
'
'
    Selection.MoveDown Unit:=wdParagraph, count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdParagraph, count:=1
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.MoveUp Unit:=wdParagraph, count:=2
    Selection.MoveDown Unit:=wdParagraph, count:=2, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdParagraph, count:=1
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.MoveUp Unit:=wdParagraph, count:=3
    Selection.MoveDown Unit:=wdParagraph, count:=3, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdParagraph, count:=1
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.MoveUp Unit:=wdParagraph, count:=4
    Selection.MoveDown Unit:=wdParagraph, count:=4, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdParagraph, count:=3
    Selection.MoveDown Unit:=wdParagraph, count:=2
    Selection.MoveUp Unit:=wdParagraph, count:=1
    Selection.PasteAndFormat (wdFormatOriginalFormatting)

I just thought this might help you converting it to AutoIt.^_^ I'm not good in converting this codes.:sweating:

Share this post


Link to post
Share on other sites

#8 ·  Posted

Sorry got a lot on at the moment, can you try:

#include <Array.au3>
#include <Word.au3>
Local $oWord = _Word_Create()
Local $sDocument = @ScriptDir & "\WordFile.docx"
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
Local $oFindRange, $oLastRange, $oNextRange, $i = 0
$oWord.Selection.HomeKey($wdStory)
Local $aHeadings[0]
For $j = 1 To $oDoc.Paragraphs.Count
    If StringInStr($oDoc.Paragraphs($j).Range.text, ", ") And $oDoc.Paragraphs($j).Range.Bold = True Then
        $i += 1
        If $i = 1 Then
            $oNextRange = $oDoc.Paragraphs($j + 1).Range
            $oLastRange = $oDoc.Paragraphs($i).Range
        ElseIf $i > 1 Then
            $oNextRange = $oDoc.Paragraphs($j + 1).Range
            $oDoc.Paragraphs($j).Range.Cut
            $oRange = $oDoc.Range($oLastRange.End, $oLastRange.End)
            $oRange.Paste
            $oLastRange = $oDoc.Paragraphs($i).Range
        EndIf
        $oDoc.Paragraphs($i).Range.InsertBefore($i)
        $oDoc.Range($oDoc.Paragraphs($i).Range.Start, $oDoc.Paragraphs($i).Range.Start + 1).Font.Superscript = True

        $oFindRange = _Word_DocFind($oDoc, ", ", -1, $oDoc.Paragraphs($j).Range)
        If Not @error Then
            $oRange = _Word_DocRangeSet($oDoc, $oFindRange, Default, Default, $wdCharacter, 0)
            $oRange.Delete
            $oRange.InsertBreak($wdLineBreak)
        EndIf
    EndIf
    If IsObj($oNextRange) And StringInStr($oNextRange.text, "; ") Then
        $oFindRange = $oNextRange
        $oFindRange.Find.ClearFormatting
        $oFindRange.Find.text = "; "
        $iEndRange = $oFindRange.End
        Do
            $oFindRange.Find.Execute
            If $oFindRange.Find.Found Then
                $oFindRange.Text = $i & $oFindRange.Text
                $oFindRange.Start = $oFindRange.Start
                $oFindRange.End = $oFindRange.Start + 1
                $oFindRange.Font.Superscript = True
                $oFindRange.Start += 2
                $oFindRange.End = $iEndRange
            EndIf
        Until $oFindRange.Find.Found = False
        $oDoc.Range($oDoc.Paragraphs($i + $i).Range.Start, $oDoc.Paragraphs($i + $i).Range.End - 1).InsertAfter($i)
        $oDoc.Range($oDoc.Paragraphs($i + $i).Range.End - 2, $oDoc.Paragraphs($i + $i).Range.End - 1).Font.Superscript = True
    EndIf
Next

 

1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted

Wooowww... Subz, that really amaze me. :lmao: You've got it. Thank you so much Subz. I knew you'll be back with correct solution.

How can i repay with your countless help to me. Cheerssss... Thank you so much.

Share this post


Link to post
Share on other sites

#10 ·  Posted

@Subz I have this related topic posted a while ago and might want to check on it.:>

Share this post


Link to post
Share on other sites

#11 ·  Posted

@Subz, Sorry but I was confused with Read Only mode in opened document. How can I removed this and use the active document. I tried having this FileSetAttrib($sDocument, "-R") but not working.

Share this post


Link to post
Share on other sites

#12 ·  Posted

I thought it was strange that you set the _Word_DocOpen in read only mode.  If you re-open a document that you had opened in Read Only mode previous it should ask if you want to continue in Read Only or open document normally.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Would that be possible to change the function? and use the active opened document instead?

I mean, like $oDoc.name? or current directory of opened doc?

Edited by KickStarter15

Share this post


Link to post
Share on other sites

#14 ·  Posted

You can use _Word_DocAttach

Share this post


Link to post
Share on other sites

#15 ·  Posted

Got it Subz, thank you so much. Big help.:D

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