[Solved] Integration with VB.NET

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mdw1975
Posts: 5
Joined: Mon Nov 06, 2017 10:24 pm

[Solved] Integration with VB.NET

Post by mdw1975 »

I am using VisualStudio 2015. I have written code in which I can open an existing document template, replace text as needed. This code works great. Now, I would like to be able to save the document with a new name and then print. Current Function is below...

Code: Select all

Public Function EditTicketForPrinteingViaOfficeWriter(inBOL As clsBOLHistory) As Boolean
        Try

            'Original code
            'Dim objWordApp As New Word.Application
            'objWordApp.Visible = True

            'Copy orignal file to new ticket file - Make file names configurable
            FileCopy(Me._LocalEditedTicketPath, Me._EditedTicketPathODT)

            '//Open an existing document using OPENOFFICE WRITER

            Dim oSM As Object = CreateObject("com.sun.star.ServiceManager")  'Root object for accessing OpenOffice from VB
            'Dim oDesk, oDoc As Object 'First objects from the API
            'Dim arg(1) As Object                'Ignore it for the moment !
            'Dim OpenPar(1) As Object 'a Visual Basic array, with 3 elements
            'Dim merge As Object
            'OpenPar(0) = MakePropertyValue("ReadOnly", False)
            'OpenPar(1) = MakePropertyValue("Password", "secret")

            'Instanciate OOo : this line is mandatory with VB for OOo API
            'oSM = CreateObject("com.sun.star.ServiceManager")
            'Create the first and most important service
            oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
            Dim MasterScriptProviderFactory As Object = oSM.createInstance("com.sun.star.script.provider.MasterScriptProviderFactory")

            'Open an existing doc (pay attention to the syntax for first argument)
            oDoc = oDesk.loadComponentFromURL("file:///" & Me._EditedTicketPathODT, "_blank", 0, OpenPar)

            'Dim sUrl As String = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=application"

            Dim oProvider As Object = MasterScriptProviderFactory.createScriptProvider("")
            Dim oScript As Object = oProvider.getScript(sUrl)
            oScript.invoke(arg, arg, arg)

#Region " TEEST CODE "

            'oDoc = ThisComponent       'No need for this, it's already open: 11/05/2017
            'Dim oEnum1 As Object
            'Dim oEnum2 As Object

            'Dim TextElement As Object   'Didn't seem to work: 11/05/2017
            'Dim TextPortion As Object
            'oEnum1 = oDoc.Text.createEnumeration

            'While oEnum1.hasMoreElements
            '    TextElement = oEnum1.nextElement

            '    If TextElement.supportsService("com.sun.star.text.Paragraph") Then
            '        oEnum2 = TextElement.createEnumeration
            '        ' loop over all sub-paragraphs
            '        While oEnum2.hasMoreElements
            '            TextPortion = oEnum2.nextElement
            '            MsgBox("'" & TextPortion.String & "'")
            '            TextPortion.String = Replace(TextPortion.String, "<Date>", inBOL.TimeStamp)
            '        End While
            '        'MsgBox(TextElement.String.ToString)
            '        'TextElement.String = Replace(TextElement.String, "<Date>", inBOL.TimeStamp)
            '    End If
            'End While

            '// 2nd attempt
            'Dim SearchDesc As Object
            'SearchDesc = oDoc.createSearchDescriptor

            'SearchDesc.searchString = "<Date>"

            'Dim Search As Object
            'Dim Replace As Object

            'Dim I As Long

            'Replace = oDoc.createReplaceDescriptor
            'Search = oDoc.createSearchDescriptor
            'Replace = oDoc.createReplaceDescriptor
            'Replace.SearchRegularExpression = True

            'Search.SearchString = "<Date>"
            'Replace.ReplaceString = inBOL.TimeStamp

            'Search.SearchString = "<StreetAddress1>"
            'Replace.ReplaceString = _CompanyInfo.StreetAddress1

            '// 3rd Attempt
            'Dim objText As Object, objCursor As Object
            'objText = oDoc.GetText
            'objCursor = objText.createTextCursor

            '' replace all
            'Dim oSrch As Object
            ''Set oSrch = oDoc.createSearchDescriptor
            'oSrch = oDoc.createReplaceDescriptor
            'oSrch.setSearchString("<Date>")
            'oSrch.setReplaceString(inBOL.TimeStamp)
            'oDoc.replaceAll(oSrch)
#End Region

