Opening a document

From Apache OpenOffice Wiki
Jump to: navigation, search

The following function opens a file into a new window.

function fnOpenDoc(sFile)
sURL = ConvertToURL(sFile)
fnOpenDoc = StarDesktop.loadComponentFromURL(sURL, "_blank", 0, Array())
end function

Examples of calling the function:

oDoc = fnOpenDoc("C:\Documents and Settings\danny\Desktop\MyCalc.ods") ' Windows
oDoc = fnOpenDoc("/home/danny/Desktop/MyCalc.ods") ' Linux

A more complete function for opening a file:

function fnOpenDoc(sFile, optional bAsTemplate, optional sPassword, optional bReadOnly, _
  optional nMacroExecutionMode, optional nUpdateDocMode, optional sJumpMark, _
  optional nViewId, optional bHidden, optional bStartPresentation,_
  optional sFilterName, optional sFilterOptions, optional vFilterData, optional bRepairPackage)
dim mFileProperties()
if not isMissing(bAsTemplate) then
        subAddproperty(mFileProperties(), "AsTemplate", bAsTemplate)
end if
if not isMissing(sPassword) then
        subAddproperty(mFileProperties(), "Password", sPassword)
end if
if not isMissing(bReadOnly) then
        subAddproperty(mFileProperties(), "ReadOnly", bReadOnly)
end if
if not isMissing(nMacroExecutionMode) then
        subAddproperty(mFileProperties(), "MacroExecutionMode", nMacroExecutionMode)
end if
if not isMissing(nUpdateDocMode) then
        subAddproperty(mFileProperties(), "UpdateDocMode", nUpdateDocMode)
end if
if not isMissing(sJumpMark) then
        subAddproperty(mFileProperties(), "JumpMark", sJumpMark)
end if
if not isMissing(nViewId) then
        subAddproperty(mFileProperties(), "ViewId", nViewId)
end if
if not isMissing(bHidden) then
        subAddproperty(mFileProperties(), "Hidden", bHidden)
end if
if not isMissing(bStartPresentation) then
        subAddproperty(mFileProperties(), "StartPresentation", bStartPresentation)
end if
if not isMissing(sFilterName) then
        subAddproperty(mFileProperties(), "FilterName", sFilterName)
end if
if not isMissing(sFilterOptions) then
        subAddproperty(mFileProperties(), "FilterOptions", sFilterOptions)
end if
if not isMissing(vFilterData) then
        subAddproperty(mFileProperties(), "FilterData", vFilterData)
end if
if not isMissing(bRepairPackage) then
        subAddproperty(mFileProperties(), "RepairPackage", bRepairPackage)
end if
sURL = ConvertToURL(sFile)
on local error resume next
fnOpenDoc = StarDesktop.loadComponentFromURL(sURL, "_blank", 0, mFileProperties())
end function

This function calls subAddProperty which is defined in working with properties.

Parameter Explanation
bAsTemplate True = Create new document
False = Open document (including templates) for editing
sPassword For opening password protected files
bReadOnly True = In the user interface the document will be presented as read only
sJumpMark For example the name of a bookmark in a text document
bHidden True = Document opened but not visible. Use with caution
bStartPresentation For Impress documents start the presentation
sFilterName See saving a document for a list of filters
sFilterOptions Use this to supply additional info. to the filter
vFilterData Use this if a string for sFilterOptions is insufficient
bRepairPackage True = opens the document in repair mode, so as much information as possible will be retrieved

For a complete reference on FilterOptions and FilterData, consult the Developer's Guide. FilterOptions for spreadsheet documents are in the Spreadsheet section.

Personal tools