Hol*_*ene 9 excel vba ms-word excel-vba word-vba
我正在尝试自动化Excel VBA正在完成所有工作的一些报告生成.我的雇主有一套标准化的模板,所有文件都应该从中生成.我需要从Excel VBA填充其中一个模板.Word模板广泛使用VBA.
这是(部分)我的Excel VBA代码:
Sub GenerateReport() ' (Tables, InputDataObj)
' code generating the WordApp object (works!)
WordApp.Documents.Add Template:="Brev.dot"
' Getting user information from Utilities.Userinfo macro in Document
Call WordApp.Run("Autoexec") ' generating a public variable
Call WordApp.Run("Utilities.UserInfo")
' more code
End sub
Run Code Online (Sandbox Code Playgroud)
在Word VBA Autoexec模块中,user定义并声明了一个名为的公共变量.模块中的Userinfo子Utilities组件填充user.这两个例程都是在没有任何VBA投诉的情况下运行的.我希望能够访问user我的Excel VBA中的变量,但我收到以下错误
编译错误:尚未在此上下文中创建的变量.
如何在Excel VBA中访问Word VBA变量?我认为它或多或少是一样的?
编辑:user变量是Type仅使用String属性定义的用户.复制填充user变量的Word VBA函数绝对可行,只需要比我更多的工作......
在 Word 模块中:
Public Function GetUserVariable() As String '// or whatever data type
GetUserVariable = user
End Function
Run Code Online (Sandbox Code Playgroud)
在 Excel 模块中:
myUser = WordApp.Run("GetUserVariable")
Run Code Online (Sandbox Code Playgroud)
或者,您可以复制变量值 - 因为它被称为“user我怀疑它正在返回有关文档的用户或作者的一些信息”。在这种情况下,您可能会遇到以下情况之一:
'// Username assigned to the application
MsgBox WordApp.UserName
'// Username defined by the system
MsgBox Environ$("USERNAME")
'// Name of the author of the file specified
MsgBox CreateObject("Shell.Application").Namespace("C:\Users\Documents").GetDetailsOf("MyDocument.doc", 9)
Run Code Online (Sandbox Code Playgroud)