#Region " UPDATE CODE "

            ReplaceWriterBOLInfor(oDoc, "<Date>", inBOL.TimeStamp)

            ' Not Used at all facilities - NEED TO STANDARDIZE TEMPLATE
            ReplaceWriterBOLInfor(oDoc, "<CompanyName>", _CompanyInfo.Name)
            ReplaceWriterBOLInfor(oDoc, "<StreetAddress1>", _CompanyInfo.StreetAddress1)
            ReplaceWriterBOLInfor(oDoc, "<StreetAddress2>", _CompanyInfo.StreetAddress2)
            ReplaceWriterBOLInfor(oDoc, "<PhoneNumber>", _CompanyInfo.PhoneNumber)
            'END NOT USED AT TRINITY

            ReplaceWriterBOLInfor(oDoc, "<DestinationName>", inBOL.CustomerName)
            ReplaceWriterBOLInfor(oDoc, "<CustomerNum>", inBOL.CustomerNumber)
            ReplaceWriterBOLInfor(oDoc, "<DestAddress1>", inBOL.CustomerStreet)
            ReplaceWriterBOLInfor(oDoc, "<DestAddress2>", "")

            Dim DestLocaleID As String = inBOL.CustomerCity & ", " & inBOL.CustomerState & " " & inBOL.CustomerZipCode
            ReplaceWriterBOLInfor(oDoc, "<DestLocaleID>", DestLocaleID)

            ReplaceWriterBOLInfor(oDoc, "<BOLNumber>", inBOL.BOLNumber)
            ReplaceWriterBOLInfor(oDoc, "<HaulerName>", inBOL.CarrierName)
            ReplaceWriterBOLInfor(oDoc, "<DriverName>", "")
            ReplaceWriterBOLInfor(oDoc, "<CarrierTruckNumber>", inBOL.TruckNumber)
            ReplaceWriterBOLInfor(oDoc, "<CarrierTrailerSerialNumber>", inBOL.TrailerNumber)
            ReplaceWriterBOLInfor(oDoc, "<MaterialName>", inBOL.Description)
            ReplaceWriterBOLInfor(oDoc, "<ProductCode>", inBOL.ProductCode)
            ReplaceWriterBOLInfor(oDoc, "<LabNum>", inBOL.LabNumber)
            ReplaceWriterBOLInfor(oDoc, "<ProjectNum>", inBOL.Project)
            ReplaceWriterBOLInfor(oDoc, "<PlantOrderNum>", inBOL.PlantOrder)
            ReplaceWriterBOLInfor(oDoc, "<MatTemp>", inBOL.LoadTemp)
            ReplaceWriterBOLInfor(oDoc, "<TareWeight>", inBOL.TareWeight)
            ReplaceWriterBOLInfor(oDoc, "<GrossWeight>", inBOL.GrossWeight)
            ReplaceWriterBOLInfor(oDoc, "<NetWeight>", inBOL.NetWeight)
            ReplaceWriterBOLInfor(oDoc, "<NetTons>", inBOL.Tons)
            ReplaceWriterBOLInfor(oDoc, "<NetGallons>", CStr(CSng(inBOL.NetGallons)))
            ReplaceWriterBOLInfor(oDoc, "<SpecGrav>", inBOL.SpecificGravity)
            ReplaceWriterBOLInfor(oDoc, "<TankNumber>", inBOL.SourceTankNumber)
            ReplaceWriterBOLInfor(oDoc, "<LbsPerGal>", Me.BOLMaterialDensity)
            ReplaceWriterBOLInfor(oDoc, "<JobNUmber>", inBOL.PlantOrder)
            ReplaceWriterBOLInfor(oDoc, "<24HREmergencyContactNumber>", Me.BOL24HREmergencyNumber)
            ReplaceWriterBOLInfor(oDoc, "<NonEmergencyContactNumber>", Me.BOLNonEmergencyNumber)

