我正在使用Java中的Jacob构建powerpoint形状,我被困在一些琐碎的事情上,但我无法让它工作。我需要创建一个矩形,并改变它的填充颜色。下面是从powerpoint应用程序创建到我想要执行的颜色更改的代码。
ActiveXComponent objPPT = new ActiveXComponent("PowerPoint.Application");
objPPT.setProperty("Visible",new Variant(true));
Variant presentations = objPPT.getProperty("Presentations");
Variant presentation = Dispatch.call(presentations.getDispatch(), "Add");
Variant slides = Dispatch.get(presentation.getDispatch(), "Slides");
Variant slide = Dispatch.call(slides.getDispatch(), "Add",1, 12);
Variant shapes = Dispatch.get(slide.getDispatch(), "Shapes");
Variant rectangle= Dispatch.call(shapes.getDispatch(), "AddShape", 1, 15, 1, 20, 8);
Variant rectangleFill = Dispatch.get(effectivity.getDispatch(), "Fill");
Variant rectangleFillForeColor = Dispatch.get(rectangleFill.getDispatch(), "ForeColor");
// there I would need to create Variant rgb = RGB(10, 10, 10)
Dispatch.Put(rectangleFillForeColor.getDispatch(), "RGB", rgb);如您所见,我错过了rgb变体的创建(下面是Long类型)。
这个长值是通过调用RGB函数返回的(RGB函数本身以红色、绿色和蓝色值作为输入)。
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/rgb-function
我的问题是:我知道如何使用Jacob (Dispatch.call)调用组件上的方法,但我从未调用过实际的函数。
有人知道怎么做吗?澄清一下,我知道我可以通过自己计算长值来绕过对RGB的调用(值是red+256*(green+256*blue)),但我真的很好奇如何使用Jacob使RGB调用工作,以防有一天在不那么简单的函数上再次发生这种情况:)
发布于 2022-07-18 10:28:18
RGB()是一个VBA函数。VBA不是COM标准的一部分。因此,雅各布没有办法叫它。
发布于 2022-07-18 13:44:39
不会很漂亮的。
因为该函数是VBA的一部分,所以不能像您所说的那样调用它。不过,它可以包装。
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;
import static com.jacob.com.Dispatch.call;
import static com.jacob.com.Dispatch.get;
public class App {
static String rgbFunction =
"""
Function RGB(Red As Integer, Green As Integer, Blue As Integer) As Long
RGB = VBA.RGB(Red, Green, Blue)
End Function
""";
public static void main(String[] args) {
ActiveXComponent objPPT = new ActiveXComponent("PowerPoint.Application");
objPPT.setProperty("Visible",new Variant(true));
Variant presentations = objPPT.getProperty("Presentations");
Variant presentation = call(presentations.getDispatch(), "Add");
Variant vbProject = call(presentation.getDispatch(),"VBProject");
Variant vbComponents = get(vbProject.getDispatch(),"VBComponents" );
Variant vbComponent = call(vbComponents.getDispatch(),"Add", 1);
Variant codeModule = get(vbComponent.getDispatch(), "CodeModule");
String moduleName = get(vbComponent.getDispatch(),"Name").getString();
call(codeModule.getDispatch(), "AddFromString",rgbFunction);
System.out.println(
call(
objPPT,
"Run",
moduleName + ".RGB",
10, 20, 30
)
);
call(vbComponents.getDispatch(), "Remove", vbComponent);
}
}这将使用包装器将模块添加到您的项目中,调用它,并在结束时删除它。这要求您已经允许访问安全中心中的VBA项目模型。
https://stackoverflow.com/questions/72987467
复制相似问题