乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何在<em>Excel</em> <em>VBA</em> 中<

如何在<em>Excel</em> <em>VBA</em> 中<

作者:乔山办公网日期:

返回目录:excel表格制作


Sub AAA()
    Dim FilePath As String   '要读取的文件路径
    Dim S1       As String   '文档的内容
    Dim S2       As String   '提取到的内容
    Dim Ar       As Variant  '用于保存最终e79fa5e98193e59b9ee7ad94339结果
    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


直接读写WORD文档是非常复杂的,基本上自己写不可行.

但可以调用WORD来处理,要求就是电脑上要安装有WORD. 基本思路就是创建一个Word.application对象,再设置为对象可见,再打开某某文件. 其实代码可以在WORD中用宏录制,再移植到EXCEL中来即可.当然,要修改成WORD对象执行这些语句的形式(在WORD VBA中Application对象是默认的).

你可以在网上搜索一下创建WORD对象并使用的代码.
在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的工作页zhidao
xlapp.Application.EnableEvents = False '禁止宏等提示的运行
Set wkBook = xlapp.Workbooks.Open(ExcelFileName)

参考这个:
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列数据e79fa5e98193e59b9ee7ad94333个数

    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 '清空
    MsgBox "文件保存在我的文档底下的AAA文件"
    
End Sub

相关阅读

  • <em>VBA</em> 用excel模块复制<em>word</em>

  • 乔山办公网word文档
  • 可以完成。但要说如何完成,那就不是几句话能说清了。vba如何将excel表格的指定内容复制到word相应..." src="/uploads/tu/619.jpg" style="width: 400px; height: 267px;" />试试下面的代码:Sub 宏1() D
关键词不能为空
极力推荐

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