#End Region

            ' Print the Edited BOL
            'Dim oDispatchHelper As Object
            'Dim oServiceManager
            Try
                Dim arg0, arg(0) As Object
                oDispatchHelper = oSM.createInstance("com.sun.star.frame.DispatchHelper")
                arg0 = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
                'oDispatchHelper = oServiceManager.createInstance("com.sun.star.frame.DispatchHelper")
                'Call oDispatchHelper.executeDispatch(oDoc, ".uno:PrintDefault", "", 0, arg)
                arg0.Name = "CopyCount"
                arg0.Value = 1
                'oDoc.Print

                CallByName(oDoc, "print", vbMethod, arg0) 'arg is an array of arguments (cf MakePropertyValue)
            Catch ex As Exception
                RaiseEvent SoftwareError(Me, New EventArgs, ex.ToString)
            End Try


            'Save the doc
            '//oDoc.Save
            'propertyValues(0) = New unoidl.com.sun.star.beans.PropertyValue
            'propertyValues(0).Name = "Overwrite"
            'propertyValues(0).Value = New uno.Any(True)


            Dim args() As Object
            'args = oSM.createInstance("com.sun.star.beans.PropertyValue")
            'args.Name = "Overwrite"
            'args(0).Value = New uno.Any(True)
            Try
                oDoc.storeAsURL("file:///" & _LocalBOLSavePath & "_" & inBOL.BOLNumber & ".docx", args)
            Catch ex As Exception
                RaiseEvent SoftwareError(Me, New EventArgs, ex.ToString)
            End Try


            'Try : FileCopy(Me._EditedTicketPathODT, _LocalBOLSavePath & "_" & inBOL.BOLNumber & ".docx") : Catch : End Try

            'Print doc
            'Dim arg1(0) As Object
            'arg1(0) = ("")
            'CallByName(oDoc, "print", vbMethod, arg1) 'arg is an array of arguments (cf MakePropertyValue)

            'Close the doc
            oDoc.Close(True)
            oDoc = Nothing


            'objDoc.PrintOut(,,,,,,, NumberOfCopiesToPrint)
            'objDoc.PrintOut()
            '//oDoc.print

            'Save and close the document  
            '//oDoc.save
            '//oDoc.close

            'objDoc.Save()
            'objDoc.Close()
            'objDoc = Nothing

            'objWordApp.Quit()



            'Try : FileCopy(Me._EditedTicketPath, _NetoworkBOLSavePath & "_" & Me._CurrentTransaction.ID & ".docx") : Catch : End Try

            'LOG ITEM FOR TRINITY ACCOUNTING
            'InsertLoadProcessLog()
            'InsertBOLHistory()


            Return True

        Catch ex As Exception
            RaiseEvent SoftwareError(Me, New EventArgs, ex.ToString)
            Return False
        End Try

    End Function


Last edited by mdw1975 on Fri Nov 10, 2017 2:47 pm, edited 1 time in total.
OpenOffice 4.14 on Windows 7
mdw1975
Posts: 5
Joined: Mon Nov 06, 2017 10:24 pm

Re: Integration with VB.NET

Post by mdw1975 »

Found print code that works...

Code: Select all

' Print the Edited BOL
            Try
            '    'THIS WORKS!!! 2017.11.07
                Dim PrintPro(0) As Object
                PrintPro(0) = MakePropertyValue("Wait", True)
                oDoc.Print(PrintPro)
            Catch ex As Exception

            End Try
Still having issues with saving edited document.
OpenOffice 4.14 on Windows 7
mdw1975
Posts: 5
Joined: Mon Nov 06, 2017 10:24 pm

Re: Integration with VB.NET

Post by mdw1975 »

I've tried Array, get exception with incorrect number of elements
I've tried Object, get exception with IOError

Code: Select all

 Dim URL As String = "file:///" & _LocalBOLSavePath & "_" & inBOL.BOLNumber & ".odt"
oDoc.storeToURL(URL, 'what parameter and type goes here?)
OpenOffice 4.14 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Integration with VB.NET

Post by Villeroy »

Empty Array(). If you save an ordinary Open Document, you do not need to pass anything. However, the API is extremely picky with arguments and data types. Missing arguments are not possible in this context. It has something to do with the independency from specific programming languages and platforms.
If you had to pass some save arguments (different document format or meta information) you would pass an array of com.sun.star.beans.PropertyValue (structs of Name and Value). Possible property values are documented here: http://www.openoffice.org/api/docs/comm ... iptor.html
Arrays of property values are the language independent way to pass a variable amount of named arguments.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mdw1975
Posts: 5
Joined: Mon Nov 06, 2017 10:24 pm

Re: Integration with VB.NET

Post by mdw1975 »

Empty Array() still generates "com.sun.star.task.ErrorCodeIOException: "
oDoc.store does not work
Generates the same exception.

I've check OpenOffice settings as well as permissions on the target directory. I'm sure that it's something simple that I'm overlooking.
OpenOffice 4.14 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Integration with VB.NET

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mdw1975
Posts: 5
Joined: Mon Nov 06, 2017 10:24 pm

Re: Integration with VB.NET

Post by mdw1975 »

Method loadComponentFromURL requires "/" even in the file path
My original code

Code: Select all

'The variable "Me._EditedTicketPathODT" = C:\HTI\TemplateBOL.odt
oDesk.loadComponentFromURL("file:///" & Me._EditedTicketPathODT, "_blank", 0, OpenPar)

New Code

Code: Select all

' Function CreateTrueURLFromTicketPath will replace "\" with "/" and any space in the path with "%20
oDesk.loadComponentFromURL("file:///" & CreateTrueURLFromTicketPath(Me._EditedTicketPathODT), "_blank", 0, OpenPar)
OpenOffice 4.14 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [SOLVED]Integration with VB.NET

Post by Villeroy »

Uniform Resource Locator
https://en.wikipedia.org/wiki/URL
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply