VBScript and VBA - namespace issues - vba

I want to automate some steps using VBA, and I also want to be able to execute everything from the command line. In my example I'm using Corel Draw, but I think that my question is independent from Corel Draw - its more a question about namespaces.
So I wrote the following small script (called foo.vbs):
dim drawApp
Set drawApp = CreateObject( "CorelDraw.Application.14" )
drawApp.OpenDocument( "C:\foo\bar.cdr" )
Dim exportFilter
set exportFilter = drawApp.ActiveDocument.ExportBitmap("bar.png", cdrPNG, cdrAllPages, cdrRGBColorImage, 10206, 8578, 300, 300, cdrNormalAntiAliasing, False, False, True, False, cdrCompressionNone)
Then, I run it from the command line:
cscript.exe foo.vbs
I get the error that cdrPNG is not defined. Of course - I did not include any Corel Draw specific stuff into the VBScript. But how do I incluce VBA stuff that is specific for one application? (This might also enable me to write dim drawApp as CorelDRAW.Application or something like that).
I'm very new to VBA and VBScripts and I have not been able to find good tutorials or reference resources (the Microsoft site is not very helpful). Any pointers welcome.
EDIT:
I copied the ExportBitmap-part from the code that was generated when I recored a macro in Corel Draw. Studying code from recored macros seems a nice way to get to know the VBA capabilities of the software. Is there a better way?

If you considered to use a .wsf script package instead .vbs script file, everything would be easier for you. Adding type libraries supported in WSF files.
<package>
<job id="Main">
<!--
add type library reference specifying progid and version
then all enum constants will be ready to use
-->
<reference object="CorelDraw.Application" version="14.0" />
<script language="VBScript">
Option Explicit
Dim drawApp
Set drawApp = CreateObject("CorelDraw.Application.14")
drawApp.OpenDocument("C:\foo\bar.cdr")
Dim exportFilter
Set exportFilter = drawApp.ActiveDocument.ExportBitmap( _
"C:\foo\bar.png", cdrPNG, cdrAllPages, cdrRGBColorImage, 10206, 8578, 300, 300, _
cdrNormalAntiAliasing, False, False, True, False, cdrCompressionNone, Nothing, Nothing)
exportFilter.Finish
drawApp.Quit
</script>
</job>
</package>

Related

How to program a PowerPoint custom object like Thinkcell objects?

I am very new to PowerPoint add-on development so please bear with me.
There are many objects that can be inserted into a PowerPoint slide, such as text box, shape, chart, etc. Is there a way to build my own custom object with custom properties and functionalities? For example, with the Thinkcell add-on, you can insert a Thinkcell chart object, connect it to Excel workbook, edit it, and show it. To the best of my understanding, this is a kind of custom object built into the Thinkcell add-on.
How can I build a custom object like this?
I have checked some tutorials on building a PowerPoint add-on with VBA, VSTO, and Javascript. To the best of my understanding, these technologies allow users to build some user interfaces to interact and modify the PowerPoint slides, such as creating and modifying elements in the slides. However, I don't see examples of creating a custom object using these technologies. Thus I am very curious about how add-ons create their custom element.
Thanks!
First, you will need to install the necessary npm packages and set up the development environment.
For example, you can use the following command to install the Office.js library:
npm install #microsoft/office-js
Then, you can use the following code to create a custom object in a PowerPoint add-in:
Office.initialize = function () {
// Create a new shape on the active slide
var shape = PowerPoint.createShape("MyCustomObject", Office.MsoAutoShapeType.msoShapeRectangle, 50, 50, 100, 100);
// Add a custom property to the shape
shape.customProperties.add("dataSource", "Excel workbook");
// Define a function to be called when the user clicks on the shape
shape.click = function () {
// Open the Excel workbook and display the data
Office.context.ui.displayDialogAsync("https://example.com/excel-workbook", {height: 50, width: 50});
};
};
Here is how you might accomplish this using VBA:
Sub ChangeObjectColor()
Dim obj As Shape
' Get a reference to the custom object
Set obj = ActivePresentation.Slides(1).Shapes("MyObject")
' Check the value of the FillColor property
If obj.FillColor = "red" Then
' Set the fill color of the object to red
obj.Fill.ForeColor.RGB = RGB(255, 0, 0)
ElseIf obj.FillColor = "green" Then
' Set the fill color of the object to green
obj.Fill.ForeColor.RGB = RGB(0, 255, 0)
End If
End Sub

How to start slideshow in PowerPoint using com32

I am trying to control my PowerPoint presentation using python via com.
There is an article on how to do this here and it suggest I do the following:
app = win32com.client.Dispatch("PowerPoint.Application")
objCOM = app.Presentations.Open(FileName="path_to_file", WithWindow=1)
objCOM.SlideShowWindow.View.Next()
If I do this, I get the error
(-2147352567, 'Ausnahmefehler aufgetreten.', (0, 'Microsoft PowerPoint', 'Presentation.SlideShowWindow : Invalid request. There is currently no slide show view for this presentation.', '', 0, -2147188160), None)
On the last line. It seems, that Open does not start the slide show.
I have been looking through the documentation of the PowerPoint object model here, but was unable to find a way to start the slide show of a presentation.
Any suggestions on how to do this?
Call SlideShowSettings.Run() after opening the file:
import win32com.client as win32
app = win32.Dispatch("PowerPoint.Application")
objCOM = app.Presentations.Open(FileName="path_to_file", WithWindow=1)
#START THE SLIDESHOW
objCOM.SlideShowSettings.Run()
objCOM.SlideShowWindow.View.Next()

SmartGWT pdf export

Has anyone successfully used SmartGWT 3.x pdf export?
My client code looks like this:
DSRequest requestProperties = new DSRequest();
requestProperties.setExportFilename("File.pdf");
requestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
requestProperties.setContentType("application/pdf");
RPCManager.exportContent(table, requestProperties);
When the code run nothing happens. Do I have to do anything server side?
I can just add that my application is successfully using the SmartGWT excel export from the list grid.
I also tried in vain to find the documentation about this. But it's not that hard. Your code seems right, have added a canvas to print and the line requestProperties.setDownloadResult(true);
final Canvas canvas = new Canvas();
canvas.setWidth(300);
canvas.setBorder("2px solid Red");
DynamicForm formPrint = new DynamicForm();
formPrint.setWidth(200);
formPrint.setHeight(100);
formPrint.setTop(20);
formPrint.setLeft(50);
formPrint.setBorder("2px solid Black");
TextItem textItem = new TextItem();
textItem.setName("NameBo");
textItem.setTitle("Title");
textItem.setValue("Value goes here...");
formPrint.setFields(textItem);
canvas.addChild(formPrint);
canvas.draw(); // to view onscreen
DSRequest requestProperties = new DSRequest();
requestProperties.setExportFilename("File");
requestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
requestProperties.setContentType("application/pdf");
requestProperties.setDownloadResult(true);
RPCManager.exportContent(canvas, requestProperties);
I then added the following jars from the smartgwtEE lib folder (in eclipse .classpath)
<classpathentry kind="var" path="SGWTEE_HOME/lib/isomorphic_contentexport.jar"/>
<classpathentry kind="var" path="SGWTEE_HOME/lib/iText-2.0.8.jar"/>
<classpathentry kind="var" path="SGWTEE_HOME/lib/jtidy-r938.jar"/>
And that's all that was to it :-)
The answer to your question is yes: countless developers have successfully used SmartGWT's PDF Export. Now gimme my points please ;)
To troubleshoot, look in your server logs for errors.

Modify HTML in a Internet Explorer window using external.menuArguments

I've got a VB.NET class that is invoked with a context menu extension in Internet Explorer.
The code has access to the object model of the page, and reading data is not a problem. This is the code of a test function...it changes the status bar text (OK), prints the page HTML (OK), changes the HTML by adding a text and prints again the page HTML (OK, in the second pop-up my added text is in the HTML)
But the Internet Explorer window doesn't show it. Where am I doing wrong?
Public Sub CallingTest(ByRef Source As Object)
Dim D As mshtml.HTMLDocument = Source.document
Source.status = "Working..."
Dim H As String = D.documentElement.innerHTML()
MsgBox(H)
D.documentElement.insertAdjacentText("beforeEnd", "ThisIsATest")
H = D.documentElement.outerHTML()
MsgBox(H)
Source.status = ""
End Sub
The function is called like this from JavaScript:
<script>
var EB = new ActiveXObject("MyObject.MyClass");
EB.CallingTest(external.menuArguments);
</script>
To the best of my understanding, in order to use insertAdjacentText or any of the other editing methods, the document object should be in the design mode.
In design mode you can edit the document freely, and so can the user.
Check this site for more details
I do not think that Alex is right, something else is the matter.
When I tried to do something like that, insertBefore would not work for me, but appendChild worked just fine, so adding an element is possible.
I worked in Javascript, but I don't expect that makes a difference.

Visual Studio Tools for Office 2008 - using a ribbon button to interact with active document

I've created my own ribbon toolbar tab with a few buttons. I can add text and similar actions to the document I'm working on. Now I want to add a button that will save the document I'm working on without using the Word save button because I want to set some of the parameters.
Every example I found showed how to save a document that was started by my code (Dim MyDoc As New Word.Application) but when I use such syntax from the ribbon button - ActiveDocument is saying that there is no active document.
Any ideas?
ThisAddIn.vb contains:
Protected Overrides Function CreateRibbonExtensibilityObject() As _
Microsoft.Office.Core.IRibbonExtensibility
Return New MyRibbon()
End Function
MyRibbon.xml is very basic (taken from an MS sample)
<group id="ContentGroup" label="Content">
<button id="textButton" label="Insert Text"
screentip="Text" onAction="OnTextButton"
supertip="Inserts text at the cursor location."
/>
</group>
The new document that you have created is not going to be of type Word.Application. Your ribbon/add-in is running in a current Word.Application context.
If this is indeed what you are doing, you should be creating instances of Word.Document, and saving those.
What exactly is the code you are using to create the document, the ribbon, and save your changes?