+ -
当前位置:首页 → 问答吧 → 如何在word 宏里调用 excel中已经编写好的宏。急。。在线等

如何在word 宏里调用 excel中已经编写好的宏。急。。在线等

时间:2007-01-20

来源:互联网

请教各位高手。

  我现在想实现的是在WORD中运行宏的时候,要调用EXCEL中已经录制的宏。

详细的代码如下:

说明;首先用WORD 打开一个EXCEL表格,已经有的文件。这个是WORD中的宏

Sub Macro1()
'
' Macro1 Macro
' 宏在2007-1-20由 adviser 录制
'
    Selection.Font.Name = "宋体"
    Selection.Font.Size = 10
    Selection.Font.Bold = True
    Selection.TypeText Text:="1. "
    Selection.InsertDateTime DateTimeFormat:="yyyy年M月", InsertAsField:=False, _
        DateLanguage:=wdSimplifiedChinese, CalendarType:=wdCalendarWestern, _
        InsertAsFullWidth:=False
    Selection.TypeText Text:="直接染料及制品进口货值、数量类比分析"
    Selection.TypeParagraph
    Set ExcelApp = CreateObject("Excel.Application")
    ExcelApp.Visible = True
    ExcelApp.Workbooks.Open "E:\11月\All companies.csv"
    Set ExcelApp = Nothing

这个过程会打开一个已有的EXCEL 文件 All companies.csv 打开

然后就得运行EXCEL 中已经录制的一个宏。这个是EXCEL 里的宏

Sub Macro4()
'
' Macro4 Macro
' 宏由 adviser 录制,时间: 2007-1-19
'

'
    Range("A1").Select
    Cells.Find(What:="company name", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, MatchByte:=False, SearchFormat:=False).Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
End Sub

然后想把这里复制的东西粘贴到原来的 WORD 文件当中。

请问各位老鸟,这个过程应该怎么实现?

还有个问题,在查看论坛的时候,看到在WORD上加栽EXCEL的代码就可以实现这个过程,可是不知道怎么可以在WORD中加栽EXCEL的代码?

我是菜鸟,恳请各位老鸟教诲。在这里先谢谢了。

 

作者: snowman77   发布时间: 2007-01-20

帮帮忙, 回一个啊。

作者: snowman77   发布时间: 2007-01-20

各位高手, 帮帮忙把。

作者: snowman77   发布时间: 2007-01-20

怎么没有人帮忙啊 ? 真是郁闷

作者: snowman77   发布时间: 2007-01-20

我没测试,直接把该宏置于WORD中即可。

Sub Macro1()
    Dim EcxelApp As Object, ExcelWk As Object, myRange As Variant
    Selection.Font.Name = "宋体"
    Selection.Font.Size = 10
    Selection.Font.Bold = True
    Selection.TypeText Text:="1. "
    Selection.InsertDateTime DateTimeFormat:="yyyy年M月", InsertAsField:=False, _
                             DateLanguage:=wdSimplifiedChinese, CalendarType:=wdCalendarWestern, _
                             InsertAsFullWidth:=False
    Selection.TypeText Text:="直接染料及制品进口货值、数量类比分析"
    Selection.TypeParagraph
    Set ExcelApp = CreateObject("Excel.Application")
    ExcelApp.Visible = True
    Set ExcelWk = ExcelApp.Workbooks.Open("E:\11月\All companies.csv")
    ExcelWk.activesheet.Cells.Find(What:="company name", After:=ActiveCell, LookIn:=xlFormulas, _
                                   LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                   MatchCase:=False, MatchByte:=False, SearchFormat:=False).Activate
    ExcelWk.activesheet.Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Copy
    Set ExcelApp = Nothing
End Sub
'----------------------


作者: 守柔   发布时间: 2007-01-21

又是老兄您帮忙, 真是感激不尽啊。 安某已经仰慕您很久了, 真是万分感激, 对你的情谊犹如滔滔江水,好比黄河之水泛滥, 一发不可收拾。

谢谢。 万分感激。

作者: snowman77   发布时间: 2007-01-22

这个真可以有

作者: 枫叶情缘   发布时间: 2010-11-25