Jump to content
KickStarter15

[Solved] Merging Document and Images into one MSword document.

Recommended Posts

KickStarter15

Hi Experts,

Just want to ask some question if autoIt can do merging for MSword .docx, .doc, .jpg/.tiff/.png into one MSword document?

Example: I have one .docx word contains first data and .doc word contains second data and some images separated and I want these files to be merged as one MSword document only. Let's say first data, second data and the images into one document. Can that be done?

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

I think you would have to add the images manually but to merge a number of word documents you can use something like:

#include <File.au3>
#include <Word.au3>
Local $aWordDocs = _FileListToArrayRec("C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras", "*.doc*", 1, 1, 0, 2)
Local $bDoc = False, $oWordDoc
Local $oWord = _Word_Create()

For $i = 1 To $aWordDocs[0]
    If $bDoc Then
        $oWordDoc.Bookmarks("\EndOfDoc").Range.InsertBreak($WdPageBreak)
    Else
        $oWordDoc = $oWord.Documents.Add($aWordDocs[$i])
        $bDoc = True
    EndIf
    $oWordDoc.Bookmarks("\EndOfDoc").Range.InsertFile($aWordDocs[$i])
Next

 

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

Thanks, it work but I need to select the document that I need to merged. I have this old script that I have this is old version and the problem of this code is I can only merge one file at a time. All I need is to select multiple document to merged and be sequence.

Here's my old code:

#include <GuiConstantsEx.au3>
#include <GuiListBox.au3>
#include <GuiConstantsEx.au3>
#include <Array.au3>
#include <File.au3>
#include <ColorConstantS.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <Word.au3>
#include <File.au3>

_WordErrorHandlerRegister()
Selectapps()
Func Selectapps()
$file = "C:\Users\" & @UserName & "\AppData\Roaming\Microsoft\Templates\Path.ini"
FileOpen($file, 0)

For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
Next
FileClose($file)
$Count = 0
    GUICreate("Merge File(s)", 400, 210, 350, 250)
    GUISetBkColor($COLOR_WHITE)
    $sourceFolder = GUICtrlCreateInput ($line, 10, 5, 310, 20)
    $ListBox2 = GUICtrlCreateList("", 10, 40, 310, 150)
    $ItemAdd = GUICtrlCreateButton("Path file", 325, 3, 70, 25)
    $RemoveList = GUICtrlCreateButton("Remove", 325, 40, 70, 45)
    $MergeFile = GUICtrlCreateButton("Merge File", 325, 85, 70, 40)
    $Cancel = GUICtrlCreateButton("Cancel", 325, 140, 70, 40)
    GUICtrlCreateLabel("Info: Select file to merge!", 5, 195)
    GUICtrlSetFont(2, 9, 999, 0, "Calibri")
    GUICtrlSetColor(-1, 0xC0C0C0)
    GUISetState(@SW_SHOW)

      $sFolder = ControlGetText("", "", "Edit1")
       Local $FileList = _FileListToArray($sFolder, "*.doc*")
       For $i = 1 To $FileList[0]
         GUICtrlSetData($ListBox2, $FileList[$i])
      Next

    $Count1 = 0
While 1
   $button = GUIGetMsg()
    Switch $button
       Case $ItemAdd
        $sFolder = ControlGetText("", "", "Edit1")
       Local $FileList = _FileListToArray($sFolder, "*.*")
       For $i = 1 To $FileList[0]
         GUICtrlSetData($ListBox2, $FileList[$i])
      Next
      Case $GUI_EVENT_ClOSE, $Cancel
      ExitLoop
    EndSwitch
        If $button = $GUI_EVENT_ClOSE Then
            ExitLoop
        EndIf
        If $button = $RemoveList Then
            $leftmoveitem = GUICtrlRead($ListBox2)
            If StringStripWS($leftmoveitem, 8) Then
                ConsoleWrite($leftmoveitem & @CRLF)
                GUICtrlSetData($ListBox2, $leftmoveitem)
                $iSel = _GUICtrlListBox_GetCurSel($ListBox2)
                _GUICtrlListBox_DeleteString($ListBox2, $iSel)
                $Count1 = $Count + 1
            EndIf
         EndIf
     Switch $button
     Case $MergeFile
      $sPath2 = GUICtrlRead($sourceFolder) & "\" & GUICtrlRead($ListBox2)
      $oWord = _WordCreate($line, 0, 1)
      If GUICtrlRead($sourceFolder) & "\" & GUICtrlRead($ListBox2) = "*.jpg" Then
         MsgBox (64, "Error!", "", "")
      Exit
      EndIf

      $oWordApp = _WordCreate($sourceFolder, 1)
      $oShape.Range.InsertAfter (@CRLF)
      With $oWord.Selection
          .EndKey(6)
          .InsertBreak(7)
          .InsertFile($sPath2)
      EndWith
      MsgBox(64, "Completed", "Merged ''" & GUICtrlRead($ListBox2) & "'' in current file!")
          $leftmoveitem = GUICtrlRead($ListBox2)
       If StringStripWS($leftmoveitem, 8) Then
          ConsoleWrite($leftmoveitem & @CRLF)
          GUICtrlSetData($ListBox2, $leftmoveitem)
          $iSel = _GUICtrlListBox_GetCurSel($ListBox2)
          _GUICtrlListBox_DeleteString($ListBox2, $iSel)
          $Count1 = $Count + 1
       EndIf
     EndSwitch

