热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用VB脚本运行MicrosoftOfficeVBA宏-RunningMicrosoftOfficeVBAmacrowithVBscript

IamworkingonaprojectaimedatshowinghowtorunamacroinMicrosoftOfficeusinganexternal

I am working on a project aimed at showing how to run a macro in Microsoft Office using an external Vbscript file. I have tried severally and failed at it also found a couple of posts here, all leaned towards Excel than Office. Having difficulties getting those examples to work on Office.

我正在开展一个项目,旨在展示如何使用外部Vbscript文件在Microsoft Office中运行宏。我已经尝试了几次并且失败了,它也发现了几个帖子,都倾向于Excel而不是Office。难以将这些示例用于Office。

1 个解决方案

#1


This applies to Excel - but the steps and procedures are identical in Word. I would search and replace Word for Excel but the code samples wouldn't make sense.

这适用于Excel - 但步骤和过程在Word中是相同的。我会搜索并替换Word for Excel,但代码示例没有意义。

Record the steps in excel macro recorder. You have to rewrite it a bit because it uses a type of syntax that vbs doesn't.

在excel宏录制器中记录步骤。你必须重写它,因为它使用了一种vbs没有的语法。

This applies (I don't have a medium9) xlRangeAutoFormatAccounting4 in vba.

这适用于(我没有medium9)xlRangeAutoFormatAccounting4在vba中。

Selection.AutoFormat Format:=xlRangeAutoFormatAccounting4, Number:=True, _
    Font:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True

So first look up constants in vba's object browser. xlRangeAutoFormatAccounting4 = 17

所以首先在vba的对象浏览器中查找常量。 xlRangeAutoFormatAccounting4 = 17

Then look the function up in object browser and look at the bottom for the function definition,.

然后在对象浏览器中查看该函数,并查看函数定义的底部。

Function AutoFormat([Format As XlRangeAutoFormat = xlRangeAutoFormatClassic1], [Number], [Font], [Alignment], [Border], [Pattern], [Width])

So the vba becomes in vbs (and vbs works in vba) (and as you can see you can work out the correct way without needing to look the function up usually)

所以vba变成了vbs(和vbs在vba中工作)(正如你所看到的,你可以通过正确的方式计算出来,而不需要通常查看函数)

Selection.AutoFormat 17, True, True, True,True, True, True

So your code becomes

所以你的代码变成了

objXLWs.Range("A3").CurrentRegion.Select.AutoFormat 17, True, True, True,True, True, True

You are using Excel and you can record it in Excel and have Excel write your code.

您正在使用Excel,您可以将其记录在Excel中并让Excel编写您的代码。

Alt + T, M, R

Alt + T,M,R

then Home key then Up Arrow. Stop recording.

然后是Home键然后向上箭头。停止录音。

Look what Excel wrote

看看Excel写的是什么

Selection.End(xlUp).Select

or if you had of recorded Go To dialog

或者如果您有录制的“转到”对话框

Application.Goto Reference:="R1C1"

or if you had of recorded Ctrl + Home

或者如果您有录制的Ctrl + Home

Range("A1").Select

推荐阅读
author-avatar
平凡书生518
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有