精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● VB和Basic>>〓〓.开发和相关文档.〓〓>>Re:請教精通vba的高手:可是不知道如何讓word宏接受vb應用程序傳過

主题:Re:請教精通vba的高手:可是不知道如何讓word宏接受vb應用程序傳過
发信人: hn_lzh(吸海垂虹)
整理人: fishy(2001-05-18 19:23:17), 站内信件
問了n次都沒有人回答,失望!!!!
我在微軟的网絡找到答案了,有興趣的朋友可以看看,原來如此簡單,哈哈.......
This article discusses a method you can use to pass a parameter to a Microsoft Word Visual Basic for Applications macro when using automation from another application. 

To pass a parameter to a Word Visual Basic for Applications macro using Automation, you need to move your Word macro code to the ThisDocument Object module of the document or template containing the macro you are calling. 
To move your macro to the ThisDocument module, follow these steps: 
Open the document or template containing the macro you are calling. 
Press ALT+F11 to open the Word Visual Basic for Applications Editor. 
Select the macro you want to move or copy. 
On the Edit menu click Copy to copy your macro or click Cut to move your macro. 
In the Project Explorer, double-click the document or template project to view "Microsoft Word Objects." 
Double-click "Microsoft Word Objects," and then double-click "ThisDocument." 
Click in the "ThisDocument" code window. 
On the Edit menu click Paste to paste your code into the ThisDocument Object module. For example, if the following macro existed in Word, paste it into the ThisDocument Object module: 

In the application you are using to automate Word, you can use one of the following example macros to call and pass a parameter to your Word macro. 
Accessing Your Word Macro by Directly Opening a Document or Template
Use the following macro to open a document template or a document "based" on a template containing your Word macro. 
   Sub AutomateWord_OpenDoc()
      Dim wrdApp As Object
      Dim wrdDoc As Object
      Dim strFileName As String
      Set wrdApp = CreateObject("Word.Application")

      On Error GoTo DocError

      ' Replace the following example string value with the path and
      ' file name of the document or template containing your macro.
      strFileName = "<Path and Filename of document or template>"

      ' Open the document or template and set a variable equal to the
      ' document or template.
      Set wrdDoc = wrdApp.Documents.Open(strFileName)

      ' Run the macro. (Replace "MyWordMacro" with the name of your macro.)
      wrdDoc.MyWordMacro ("This is a test.")

   DocError:
      If Err.Number <> 0 Then Msgbox Err.Description

      ' Quit this instance of Word.
      wrdApp.Quit

      ' Clear variable memory.
      Set wrdApp = Nothing
      Set wrdDoc = Nothing

   End Sub 



----
有緣則聚,
無緣相忘于江湖...........          

[关闭][返回]