WEnd
EndFunc

I don't know if this is perfect but image is not merging, and only 1 file at a time. I need to select multiple document to merge. Sooner or later, I will add button up and down to sequence the files for merging.

Hope you can check on this.:sweating:


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Are you still using an old version of AutoIt, Still notice your Word udf is out of date.

Share this post


Link to post
Share on other sites
KickStarter15

Nope, I already have the latest version, that code was coded from my old version before long time ago and I want to improvise the features in it if possible using the latest version.^_^


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Maybe something like the following:

nb: If you Add one file at a time you can select the order of the merge, tested with the following file types and merged into a single document:

  • 1 x doc
  • 2 x docx
  • 1 x jpg
  • 1 x png
  • 1 x tif
#include <GuiConstantsEx.au3>
#include <GuiListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Word.au3>

Selectapps()
Func Selectapps()
    GUICreate("Merge File(s)", 400, 210, 350, 250)
        GUISetBkColor(0xFFFFFF)
    Local $idMergeDoc = GUICtrlCreateInput (@ScriptDir & "\MergeDoc.doc", 10, 10, 310, 20)
    Local $idBrowse = GUICtrlCreateButton("Browse...", 325, 5, 70, 30)
    Local $idListBox = GUICtrlCreateList("", 10, 40, 310, 145, BitOR($WS_BORDER, $WS_VSCROLL))
    Local $idAddItem = GUICtrlCreateButton("Add", 325, 40, 70, 30)
    Local $idRemoveItem = GUICtrlCreateButton("Remove", 325, 75, 70, 30)
    Local $idMergeFiles = GUICtrlCreateButton("Merge File", 325, 110, 70, 30)
    Local $idCloseGui = GUICtrlCreateButton("Cancel", 325, 145, 70, 30)
    GUICtrlCreateLabel("Info:", 5, 190, 25, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    Local $idStatus = GUICtrlCreateLabel("Select files to merge!", 30, 190, 360, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_ClOSE, $idCloseGui
                ExitLoop
            Case $idBrowse
                Local $sFileSave = FileSaveDialog("Select Merge Document File Path", @ScriptDir, "Word Doc(*.doc;*.docx)", 18, "MergeDoc.docx")
                If @error Then ContinueLoop
                GUICtrlSetData($idMergeDoc, $sFileSave)
            Case $idAddItem
                Local $sAddFiles = FileOpenDialog("Hold down Ctrl or Shift to choose multiple files.", @ScriptDir, "Merge Files(*.doc;*.docx;*.jpg;*.jpeg;*.tif;*.tiff;*.png)", 7)
                If @error Then ContinueLoop
                If StringInStr($sAddFiles, "|") Then
                    Local $aAddFiles = StringSplit($sAddFiles, "|")
                    For $i = 2 To $aAddFiles[0]
                        _GUICtrlListBox_AddString($idListBox, $aAddFiles[1] & "\" & $aAddFiles[$i])
                    Next
                Else
                    _GUICtrlListBox_AddString($idListBox, $sAddFiles)
                EndIf
            Case $idRemoveItem
                _GUICtrlListBox_DeleteString($idListBox, _GUICtrlListBox_GetCurSel($idListBox))
            Case $idMergeFiles
                Local $bDocStart = True, $bDocAdd = False
                $iCount = _GUICtrlListBox_GetCount($idListBox)
                    If $iCount = 0 Then ContinueLoop
                GUICtrlSetState($idAddItem, $GUI_DISABLE)
                GUICtrlSetState($idBrowse, $GUI_DISABLE)
                GUICtrlSetState($idRemoveItem, $GUI_DISABLE)
                GUICtrlSetState($idMergeFiles, $GUI_DISABLE)
                $oWord = _Word_Create(False, True)
                $oWordDoc = _Word_DocAdd($oWord)
                For $i = 0 To $iCount
                    $iFileExtension = _FileExtension(_GUICtrlListBox_GetText($idListBox, $i))
                        If $iFileExtension = -1 Then ContinueLoop
                    GUICtrlSetData($idStatus, "Merging " & _GUICtrlListBox_GetText($idListBox, $i))
                    If $bDocStart Then
                        $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListBox, $i))
                        $bDocStart = False
                        ContinueLoop
                    Else
                        If $bDocAdd Then $oWordDoc.Bookmarks("\EndofDoc").Range.InsertBreak($WdPageBreak)
                    EndIf
                    $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListBox, $i))
                Next
                GUICtrlSetState($idAddItem, $GUI_ENABLE)
                GUICtrlSetState($idBrowse, $GUI_ENABLE)
                GUICtrlSetState($idRemoveItem, $GUI_ENABLE)
                GUICtrlSetState($idMergeFiles, $GUI_ENABLE)
                _Word_DocSaveAs($oWordDoc, GUICtrlRead($idMergeDoc))
                _Word_DocClose($oWordDoc)
                _Word_Quit($oWord)
                GUICtrlSetData($idStatus, "Select files to merge!")
                MsgBox(64, "Completed", "Merged Completed!")
        EndSwitch
    WEnd
