How to interact from Office VBA with a Libre Sheet - vba

How do I get as Object the LibreOfficeCalcPortable.exe to interact from Catia VBA. I write bellow the way to interact with MS Excel from Catia VBA. how to write within a Cell from LibreOfficeCalcPortable.exe from CATIA VBA?
Example Catia to Excel.
Dim Excel As Object
On Error Resume Next
Set Excel = GetObject(, "EXCEL.Application")
If Err.Number <> 0 Then
Err.Clear
Set Excel = CreateObject("Excel.Application")
End If
Thank you!

Here is a Calc example in VBScript which I tested just now in LibreOffice 7.2. See the comment by #FaneDuru regarding how to adapt for VBA.
Set oSM = CreateObject("com.sun.star.ServiceManager")
Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
Dim arg()
Set wb = oDesk.loadComponentFromURL("private:factory/scalc", "_blank", 0, arg)
Set oSheet = wb.CurrentController.ActiveSheet
oSheet.getCellByPosition(1, 2).String = "Hello world!"
MsgBox "The End"

Related

How to save an embedded Word document in an Excel workbook as a separate file using VBA

I am creating a workbook that will populate an embedded Word document template with pictures, and then save the document elsewhere, without editing the embedded template.
However, when I try to save the document, I get a Run-Time Error 4605 telling me:
"The SaveAs method or property is not available because this document is being edited in another applicator"
This is the sub to open the template:
Sub OpenWord()
'Opens the template when the main function first runs
Set WDObj = Sheets("Template").OLEObjects("Template")
WDObj.Activate
WDObj.Object.Application.Visible = False
Set WDApp = GetObject(, "Word.Application")
Set WDDoc = WDApp.ActiveDocument
End Sub
After this a main sub runs which populates the template, then when I try to save the document using:
WDDoc.SaveAs "myDocument.doc", FileFormat:=wdFormatDocumentDefault
I get the error.
Please has anyone encountered this error before/ know to to fix it, I have done much Googling which still has not gotten me anywhere.
I tested the following, which worked on my system/installation:
Sub OpenWord()
'Opens the template when the main function first runs
Dim WDObj As Object
Dim WDApp As Object
Set WDApp = GetObject(, "Word.Application")
Set WDObj = Sheets("Template").OLEObjects("Template")
WDObj.Activate
WDObj.Object.Application.Visible = False
WDApp.ActiveDocument.SaveAs ("YourFilename.doc")
Set WDObj = Nothing
Set WDApp = Nothing
End Sub

Copying a Range from Excel and Pasting it into Powerpoint NOT as a metafile but as a table which you can edit in PPP

All I want to do is copy a range from Excel and then paste this range into PowerPoint.
When my range is manually copied from Excel to the clipboard... If I right click on a blank slide when pasting into PowerPoint, it gives me the option to paste "using destination styles".
This means that I can edit the resulting table. This is the result I want.
So far I have only found solutions which involve pasting in as a metafile.
If you have a solution please can you show me the full code including the dimensions as VBA in PowerPoint really isn't my cup of tea.
Thanks in advance for restoring my sanity!
Private Sub CommandButton2_Click()
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim cht As Excel.ChartObject
'Look for existing instance
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Show the PowerPoint
newPowerPoint.Visible = True
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.count)
ActiveSheet.Range("d51:d57").Copy
activeSlide.Shapes.PasteSpecial ppPasteEnhancedMetafile
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 165
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 395
AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub
There appears to be no corresponding method in the PowerPoint object model. The only way to do this is to call the ribbon button itself:
ActiveSheet.Range("d51:d57").Copy
newPowerPoint.CommandBars.ExecuteMso("PasteExcelTableSourceFormatting")
BTW: To find the list of ribbon buttons, search for "Office 2010 Control IDs".

How to fix downward incompatibility of VBA Macro for getting Excel data in MS-Word 2007? [duplicate]

