WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Append Multiple Word Documents Together


We document every server build so rebuilds or disaster recovery will be easy. Each build has repetitious and unique steps (the OS build itself and then different system settings, applications, etc.). The steps are stored in separate Word document 'modules', and the modules we need are then glued together.

Drop this in a folder with Word documents. Set one file as the base template ('template='). Optionally set how many docs you want to be displayed in the dialog (maxButtons='). Dynamic buttons inspired by code posted by chrislegarth in December.

;*************************************************************************************
;*** Stick multiple Word documents together.
;*** 
;*** Winbatch 2004A
;*** 
;*** Code to dynamically create the dialog buttons inspired by:
;*** chrislegarth Posted on WB board 12/18/2003
;*************************************************************************************

ErrorMode(@CANCEL)

; ********************************************************************************
; *** Establish if script is running in debug (testing) mode or is a compiled EXE
; ********************************************************************************
state=rtStatus()
  If state==1 then  home=dirhome()   ; compiled EXE
  If state==10 then home=FilePath(IntControl(1004,0,0,0,0))    ; debug mode


; ************************************************************************************
; *** In their order: Enable close command from icon.
; ***                 Allow quiet termination of this app at Windows shutdown.
;***                  Add system menus to dialog box.
;***                  Hide the Winbatch icon from the taskbar. 
; ************************************************************************************
IntControl (1008, 1, 0, 0, 0)
IntControl(12, 5, 0, 0, 0)
IntControl(49,1,99,0,0)
IntControl (1002, 0, 0, 0, 0)


;*** the 'standard' list is the buttons that will always appear at the botton of the dialog.
Standard="Show More Modules - Up,Show More Modules - Down,Create the Document,Review/Reset module order"
StandardButtons=ItemCount(Standard, ",")
template="PUT FILE NAME HERE"
maxButtons=14                  ; <--- enhanced for 800x600 resolution.
disableButton="Up"             ; <--- disable the Up button first time this runs
show_status="DEFAULT"
buildList=""
error1=""


If ! DirExist("C:\Temp") then DirMake("C:\Temp")
tempFile="C:\Temp\temp.txt"

DirChange(home)
If ! FileExist(template) then error1="%template% file missing."

;*** The template file is already open. Can't move on.
If FileExist(template)==2
   Message("Close template file", "The template file '%template%' is open. You must close it before this program can run.")
   Exit
EndIf

;*** One or both of the 2 essential files are missing.
If error1 !="" 
   Message("File missing", StrCat(error1, @CRLF, "Cannot continue."))
   Exit
EndIf
 
  
M1="At the next screen select the modules you wish to add.%@CRLF%"
M2="Select in the order you want them added to the Template."

Message("Select Modules", StrCat(m1, m2)) 
  
buttons = fileitemize("%home%*.doc")



;*** Remove the Template file from the list.
index1=ItemLocate(template, buttons, @TAB)
buttons = ItemRemove(index1, buttons, @TAB)



;*** Remove '.doc' extension from file names
FilePut(tempFile, buttons)
fs=FileSize(tempFile)
binbuf=BinaryAlloc(fs+100)
BinaryRead(binbuf, tempFile)
BinaryReplace(binbuf, ".doc", "", @FALSE)
BinaryWrite(binbuf, tempFile)
BinaryFree(binbuf)

buttons=FileGet(tempFile)
FileDelete(tempfile)


;*** Sort and count buttons.
buttons = itemsort(buttons,@TAB)
buttoncount = itemcount(buttons,@TAB)
If buttoncount <=maxButtons then maxButtons=buttoncount


 clipButtons=strReplace(buttons, @TAB, @CRLF)
 clipappend(clipbuttons)

;*** Get the length of the longest file name (used to adjust dialog and button length).
Length=25                     ; <--- Length of 'REVIEW/RESET MODULE ORDER' string
For ZZ = 1 to buttoncount
   item=itemextract(ZZ,buttons,@tab)
   Len=StrLen(item)
   If Len > length then length=Len
Next

;*** !NOTE:! Change the multiplier if things are too short or too long in your display.
DialogLength=((Length * 3) + 13)
buttonLength=((Length * 3) + 4)

;*** One time init.
startPoint=1
endPoint=maxButtons
mods=0

:START

;*** Always set to disable both 'Up' and 'Down' buttons if less than maxbuttons.
If buttoncount <=maxButtons then disableButton="Both"


MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`%mods% Modules`
MyDialogX=-01
MyDialogY=-02
MyDialogWidth=DialogLength
MyDialogHeight=(12 * (maxButtons + StandardButtons))+2  * ((maxButtons + StandardButtons) + 4 )
MyDialogNumControls=(maxButtons + StandardButtons)
MyDialogProcedure=`DEFAULT`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

cn=1
str="00"
ctrlnum=StrCat(Str, cn)
spot = 3

For x = startPoint to endPoint

   button = itemextract(x,buttons,@tab)
   MyDialog%ctrlnum%=`003,%spot%,%buttonLength%,012,PUSHBUTTON,DEFAULT,%button%,%cn%,%cn%,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
 
   spot=(spot + 14)
   cn=(cn+1)
   ;*** Adjust for 1 and 2 digit numbers.
   If cn >= 10 then str="0"
   If cn >= 100 then str=""
   ctrlnum=StrCat(Str, cn)
   startPoint=(startPoint+1)
   
Next x

;*** Now add the 'standard' buttons.

For append = 1 to StandardButtons

   stdbutton=itemextract(append,Standard,",")

   ;*** Act on disabling buttons when appropriate.
   If disableButton=="Both" && stdbutton=="Show More Modules - Up" then show_status=2
   If disableButton=="Both" && stdbutton=="Show More Modules - Down" then show_status=2
   If disableButton=="Up" && stdbutton=="Show More Modules - Up" then show_status=2
   If disableButton=="Down" && stdbutton=="Show More Modules - Down" then show_status=2
   MyDialog%ctrlnum%=`003,%spot%,%buttonLength%,012,PUSHBUTTON,DEFAULT,%stdbutton%,%cn%,%cn%,%show_status%,"Arial|6656|170|34","0|0|0",DEFAULT`
   
   ;*** reset the button display attribute.
   show_status="DEFAULT"
   ;*** Increment values.
   spot=(spot + 14)
   cn=(cn+1)
   If cn >= 10 then str="0"
   ctrlnum=StrCat(Str, cn)
   
Next append

;*** Decrement startPoint by 1 (the last for/next loop adds 1 too many to startpoint)
startPoint=(startPoint - 1)

ButtonPushed=Dialog("MyDialog")


If buttonpushed==99 then exit  ; <--- the "X" in windows upper-right system menu


;*** Discover and act on the button that was pushed.
Switch buttonpushed


;*** IMPORTANT: The 'Case' values are dependent on the number of
;*** 'Standard' buttons defined.
;*** If you add/remove standard buttons you also need to adjust the CASE entries.



;*** 'MODULE LIST - UP' button pushed. 
Case (MyDialogNumControls-3)

   ;*** Can't go up any further.
   If startPoint <= maxButtons
      startPoint=1
      endPoint=maxButtons
      disableButton="Up"
   EndIf

   ;*** We're somewhere in the list that allows movement up.
   If startPoint > maxButtons
      startPoint=(startPoint - (2 * maxButtons) +1)
         If startPoint <= 1
            startPoint=1
            disableButton="Up"
               ;If StartPoint > 1 then disableButton=""
         EndIf
      If StartPoint > 1 then disableButton=""
      endPoint=((startPoint + maxButtons) -1)
   EndIf 
  
   Goto START




;*** 'MODULE LIST - DOWN' button pushed. 
Case (MyDialogNumControls-2)


   
   ;*** Less than maxButtons left to show, or at end of the list.
   If (endPoint + maxButtons) > buttoncount
      endPoint=buttoncount
      startPoint=(endPoint - maxButtons) +1
      disableButton="Down"
   EndIf



   ;*** We're somewhere in the list that allows movement down
   ;*** one full set of maxButtons.
   If (endPoint + maxButtons) <= buttoncount
      startPoint=(endPoint + 1)
      endPoint=((startPoint + maxButtons) -1)
         If endPoint==buttoncount then disableButton="Down"
            Else disableButton=""
   EndIf

   Goto START



;*** 'BUILD THE BOOK' button pushed.
Case (MyDialogNumControls-1)

   ;*** Clean up the buildlist
   buildlist=StrReplace(buildlist, @CR, @TAB)
   buildlist=StrTrim(buildlist)
   count=ItemCount(buildlist,  @TAB)

   

   ;*** Declare MS Word constants
   wdScreen=7
   wdGoToPage=1
   wdGoToFirst=1

   ;*** Obtain application and document objects.
   oWORD = ObjectOpen("Word.Application")
   oDOCS = oWORD.Documents
   ;*** Open template file
   oDOC = oDOCS.Open("%home%%template%")
   oWORD.visible=@true
   ;*** Obtain Selection object.
   oSelection=oWORD.Selection

   ;*** Move to page 100 (that should be more than enough!)
   oSelection.GoTo (:: What=wdGoToPage, Which=wdGoToFirst, Count=100, Name="")

   ;*** Page Down 10 times to get to bottom of last page.
   oSelection.MoveDown(:: Unit=wdScreen, Count=10)

   ;*** Hit Enter 4 times to make some whitespace.
   oSelection.TypeParagraph
   oSelection.TypeParagraph
   oSelection.TypeParagraph
   oSelection.TypeParagraph

 
   ;*** Extract each module name, then insert it into the template. 
      For Num = 1 to count
         thisDoc=ItemExtract(Num, buildlist, @TAB)

          ;*** Insert module into template.
          oSelection.InsertFile (:: FileName="%home%%thisDoc%.doc", Range="", ConfirmConversions=@False, Link=@False, Attachment=@False)
          ;*** Hit Enter 4 times to make some whitespace.
          oSelection.TypeParagraph
          oSelection.TypeParagraph
          oSelection.TypeParagraph
          oSelection.TypeParagraph
      Next Num

   
   
   ;*** Finished building new book. Clean up.
   objectclose(oSelection)
   objectclose(oDOC)
   objectclose(oDOCS)
   objectclose(oWORD)
   message("","All done")

   Exit


;*** 'REVIEW/RESET THE MODULE ORDER' button pushed.
Case MyDialogNumControls
   Q=AskYesNo("Clear the list and start over?",buildlist)
    startPoint=((endPoint - maxButtons) +1)
      If Q==@YES   
         drop(buildlist)
         mods=0
         buildlist=""
         Display(1, "Reset", "Starting over")
      EndIf

   goto START



;*** Default action when one of the actual 'module' file buttons was pushed.
case buttonpushed
   
   ;*** Find out which button was pushed and extract the button name from the dialog.
   ;*** Adjust for 1 digit and 2 digit numbers (1 to 9, 10 to 99).
   If StrLen(buttonpushed)==1 then dlgNum=StrCat("00", %buttonpushed%)
   If StrLen(buttonpushed)==2 then dlgNum=StrCat("0", %buttonpushed%)
      buttonName=ItemExtract(7, mydialog%dlgnum%, ",")
      buildList=StrCat(buildList, @CR, buttonname)
      startPoint=((endPoint - maxButtons) +1)
      mods=mods+1
      Goto START

EndSwitch

:CANCEL
startPoint=((endPoint - maxButtons) +1)
GOTO START

Article ID:   W16139
File Created: 2004:03:30:15:42:58
Last Updated: 2004:03:30:15:42:58