EndFunc

Func _MergeDocuments($_oWordDoc, $_iFileExtension, $_sFileName)
    Switch $_iFileExtension
        Case 1
            $_oWordDoc.Bookmarks("\EndOfDoc").Range.InsertFile($_sFileName)
            Return True
        Case 2
            _Word_DocPictureAdd($_oWordDoc, $_sFileName, False, True, $_oWordDoc.Bookmarks("\EndOfDoc").Range)
            Return True
        Case Else
            Return False
    EndSwitch
EndFunc
Func _FileExtension($_sFileName)
    Local $iFileExtension = StringInStr($_sFileName, ".", 0, -1)
        If $iFileExtension = 0 Then Return -1
    Local $sFileExtension = StringTrimLeft($_sFileName, $iFileExtension)
    Switch $sFileExtension
        Case "doc", "docx"
            Return 1
        Case "jpg", "jpeg", "tif", "tiff", "png"
            Return 2
    EndSwitch
    Return -1
EndFunc

 

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

Thanks, it really merged the files. But I need to merge them in the active document that is open. Like, I have one document that is open and I noticed that this document has missing information found only in the second document and images were separated as well. So, I'l select the missing file and images from that GUI and merge them in the active document/opened document.


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

I want make it like the below which the old script does. It shows all the document file found from the path given and merge them into an active document that is open. This old script will only select one file at a time.

image.png.ce5207a09af3edeabd1fb53faddc9604.png

 

And I want it to be like this, that when I selected a file I can add the file to the below and the Up and Down buttons will be the one to sequence them according to my desired.

image.png.8d0b9a0834237888493508abe3a3a72d.png

EDIT: Oppss!.. missed to add, that the button "Path File" is where the active document path.:sweating:

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Please try:

#include <Array.au3>
#include <File.au3>
#include <GuiComboBox.au3>
#include <GuiConstantsEx.au3>
#include <GuiEdit.au3>
#include <GuiListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Word.au3>

Global $idListDocs, $idListItems, $idItemUp, $idItemDown, $iLastItemSelect

Selectapps()

