Oliver Brinzing wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Jo,

I'm trying to obtain the creation date  of the currently open document.
(an exception will probably be needed if the document has not been saved yet).

try ...

OPTION EXPLICIT

Sub Main

        Dim oDocument as Object
        Dim oInfo as Object
        Dim oDate as Object
        Dim nDate as Long
        Dim vTime as Variant

        oDocument = ThisComponent
        oInfo = oDocument.DocumentInfo

        ' get ...
        oDate = oInfo.CreationDate
        MsgBox oDate.Day & "." & oDate.Month & "." & oDate.Year & " " & oDate.Hours & 
":" & oDate.Minutes

        ' set ...
        nDate = DateValue(Date)
        oDate.Day = Day(nDate)
        oDate.Month = Month(nDate)
        oDate.Year = Year(nDate)
        vTime = TimeValue(Time)
        oDate.Hours = Hour(vTime)
        oDate.Minutes = Minute(vTime)
        oDate.Seconds = Second(vTime)
        oInfo.CreationDate = oDate

        MsgBox oDate.Day & "." & oDate.Month & "." & oDate.Year & " " & oDate.Hours & 
":" & oDate.Minutes

End Sub

Oliver
Hi Oliver,

Your code has the kind of simplicity that I admire. Many thanks for helping me out! I hope one day I will also be able to see through the API and produce code like that.

This is what it became:

Sub GetDocCreationDate(oDoc) As Object
 ' Many thanks to Oliver Brinzing for helping me out on this
 Dim oInfo as Object

 oInfo = oDoc.DocumentInfo

 GetDocCreationDate = oInfo.CreationDate
End Sub

Sub InsertEurDate()
 Dim oDate as Object

 oDate = GetDocCreationDate(ThisComponent)
 InsertDate(oDate.Day & "/" & oDate.Month & "/" & oDate.Year)
End Sub

Sub InsertUSDate()
 Dim oDate as Object

 oDate = GetDocCreationDate(ThisComponent)

 InsertDate(oDate.Month & "/" & oDate.Day & "/" & oDate.Year)
End Sub

Sub InsertDate(sDate)
' Based on GPLd code from Winfried Rohr. I tore it to pieces but being able
 ' to reuse his code saved me a good deal of time.
 Dim oDesktop
 Dim oController
 Dim oDocument
 Dim oTextCursor
 Dim oCell
 Dim sCursorPosition
 Dim oFrame
 Dim oPageStyle

 oDesktop = createUnoService( "com.sun.star.frame.Desktop" )
 oController = oDesktop.CurrentFrame.Controller
 oDocument = oController.Model
 oViewCursor = oController.ViewCursor

 if oDocument.supportsService("com.sun.star.text.TextDocument" ) then
sCursorPosition = WhereIsCursor( oController.ViewCursor ) if sCursorPosition = "InText" then oTextCursor = oDocument.getText().createTextCursorByRange( oViewCursor )
     oTextCursor.String = sDate
   elseif sCursorPosition = "InTable" then
     oCell = oViewCursor.Cell
     oTextCursor = oCell.getText().createTextCursorByRange( oViewCursor )
     oCell.insertString( oTextCursor, sDate , FALSE )
   elseif sCursorPosition = "InFrame" then
     oFrame = oViewCursor.TextFrame()
oFrame.getText().insertText( oViewCursor, sDate, False ) elseif sCursorPosition = "InHeaderOrFooter" then oPageStyle = oDocument.StyleFamilies().getByName("PageStyles").getByName( oViewCursor.PageStyleName )
     if not IsEmpty( oPageStyle.HeaderText ) then
       oText = oPageStyle.HeaderText
     else
       oText = oPageStyle.FooterText
     end if
     oTextCursor = oText.createTextCursorByRange( oViewCursor )
     oText.insertString( oTextCursor, sDate , FALSE )
   end if
 end if
End Sub

Sub    WhereIsCursor( oViewCursor ) As String
' Based on GPLd code from Winfried Rohr. I tore it to pieces but being able
 ' to reuse his code saved me a good deal of time.

 ' Test if cursor is in Header or Footer
 if oViewCursor.getText().ImplementationName = "SwXHeadFootText" then
   WhereIsCursor = "InHeaderFooter"
   exit sub
 elseif Not IsEmpty( oViewCursor.TextTable ) then
   WhereIsCursor = "InTable"
   exit sub
 elseif Not IsEmpty( oViewCursor.TextFrame ) then
   WhereIsCursor = "InFrame"
   exit sub
 elseif Not IsEmpty( oViewCursor.Text ) then
   WhereIsCursor = "InText"
 endif
End Sub

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to