乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何Word中用VBA打开<em>Excel</em>表格,读取其中的内容写入...-excel

如何Word中用VBA打开<em>Excel</em>表格,读取其中的内容写入...-excel

作者:乔山办公网日期:

返回目录:excel表格制作


参考这个:
Sub ExcelToWord() '  利用Word程序创建文本文件
    Dim WordApp As Object
    Dim Records As Integer, i As Integer
    Dim Region As String, SalesAmt As String, SalesNum As String, strTitle As String

    Set WordApp = CreateObject("Word.Application") '创建word对象
    Records = Application.CountA(Sheets("sheet1").Range("A:A")) 'A列数据个数

    WordApp.Documents.Add '新建文档


    '写Title
        strTitle = Cells(1, 5)
        With WordApp.Selection
            .Font.Size = 28
            .ParagraphFormat.Alignment = 1  '左对齐0  居中1 右对齐2
            .Font.Bold = True
            .TypeText Text:=strTitle
            .TypeParagraph
        End With
        
    '写内容
    For i = 1 To Records
        'Region = Data.Cells(i, 1).Value '将第一列某行的值赋值给变量
        Region = Cells(i, 1)
        'SalesNum = Data.Cells(i, 2).Value '获取该行B列数据
        SalesNum = Cells(i, 2)
        'SalesAmt = Data.Cells(i, 3).Value '获取该行C列数据
        SalesAmt = Cells(i, 3)
        
        
        
                
        With WordApp.Selection
                
                .Font.Size = 14 '设置字体字号
                .Font.Bold = True '字体粗
                .ParagraphFormat.Alignment = 0 '设置对齐
                .TypeText Text:=Region & SalesNum
                .TypeParagraph

                .Font.Size = 12 '设置字体
                .ParagraphFormat.Alignment = 0 '设置对齐
                .Font.Bold = False '字体不加粗
                .TypeText Text:=vbTab & SalesAmt
                .TypeParagraph '回车
                .TypeParagraph '回车
                
        End With
    Next i

    WordApp.ActiveDocument.SaveAs Filename:="AAA" '保存文件
    WordApp.Quit '退出程序
    Set WordApp = Nothing '清空e799bee5baa6e58685e5aeb9333
    MsgBox "文件保存在我的文档底下的AAA文件"
    
End Sub


在word vba工具-引用中选中Ms Excel就可以正常打开EXCEL进行操作了
Dim xlapp As Excel.Application ‘代表excel程序
Dim wkBook As Excel.Workbook '代表excelworkbook(也就是excel工作簿文件 .xls .xlsx)
Dim wkSheet As Excel.Worksheet '代表excel的工作页
xlapp.Application.EnableEvents = False '禁止宏等提示的运行
Set wkBook = xlapp.Workbooks.Open(ExcelFileName)
如果WORD中每一页都有文字和表格。可以在excel中选择要创建word应用程序后再打开要操作的文档,再提取word内容中表格部分内容;

Sub AAA()
    Dim FilePath As String   '要读取的文件路径
    Dim S1       As String   '文档的内容
    Dim S2       As String   '提取到的内容
    Dim Ar       As Variant  '用于保存最e69da5e6ba90e79fa5e98193339终结果
    Dim L1       As Long     '记录当前查找到的字符位置
    FilePath = Application.GetSaveAsFilename(fileFilter:="Word文档,*.doc;*.docx")
    If FilePath = "False" Then MsgBox "您没有选择文件,将退出程序。": Exit Sub
    With CreateObject("word.application")
        With .Documents.Open(FilePath, True, True)
            S1 = .Content
            .Close False
        End With
        .Quit
    End With
    L1 = InStr(S1, "<")  '第一个 < 位置
    Do Until L1 = 0
        If Len(S2) <> 0 Then
            S2 = S2 & "Crazy0qwer" & Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
        Else
            S2 = Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
        End If
        L1 = InStr(L1 + 1, S1, "<")
    Loop
    Ar = Split(S2, "Crazy0qwer")
    Range("A1").Resize(UBound(Ar) + 1) = Application.Transpose(Ar)
End Sub

相关阅读

  • 如何<em>Word</em>中用<em>VBA打开Excel</em>

  • 乔山办公网excel表格制作
  • Word中用VBA打开Excel表格,读取其中的内容写入..." src="/uploads/tu/600.jpg" style="width: 400px; height: 267px;" />可用CreateObject函数创建对EXCEL的引zd用,然后就可打开EXCEL工作簿,将表格中的数据
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网