Func Selectapps()
    Local $bDocAttach = True
    Local $oWord = _Word_Create(True, False)
    GUICreate("Merge File(s)", 400, 395, 350, 250)
        GUISetBkColor(0xFFFFFF)
    Local $idSelectDoc = GUICtrlCreateCombo("Select New or Opened Document", 10, 10, 310, 20)
    Local $oDocs = _Word_DocGet($oWord)
        If @error Then
            $bDocAttach = False
        Else
            _GUICtrlComboBox_BeginUpdate($idSelectDoc)
            For $oDoc In $oDocs
                _GUICtrlComboBox_AddString($idSelectDoc, $oDoc.FullName)
            Next
            _GUICtrlComboBox_EndUpdate($idSelectDoc)
        EndIf
    Local $idBrowseDoc = GUICtrlCreateButton("Open...", 325, 9, 70, 23)
    $idListDocs = GUICtrlCreateList("", 10, 60, 310, 150, BitOR($WS_BORDER, $WS_VSCROLL))
    Local $idSelectFolder = GUICtrlCreateInput(@ScriptDir, 10, 35, 310, 20)
        _GUICtrlEdit_SetCueBanner($idSelectFolder, "Select Folder")
        _AddSelectFolder(GUICtrlRead($idSelectFolder))
    Local $idBrowseFolder = GUICtrlCreateButton("Browse...", 325, 34, 70, 23)
    Local $idAddItem = GUICtrlCreateButton("Add", 325, 60, 70, 30)
    Local $idClearItem = GUICtrlCreateButton("Clear Item", 325, 95, 70, 30)
    Local $idClearAll = GUICtrlCreateButton("Clear All", 325, 130, 70, 30)
    $idListItems = GUICtrlCreateList("", 10, 215, 310, 150, BitOR($WS_BORDER, $WS_VSCROLL))
    $idItemUp = GUICtrlCreateButton("Up", 325, 215, 70, 20)
    $idItemDown = GUICtrlCreateButton("Down", 325, 240, 70, 20)
    Local $idRemoveItem = GUICtrlCreateButton("Remove", 325, 265, 70, 30)
    Local $idMergeFiles = GUICtrlCreateButton("Merge File", 325, 300, 70, 30)
    Local $idCloseGui = GUICtrlCreateButton("Cancel", 325, 335, 70, 30)
    GUICtrlCreateLabel("Info:", 5, 370, 25, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    Local $idStatus = GUICtrlCreateLabel("Select files to merge!", 30, 370, 360, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    GUISetState(@SW_SHOW)

    AdlibRegister("_ControlUpdate")

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_ClOSE, $idCloseGui
                ExitLoop
            Case $idBrowseDoc
                Local $sSelectDoc = FileOpenDialog("Select Merge Document File Path", @ScriptDir, "Word Doc(*.doc;*.docx)", 3)
                If @error Then ContinueLoop
                GUICtrlSetData($idSelectDoc, $sSelectDoc)
            Case $idBrowseFolder
                Local $sBrowseFolder = FileSelectFolder ("Select Folder with Documents to merge", "", 4, @ScriptDir)
                If @error Then ContinueLoop
                _AddSelectFolder($sBrowseFolder)
            Case $idAddItem
                $iListDocs = _GUICtrlListBox_GetCurSel($idListDocs)
                    If $iListDocs = -1 Then ContinueLoop
                _GUICtrlListBox_AddString($idListItems, _GUICtrlListBox_GetText($idListDocs, $iListDocs))
            Case $idClearItem
                If _GUICtrlListBox_GetCurSel($idListDocs) = -1 Then ContinueLoop
                _GUICtrlListBox_DeleteString($idListDocs, _GUICtrlListBox_GetCurSel($idListItems))
            Case $idClearAll
                GUICtrlSetData($idSelectFolder, "")
                _GUICtrlListBox_ResetContent($idListDocs)
            Case $idItemUp
                _ListBoxDirect($idListItems, "Up")
            Case $idItemDown
                _ListBoxDirect($idListItems, "Down")
            Case $idRemoveItem
                If _GUICtrlListBox_GetCurSel($idListItems) = -1 Then ContinueLoop
                _GUICtrlListBox_DeleteString($idListItems, _GUICtrlListBox_GetCurSel($idListItems))
            Case $idMergeFiles
                If FileExists(GUICtrlRead($idSelectDoc)) = 0 Then ContinueLoop
                Local $bDocStart = True, $bDocAdd = False
                $iCount = _GUICtrlListBox_GetCount($idListItems)
                    If $iCount <= 0 Then ContinueLoop
                GUICtrlSetState($idAddItem, $GUI_DISABLE)
                GUICtrlSetState($idBrowseFolder, $GUI_DISABLE)
                GUICtrlSetState($idItemUp, $GUI_DISABLE)
                GUICtrlSetState($idItemDown, $GUI_DISABLE)
                GUICtrlSetState($idRemoveItem, $GUI_DISABLE)
                GUICtrlSetState($idMergeFiles, $GUI_DISABLE)
                If $bDocAttach = False Then
                    $oWordDoc = _Word_DocOpen($oWord, GUICtrlRead($idSelectDoc))
                Else
                    $oWordDoc = _Word_DocAttach($oWord, GUICtrlRead($idSelectDoc))
                EndIf
                For $i = 0 To $iCount
                    $iFileExtension = _FileExtension(_GUICtrlListBox_GetText($idListItems, $i))
                        If $iFileExtension = -1 Then ContinueLoop
                    GUICtrlSetData($idStatus, "Merging " & _GUICtrlListBox_GetText($idListItems, $i))
                    If $bDocStart Then
                        $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListItems, $i))
                        $bDocStart = False
                        ContinueLoop
                    Else
                        If $bDocAdd Then $oWordDoc.Bookmarks("\EndofDoc").Range.InsertBreak($WdPageBreak)
                    EndIf
                    $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListItems, $i))
                Next
                _GUICtrlListBox_ResetContent($idListItems)
                GUICtrlSetState($idAddItem, $GUI_ENABLE)
                GUICtrlSetState($idBrowseFolder, $GUI_ENABLE)
                GUICtrlSetState($idRemoveItem, $GUI_ENABLE)
                GUICtrlSetState($idMergeFiles, $GUI_ENABLE)
                _Word_DocSave($oWordDoc)
;~              _Word_DocSaveAs($oWordDoc, GUICtrlRead($idSelectDoc))
;~              _Word_DocClose($oWordDoc)
;~              _Word_Quit($oWord)
                GUICtrlSetData($idStatus, "Select files to merge!")
                MsgBox(64, "Completed", "Merged Completed!")
        EndSwitch
    WEnd
EndFunc

Func _ListBoxDirect($_hListBox, $_sListBoxDirect)
    Local $iListItemSelect = _GUICtrlListBox_GetCurSel($_hListBox)
    Local $iListItemCount = _GUICtrlListBox_GetCount($idListItems) - 1
    Local $sListItemSelect = _GUICtrlListBox_GetText($_hListBox, $iListItemSelect)
    Switch $_sListBoxDirect
        Case "Up"
            If $iListItemSelect <= 0 Then Return
            _GUICtrlListBox_DeleteString($_hListBox, $iListItemSelect)
            _GUICtrlListBox_InsertString($_hListBox, $sListItemSelect, $iListItemSelect - 1)
            _GUICtrlListBox_SetCurSel($_hListBox, $iListItemSelect - 1)
        Case "Down"
            If $iListItemSelect >= $iListItemCount Then Return
            _GUICtrlListBox_DeleteString($_hListBox, $iListItemSelect)
            _GUICtrlListBox_InsertString($_hListBox, $sListItemSelect, $iListItemSelect + 1)
            _GUICtrlListBox_SetCurSel($_hListBox, $iListItemSelect + 1)
        Case Else
            Return
    EndSwitch
EndFunc

