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.

OLE Word File Append


sourcedir = "cC:\Test Data Sheets\"                  ;CHANGE TO FIT YOUR NEEDS
destdir =  "C:\Test Data Sheets\Projects\"   ;CHANGE TO FIT YOUR NEEDS

If !DirExist(destdir) Then DirMake(destdir)

docfiles = FileItemize(StrCat(sourcedir, "*.doc"))

project = AskLine("Project Name","Please enter your project name","")
projectfile = StrCat(destdir,project,".doc")  

if !FileExist(projectfile)
   Message("Error",StrCat(projectfile," Does Not Exist. Make sure it exists before running this code."))
   exit
Endif

filelist = AskItemList("Choose files for your project.", docfiles, @TAB, @Unsorted,@Multiple)

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

;*** Obtain application and document objects.
oWORD = ObjectOpen("Word.Application")
oDOCS = oWORD.Documents
;*** Open Output file
oDOC = oDOCS.Open(ProjectFile)
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

count = ItemCount(filelist, @tab)
;*** Extract each module name, then insert it into the template. 
For Num = 1 to count
   thisDoc=StrCat(sourcedir, ItemExtract(Num, filelist, @TAB))
   
   ;*** Insert module into template.
   oSelection.InsertFile (:: FileName=thisDoc, Range="", ConfirmConversions=@False, Link=@False, Attachment=@False)
   ;*** Hit Enter 4 times to make some whitespace.
   oSelection.TypeParagraph
   oSelection.TypeParagraph
   oSelection.TypeParagraph
   oSelection.TypeParagraph
Next 

oDOC.SaveAs(projectfile)
oDOC.Close ()
oWORD.Quit()

;*** Finished building new book. Clean up.
objectclose(oSelection)
objectclose(oDOC)
objectclose(oDOCS)
objectclose(oWORD)

Article ID:   W17187
File Created: 2007:07:03:14:28:38
Last Updated: 2007:07:03:14:28:38