My VB 6 program code is referring to 14.0 object library, it is compiled and executed..working fine with the new code (accessing 14.0 libraries).
In my development system, Microsoft Office 2010 is installed (14.0 library), where my new code is compiled and working fine..
I'm using these libraries to convert document to pdf.
I'm trying to install the setup of the same VB 6 program and run in different machine, where 14.0 libraries are not present, because MS Office 2000 is installed on that machine (12.0 libraries).
Setup installation is getting successful, but the program is throwing an error while referencing 14.0 libraries.
Now, I need a help in this regard, that, How can I install the 14.0 object libraries on the new machine, so that the references will be there for the program to run.
Or, please suggest me any other approaches to get it done..
Thanks
The problem is that you are using Early Binding. You have to use Late Binding.
In Early Binding, you set a reference to the Excel Object xx.xx Library where as in late Binding you do not.
Here is an example of both
Early Binding
Sub EarlyBindingEx()
Dim oXLApp As Excel.Application
Dim oXLWb As Excel.Workbook
Dim oXLWs As Excel.Worksheet
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = New Excel.Application
End If
Err.Clear
On Error GoTo 0
'~~> Show Excel
oXLApp.Visible = True
'~~> Open files
Set oXLWb = oXLApp.Workbooks.Open("C:\Sample.xlsx")
'~~> Set the relevant worksheet
Set oXLWs = oXLWb.Sheets(1)
'
'~~> Rest of your code
'
End Sub
Late Binding
Sub LateBindingEx()
Dim oXLApp As Object
Dim oXLWb As Object, oXLWs As Object
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
'~~> Show Excel
oXLApp.Visible = True
'~~> Open files
Set oXLWb = oXLApp.Workbooks.Open("C:\Sample.xlsx")
'~~> Set the relevant worksheet
Set oXLWs = oXLWb.Sheets(1)
'
'~~> Rest of your code
'
End Sub
HERE is an MSDN link regarding Early Binding Vs Late Binding.

How to switch to open Excel spreadsheet and work on it from Word with VBA

I'm just trying to work on an open Excel spreadsheet from Word using VBA. I did a search on this and found the following code on How to get reference to an open Excel spreadsheet from Word?. The problem is, it seems to open a separate instance of Excel rather than the one I already have open, and then closes it after the action. How can I get the procedure to switch to the open Excel spreadsheet, perform the desired actions, and leave the spreadsheet open?
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value 'Here's where I would like to insert my code
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Thanks for any assistance!
Thanks again to dcromley for his earlier response. In the meantime, here's what I came up after a couple more searches on the Internet. It takes care of the situation where the Excel document is already open. Hopefully that will help others with similar situations, although it's not exhaustive (for example, if several Excel documents are open).
Sub DoStuffWithExcelInWordRevised()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.xlsx")
Set wk = wkbk.Sheets(1)
End If
xl.Visible = True
With xl
' Insert Excel code here
End With
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Try:
Add xl.Visible = True
Del xl.Quit
It appears that you are trying to do this with a Workbook that is already open. Since you want to use the open workbook / application then you need to:
Set xl = GetObject(,"Excel.Application")

Excel VBA: Copy XL named range values to DOC bookmarks, then export to PDF

I'm trying to copy the values from a named range in Excel to a bookmark in Word. I found this code on the web that does it in Excel VBA, but I'm getting an Error 13.
Set pappWord = CreateObject("Word.Application")
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName
'Activate word and display document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 0
.Activate
End With
I know that the line that is causing the error is:
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
What am i doing wrong? Also, how & where would I code so that I can export the doc to PDF?
Thanks in advance.
Note: I've already selected the reference to the Microsoft Word (version number 14) Object model in Excel
so I use it to accomplish this task but taking an image from formatted Excel table.
Sub FromExcelToWord()
Dim rg As Range
For Each xlName In wb.Names
If docWord.Bookmarks.Exists(xlName.Name) Then
Set rg = Range(xlName.Value)
rg.Copy
docWord.ActiveWindow.Selection.Goto what:=-1, Name:=xlName.Name
docWord.ActiveWindow.Selection.PasteSpecial link:=False, DataType:=wdPasteEnhancedMetafile, Placement:= _
0, DisplayAsIcon:=False
End If
Next xlName
End Sub
Just curious... Why are you adding a document rather than opening the relevant doc which has the bookmarks? Try this code (I usually test the code before posting but I haven't tested this particular code. Just quickly wrote it)
Also I am using Late Binding so no reference to the Word Object Library is required.
Sub Sample()
Dim wb As Workbook
Dim pappWord As Object, docWord As Object
Dim FlName As String
Dim xlName As Name
FlName = "C:\MyDoc.Doc" '<~~ Name of the file which has bookmarks
'~~> Establish an Word application object
On Error Resume Next
Set pappWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set pappWord = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
Set docWord = pappWord.Documents.Open(FlName)
Set wb = ActiveWorkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName).Value
End If
Next xlName
'Activate word and display document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 0
.Activate
End With
End Sub
EDIT
Changed
Range(xlName.Value)
to
Range(xlName).Value
Now the above code is TRIED AND TESTED :)