Func _ControlUpdate()
    Local $iListItemSelect = _GUICtrlListBox_GetCurSel($idListItems)
        If $iLastItemSelect = $iListItemSelect Then Return
    Local $iListItemCount = _GUICtrlListBox_GetCount($idListItems) - 1
        $iLastItemSelect = $iListItemSelect
    Switch $iListItemSelect
        Case -1
            If $iListItemCount = 0 Or $iListItemCount = -1 Then
                GUICtrlSetState($idItemDown, $GUI_DISABLE)
                GUICtrlSetState($idItemUp, $GUI_DISABLE)
            EndIf
        Case 0
            GUICtrlSetState($idItemUp, $GUI_DISABLE)
            If $iListItemCount > 0 Then GUICtrlSetState($idItemDown, $GUI_ENABLE)
        Case $iListItemCount
            GUICtrlSetState($idItemDown, $GUI_DISABLE)
            If $iListItemSelect > 0 Then GUICtrlSetState($idItemUp, $GUI_ENABLE)
        Case Else
            GUICtrlSetState($idItemUp, $GUI_ENABLE)
            GUICtrlSetState($idItemDown, $GUI_ENABLE)
    EndSwitch
EndFunc

Func _AddSelectFolder($_sFolderPath)
    Local $aFolderList = _FileListToArrayRec($_sFolderPath, "*.doc;*.docx;*.jpg;*.jpeg;*.png;*.tif;*.tiff|~*.doc;~*.docx", 1, 0, 0, 2)
    If @error Then Return ""
    For $i = 1 To $aFolderList[0]
        _GUICtrlListBox_AddString($idListDocs, $aFolderList[$i])
    Next
EndFunc

Func _MergeDocuments($_oWordDoc, $_iFileExtension, $_sFileName)
    Switch $_iFileExtension
        Case 1
            $_oWordDoc.Bookmarks("\EndOfDoc").Range.InsertFile($_sFileName)
            Return True
        Case 2
            _Word_DocPictureAdd($_oWordDoc, $_sFileName, False, True, $_oWordDoc.Bookmarks("\EndOfDoc").Range)
            Return True
        Case Else
            Return False
    EndSwitch
EndFunc
Func _FileExtension($_sFileName)
    Local $iFileExtension = StringInStr($_sFileName, ".", 0, -1)
        If $iFileExtension = 0 Then Return -1
    Local $sFileExtension = StringTrimLeft($_sFileName, $iFileExtension)
    Switch $sFileExtension
        Case "doc", "docx"
            Return 1
        Case "jpg", "jpeg", "tif", "tiff", "png"
            Return 2
    EndSwitch
    Return -1
EndFunc

 

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

Wow!!!!!:lmao:... Thanks Subz, amazing...:>

I only have few suggestions:sweating:, I tried checking you code but I can't trace where to start regarding my suggestion. Opss!:D

1st: Please can you remove the path shown in the GUI? can you show only the files that was found from the active document/path?

image.png.43ddcea33697dce9f222f97c2312ffc8.png

2nd: Instead of browsing the files to merged, can you have like path the file automatically where it was opened? and FileReadLine() for the path? Like the below what I did is (using macro vba) once they click on the link found in their tools in MSword (addins tool), it will create the path.ini (getting the active document path) below and then call the merging tool and the merging tool will check on the path.ini where the active document was path. After checking the line in path.ini, all files found from that path will be shown to the GUI, the same as what you made but this time not doing the browsing. Can that be done?:>

Func Selectapps()
$file = "C:\Users\" & @UserName & "\AppData\Roaming\Microsoft\Templates\Path.ini"
FileOpen($file, 0)

For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
Next
FileClose($file)
$Count = 0
;code here
EndFunc

 

Thanks  in advance!

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Your png doesn't display for some reason, also can you post an example of the Path.ini file?

Share this post


Link to post
Share on other sites
KickStarter15

When testing your code, everything is perfect, png is fine and was merged to the active document. The line in Path.ini is only like this "D:\Programs\Test\AutoIt\Merged" and the merging tool will get the name of the active document that is opened.

 

EDIT: I mean, the merging tool will get the files from that path found in Path.ini

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Png is displaying now.

1st: Please can you remove the path shown in the GUI?
Do you mean the top line in the first ListBox as that is the same as the document selected, the one you highlighted appears to be a different document altogether.

Can you show only the files that was found from the active document/path?
Do you mean by default and remove the Browse button to select a new folder?

So Path.ini isn't a real .ini file? i.e.
[Section Name]
Path = D:\Programs\Test\AutoIt\Merged

If it is only a text file then can we just use FileReadLine(@AppData & "\Microsoft\Templates\Path.ini", 1), i.e. just reads the first line or will there be more than one line?

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

I made it this way, in fact it shows all the document found from the path of the active document and everything follows...:lol:

