A list of products available on this site
Documentation for XtraMania's xtras
Prices and links to the online store
Xtras, PDFs, samples
Have a question? Ask us!
Logo. www.xtramaina.com  
Home Search E-mail
VbScriptXtra Samples/Word

ActiveCompanionSet xtras for Macromedia Director

Typing something in new Word document

This handler will run Word application (if any) create new document and type simple message at the top of the page.

on testWord
  -- Creating a new instance of Microsoft Word
  -- Checking whether object is created
  if not objectP(wordApp) then
    alert "Failed to create Word.Application:"&RETURN&wordApp
  end if
  -- Setting application's window params
  -- Creating new workbook
  -- Arranging document window
  -- Typing simple message at the beginning of the document
  -- Extra parentheses required for Director 7
  (doc.range()).InsertAfter("Hello Word!")
  -- Setting the font style of all text in document

Batch search and replace over Word documents

This sample (thanks to Steven Hite) performs search and replace operation for all Word documents in the specified folder. It uses BuddyAPI for getting files from folder and VbScriptXtra for handling Word documents:

-- Batch process "search and replace" any string in each MS Word Document found in a folder.
on WordDoc_BatchSearchAndReplace findString, replaceString, sourceFolder
  aFiles = baFileList(sourcefolder, "*.doc" )
  if aFiles.count = 0 then exit
  objWord = xtra("VbScriptXtra").CreateObject("Word.Application")
  objWord.Visible = True
  repeat with i = 1 to aFiles.count
    thisDoc = sourceFolder & aFiles[i]
    objDocs = objWord.Documents
    objDoc = objDocs.Open(thisdoc)
    if objDocs.failed then
      put objDocs.LastError
      objSelection = objWord.Selection
      -- Execute( FindText, MatchCase, MatchWholeWord, MatchWildcards, \
      -- MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, \
      -- Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl )
      objSelection.Find.Execute("a", false, false, false, false, false, true, true, void, \
            "e", #wdReplaceAll)
      --Save changes and closes the document
      objSelection = VOID
    end if
  end repeat
  -- Quit MS Word
  alert "Done"

Using OLE Embedded Word Document

This sample uses both OLExtra and VbScriptXtra from ActiveCompanionSet.

-- Create new OLE cast member
assetOLE = new( #OLEobject )

-- Create new OLE object of type Word document
assetOLE.InsertOLE( "Word.Document" )
-- Also you can try some of these:
-- assetOLE.InsertOLE( "d:TempSomeFile.doc" ) -- create from file
-- assetOLE.InsertOLE( ) -- allow user to choose it by hands with system Insert
OLE dialog

-- Open the OLE object with Microsoft Word
doc = assetOLE.GetObject()

if assetOLE.Failed then
  put assetOLE.LastError
end if

-- Typing simple message at the beginning of the document
doc.range().InsertAfter( "Editing by Lingo..." )

-- Setting the font style of all text in document
doc.content.bold = #true

-- Now we save changes and close the object
doc = void

In this sample doc is usual VBScriptXtra wrapper. Refer to the VbScriptXtra's documentation for more details. More sophisticated applications can use event handling for controlling operations that user performs with the activated OLE document. Also it is possible to control the location of the application window that is used for editing OLE document and so on.

Where to find more info about Word?

Microsoft Word contains rather large help system, which contains just everything about Word. Look at Visual Basic for Word topics, which describe Word data model, objects, methods and properties you can use.
Make sure you have installed this part of the help system, since it may not be included in typical installation.

VbScriptXtra provides autodocumentation feature allowing you to see what you can do with particular object instance. Just create an object and type in Messages window:
put interface(word)
put interface(word.Documents)
put interface(doc)
put interface(doc.range())

ObjectBrowser xtra - autodocumentation companion for VbScriptXtra - will show all available Word interfaces, methods, properties and enumerations.

ObjectBrowser xtra is a part of ActiveCompanionSet. It is available to Download.

Site homeSearchContact author © Eugene Shoustrov. www.xtramania.com