Func Selectapps()
   $file = @ScriptDir & "\Path.ini"
   FileOpen($file, 0)
   For $i = 1 to _FileCountLines($file)
      $line = FileReadLine($file, $i)
   Next
   FileClose($file)
   Global $oWord = _Word_Create()
   Global $oDoc = _Word_DocGet($oWord, 1)
   Local $DocFile = $oDoc.Name
    Local $bDocAttach = True
    Local $oWord = _Word_Create(True, False)
    GUICreate("Merge File(s)", 400, 395, 350, 250)
        GUISetBkColor(0xFFFFFF)
    Local $idSelectDoc = GUICtrlCreateCombo($line & "\" & $DocFile, 10, 10, 310, 20) ; getting the active document and the $line from the path.ini

 

19 minutes ago, Subz said:

Do you mean the top line in the first ListBox as that is the same as the document selected, the one you highlighted appears to be a different document altogether.

Highlighting that document was not intentionally, it's just I forgot to change the highlighted doc. Yup, I added two the same file name but different extension (sorry I missed upo:)).

 

19 minutes ago, Subz said:

If it is only a text file then can we just use FileReadLine(@AppData & "\Microsoft\Templates\Path.ini", 1), i.e. just reads the first line or will there be more than one line?

It's just one line for the users accuracy not to browse anymore and instead, it will show automatically the files.

 

19 minutes ago, Subz said:

an you show only the files that was found from the active document/path?
Do you mean by default and remove the Browse button to select a new folder?

I mean.

From this:

From.png

 

To this:

Shoul Be.png

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Can you test the following:

I haven't removed the file paths yet, the only issue I have with that is if a user selects another folder to add more docs to merge, then the script wouldn't know where to look for the files.  For example if you wanted to merge documents from:

C:\Folder1\Image1.jpg
C:\Folder2\Image1.jpg

It wouldn't know where Image1.jpg is coming from if we remove the file path.

Hope that makes sense.

#include <Array.au3>
#include <File.au3>
#include <GuiComboBox.au3>
#include <GuiConstantsEx.au3>
#include <GuiEdit.au3>
#include <GuiListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Word.au3>

Global $idListDocs, $idListItems, $idItemUp, $idItemDown, $iLastItemSelect, $idSelectDoc

Selectapps()

Func Selectapps()
    Local $sSelectDoc = "Select New or Opened Document"
    Local $sSelectPathDoc = FileReadLine(@AppDataDir & "\Microsoft\Templates\Path.ini", 1)
        If @error Then $sSelectPathDoc = ""
    Local $bDocAttach = True
    Local $oWord = _Word_Create(True, False)
    GUICreate("Merge File(s)", 400, 395, 350, 250)
        GUISetBkColor(0xFFFFFF)
    $idSelectDoc = GUICtrlCreateCombo($sSelectDoc, 10, 10, 310, 20)
    Local $oDocs = _Word_DocGet($oWord)
        If @error Then
            $bDocAttach = False
        Else
            _GUICtrlComboBox_BeginUpdate($idSelectDoc)
            For $oDoc In $oDocs
                _GUICtrlComboBox_AddString($idSelectDoc, $oDoc.FullName)
            Next
            _GUICtrlComboBox_EndUpdate($idSelectDoc)
        EndIf

    Local $idBrowseDoc = GUICtrlCreateButton("Open...", 325, 9, 70, 23)
    $idListDocs = GUICtrlCreateList("", 10, 60, 310, 150, BitOR($WS_BORDER, $WS_VSCROLL))
    Local $idSelectFolder = GUICtrlCreateInput("", 10, 35, 310, 20)
        _GUICtrlEdit_SetCueBanner($idSelectFolder, "Select Folder")
        GUICtrlSetData($idSelectFolder, $sSelectPathDoc)
        _AddSelectFolder(GUICtrlRead($idSelectFolder))
    Local $idBrowseFolder = GUICtrlCreateButton("Browse...", 325, 34, 70, 23)
    Local $idAddItem = GUICtrlCreateButton("Add", 325, 60, 70, 30)
    Local $idClearItem = GUICtrlCreateButton("Clear Item", 325, 95, 70, 30)
    Local $idClearAll = GUICtrlCreateButton("Clear All", 325, 130, 70, 30)
    $idListItems = GUICtrlCreateList("", 10, 215, 310, 150, BitOR($WS_BORDER, $WS_VSCROLL))
    $idItemUp = GUICtrlCreateButton("Up", 325, 215, 70, 20)
    $idItemDown = GUICtrlCreateButton("Down", 325, 240, 70, 20)
    Local $idRemoveItem = GUICtrlCreateButton("Remove", 325, 265, 70, 30)
    Local $idMergeFiles = GUICtrlCreateButton("Merge File", 325, 300, 70, 30)
    Local $idCloseGui = GUICtrlCreateButton("Cancel", 325, 335, 70, 30)
    GUICtrlCreateLabel("Info:", 5, 370, 25, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    Local $idStatus = GUICtrlCreateLabel("Select files to merge!", 30, 370, 360, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(2, 9, 999, 0, "Calibri")
        GUICtrlSetColor(-1, 0xC0C0C0)
    GUISetState(@SW_SHOW)

    AdlibRegister("_ControlUpdate")

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_ClOSE, $idCloseGui
                ExitLoop
            Case $idSelectDoc
                If GUICtrlRead($idSelectDoc) = $sSelectDoc Then ContinueLoop
                $sSelectDocPath = StringLeft(GUICtrlRead($idSelectDoc), StringInStr(GUICtrlRead($idSelectDoc), "\", 0, -1) - 1)
                GUICtrlSetData($idSelectFolder, $sSelectDocPath)
                _AddSelectFolder(GUICtrlRead($idSelectFolder))
            Case $idBrowseDoc
                Local $sBrowseDoc = FileOpenDialog("Select Merge Document File Path", @ScriptDir, "Word Doc(*.doc;*.docx)", 3)
                If @error Then ContinueLoop
                GUICtrlSetData($idSelectDoc, $sBrowseDoc)
            Case $idBrowseFolder
                Local $sBrowseFolder = FileSelectFolder ("Select Folder with Documents to merge", "", 4, @ScriptDir)
                If @error Then ContinueLoop
                _AddSelectFolder($sBrowseFolder)
            Case $idAddItem
                $iListDocs = _GUICtrlListBox_GetCurSel($idListDocs)
                    If $iListDocs = -1 Then ContinueLoop
                _GUICtrlListBox_AddString($idListItems, _GUICtrlListBox_GetText($idListDocs, $iListDocs))
            Case $idClearItem
                If _GUICtrlListBox_GetCurSel($idListDocs) = -1 Then ContinueLoop
                _GUICtrlListBox_DeleteString($idListDocs, _GUICtrlListBox_GetCurSel($idListDocs))
            Case $idClearAll
                GUICtrlSetData($idSelectFolder, "")
                _GUICtrlListBox_ResetContent($idListDocs)
            Case $idItemUp
                _ListBoxDirect($idListItems, "Up")
            Case $idItemDown
                _ListBoxDirect($idListItems, "Down")
            Case $idRemoveItem
                If _GUICtrlListBox_GetCurSel($idListItems) = -1 Then ContinueLoop
                _GUICtrlListBox_DeleteString($idListItems, _GUICtrlListBox_GetCurSel($idListItems))
            Case $idMergeFiles
                If FileExists(GUICtrlRead($idSelectDoc)) = 0 Then ContinueLoop
                Local $bDocStart = True, $bDocAdd = False
                $iCount = _GUICtrlListBox_GetCount($idListItems)
                    If $iCount <= 0 Then ContinueLoop
                GUICtrlSetState($idAddItem, $GUI_DISABLE)
                GUICtrlSetState($idBrowseFolder, $GUI_DISABLE)
                GUICtrlSetState($idItemUp, $GUI_DISABLE)
                GUICtrlSetState($idItemDown, $GUI_DISABLE)
                GUICtrlSetState($idRemoveItem, $GUI_DISABLE)
                GUICtrlSetState($idMergeFiles, $GUI_DISABLE)
                If $bDocAttach = False Then
                    $oWordDoc = _Word_DocOpen($oWord, GUICtrlRead($idSelectDoc))
                Else
                    $oWordDoc = _Word_DocAttach($oWord, GUICtrlRead($idSelectDoc))
                EndIf
                For $i = 0 To $iCount
                    $iFileExtension = _FileExtension(_GUICtrlListBox_GetText($idListItems, $i))
                        If $iFileExtension = -1 Then ContinueLoop
                    GUICtrlSetData($idStatus, "Merging " & _GUICtrlListBox_GetText($idListItems, $i))
                    If $bDocStart Then
                        $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListItems, $i))
                        $bDocStart = False
                        ContinueLoop
                    Else
                        If $bDocAdd Then $oWordDoc.Bookmarks("\EndofDoc").Range.InsertBreak($WdPageBreak)
                    EndIf
                    $bDocAdd = _MergeDocuments($oWordDoc, $iFileExtension, _GUICtrlListBox_GetText($idListItems, $i))
                Next
                _GUICtrlListBox_ResetContent($idListItems)
                GUICtrlSetState($idAddItem, $GUI_ENABLE)
                GUICtrlSetState($idBrowseFolder, $GUI_ENABLE)
                GUICtrlSetState($idRemoveItem, $GUI_ENABLE)
                GUICtrlSetState($idMergeFiles, $GUI_ENABLE)
                _Word_DocSave($oWordDoc)
;~              _Word_DocSaveAs($oWordDoc, GUICtrlRead($idSelectDoc))
;~              _Word_DocClose($oWordDoc)
;~              _Word_Quit($oWord)
                GUICtrlSetData($idStatus, "Select files to merge!")
                MsgBox(64, "Completed", "Merged Completed!")
        EndSwitch
    WEnd
EndFunc

Func _ListBoxDirect($_hListBox, $_sListBoxDirect)
    Local $iListItemSelect = _GUICtrlListBox_GetCurSel($_hListBox)
    Local $iListItemCount = _GUICtrlListBox_GetCount($idListItems) - 1
    Local $sListItemSelect = _GUICtrlListBox_GetText($_hListBox, $iListItemSelect)
    Switch $_sListBoxDirect
        Case "Up"
            If $iListItemSelect <= 0 Then Return
            _GUICtrlListBox_DeleteString($_hListBox, $iListItemSelect)
            _GUICtrlListBox_InsertString($_hListBox, $sListItemSelect, $iListItemSelect - 1)
            _GUICtrlListBox_SetCurSel($_hListBox, $iListItemSelect - 1)
        Case "Down"
            If $iListItemSelect >= $iListItemCount Then Return
            _GUICtrlListBox_DeleteString($_hListBox, $iListItemSelect)
            _GUICtrlListBox_InsertString($_hListBox, $sListItemSelect, $iListItemSelect + 1)
            _GUICtrlListBox_SetCurSel($_hListBox, $iListItemSelect + 1)
        Case Else
            Return
    EndSwitch
EndFunc

Func _ControlUpdate()
    Local $iListItemSelect = _GUICtrlListBox_GetCurSel($idListItems)
        If $iLastItemSelect = $iListItemSelect Then Return
    Local $iListItemCount = _GUICtrlListBox_GetCount($idListItems) - 1
        $iLastItemSelect = $iListItemSelect
    Switch $iListItemSelect
        Case -1
            If $iListItemCount = 0 Or $iListItemCount = -1 Then
                GUICtrlSetState($idItemDown, $GUI_DISABLE)
                GUICtrlSetState($idItemUp, $GUI_DISABLE)
            EndIf
        Case 0
            GUICtrlSetState($idItemUp, $GUI_DISABLE)
            If $iListItemCount > 0 Then GUICtrlSetState($idItemDown, $GUI_ENABLE)
        Case $iListItemCount
            GUICtrlSetState($idItemDown, $GUI_DISABLE)
            If $iListItemSelect > 0 Then GUICtrlSetState($idItemUp, $GUI_ENABLE)
        Case Else
            GUICtrlSetState($idItemUp, $GUI_ENABLE)
            GUICtrlSetState($idItemDown, $GUI_ENABLE)
    EndSwitch
EndFunc

Func _AddSelectFolder($_sFolderPath)
    Local $aFolderList = _FileListToArrayRec($_sFolderPath, "*.doc;*.docx;*.jpg;*.jpeg;*.png;*.tif;*.tiff|~*.doc;~*.docx", 1, 0, 0, 2)
    If @error Then Return ""
    For $i = 1 To $aFolderList[0]
        If $aFolderList[$i] = GUICtrlRead($idSelectDoc) Then ContinueLoop
        _GUICtrlListBox_AddString($idListDocs, $aFolderList[$i])
    Next
EndFunc

Func _MergeDocuments($_oWordDoc, $_iFileExtension, $_sFileName)
    Switch $_iFileExtension
        Case 1
            $_oWordDoc.Bookmarks("\EndOfDoc").Range.InsertFile($_sFileName)
            Return True
        Case 2
            _Word_DocPictureAdd($_oWordDoc, $_sFileName, False, True, $_oWordDoc.Bookmarks("\EndOfDoc").Range)
            Return True
        Case Else
            Return False
    EndSwitch
EndFunc
Func _FileExtension($_sFileName)
    Local $iFileExtension = StringInStr($_sFileName, ".", 0, -1)
        If $iFileExtension = 0 Then Return -1
    Local $sFileExtension = StringTrimLeft($_sFileName, $iFileExtension)
    Switch $sFileExtension
        Case "doc", "docx"
            Return 1
        Case "jpg", "jpeg", "tif", "tiff", "png"
            Return 2
    EndSwitch
    Return -1
EndFunc

 

Edited by Subz
Fixed Clear Item.
  • Like 1

Share this post


Link to post
Share on other sites
KickStarter15
3 hours ago, Subz said:

It wouldn't know where Image1.jpg is coming from if we remove the file path.

Hmmm yup, it makes sense.:sweating: But there are cases that they can't see the files what to merge due to the long path that was shown in $idListDocs, like below. The user needs to see the files what they are going to merge.:>

image.png.f780d143b1a972a0a2390b99ee7b3682.png

 

Over all, thank you very much, it work as expected and much appreciated Subz.o:)

 

EDIT: I just notice that the clear item is not working.

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
Subz

Sorry have fixed the Clear Item in the code above, what I would suggest is using ListView rather than ListBox, for column 1 have the File Path and for column 2 have the File Name, then change the Column 1 Width to 0 (basically hidden).  That should get around the issue, alternatively you could add $WS_HSCROLL to the ListBox, but the work around is not as nice as a ListView.  If I get a chance I'll look into it for you if you like, your call.

Share this post


Link to post
Share on other sites
KickStarter15

@Subz,

Thank you so much for the BIG help Subz, I agree with the path that should be shown in the GUI to identify where that file/s were located or saved. I did some interview with the Station Stuff in production yesterday and found out that they are also merging some files from different path. Hmmmm:sweating: you've got me there, thankz!

Anyways, thank you for fixing the clear item as well (I did not notice that "$idListItems" was declared instead of "$idListDocs") and what I did with your code is, I added the below code for them to resize freely the GUI to fully see the files found for merging. In that, that fixes my issue and thank you! thank you! thank you....! so much for the help. Really appreciated!!!:> In short, there's no need to changed you code IT WAS PERFECT..... Still your the best.

GUICtrlSetResizing(-1, BitOr($GUI_DOCKTOP, $GUI_DOCKLEFT, $GUI_DOCKSIZE))

 

Thanks!o:)


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.