ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: jsgj2023

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 13:31 | 显示全部楼层
Sub 创建和保存word文档()
    Dim res
    Dim docApp As Object
    Application.StatusBar = "创建新的Word文档......"
    Set docApp = CreateObject("Word.Application")
    With docApp
        .Visible = True
        Application.StatusBar = "创建新的Word文档..."
        .Documents.Add
        .ActiveDocument.Paragraphs(1).Range.InsertBefore "Excel VBA实用操作技巧"
        .Application.StatusBar = "保存文档......"
        .ActiveDocument.SaveAs ThisWorkbook.Path & "\Excel VBA实用操作技巧.doc"
        Application.StatusBar = "退出新建的Word文档..."
        res = MsgBox("已经创建了Word文档,是否要关闭Word文档?", vbYesNo)
        If res = vbYes Then
            .Quit
        End If
    End With
    Set docApp = Nothing
    Application.StatusBar = False
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 13:44 | 显示全部楼层

Sub 打开指定的word文档()
    Dim myFile As String
    Dim docApp As Word.Application
    myFile = ThisWorkbook.Path & "\Excel VBA实用操作技巧.doc"
    Set docApp = New Word.Application
    With docApp
        .Documents.Open myFile
        .Visible = True
    End With
    Set docApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 13:47 | 显示全部楼层
Sub 打开指定的word文档_创建对象()
    Dim myFile As String
    Dim docApp As Object
    '指定要打开的Word文档
    myFile = ThisWorkbook.Path & "\Excel VBA实用操作技巧.doc"
    Set docApp = CreateObject("Word.Application")
    With docApp
        .Documents.Open myFile
        .Visible = True
    End With
    Set docApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 13:57 | 显示全部楼层
Sub 复制word文档第4行以下的数据到表格()
Dim myfile As String
Dim docapp As Word.Application
Dim docrange  As Word.Range
Dim ws  As Worksheet
myfile = ThisWorkbook.Path & "\操作说明.doc"
Set docapp = New Word.Application
docapp.Documents.Open myfile
With docapp.ActiveDocument
    If .Paragraphs.Count >= 4 Then
        Set docrange = .Paragraphs(4).Range
        docrange.Copy
    End If
End With
ws.Range("a1").Activate
ws.Paste
docapp.Quit
Set docrange = Nothing
Set docapp = Nothing
Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 14:15 | 显示全部楼层
Sub 创建ppt文档()
Dim pptapp As PowerPoint.Application
Dim pptprs As PowerPoint.Presentation
Dim myppt As String
Dim res
myppt = ThisWorkbook.Path & "\PowerPoint1.ppt"
Set pptapp = New PowerPoint.Application
pptapp.Visible = True
Set pptprs = pptapp.Presentations.Add
With pptprs
    With .Slides.Add(Index:=1, Layout:=ppLayoutText).Shapes
        .Title.TextFrame.TextRange = "这是PowerPoint试验标题"
        .Range(2).TextFrame.TextRange = "这是PowerPoint试验正文"
    End With
    res = MsgBox("已经创建PowerPoint文档,是否要保存并关闭?", vbYesNo)
    If res = vbYes Then
        .SaveAs myppt
        .Close
        pptapp.Quit
    End If
End With
Set pptprs = Nothing
Set pptapp = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-22 14:32 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 15:12 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 打开指定的ppt文档()
    Dim pptApp As PowerPoint.Application
    Dim pptPrs As PowerPoint.Presentation
    Dim myppt As String
    myppt = ThisWorkbook.Path & "\Adele.ppt"
    Set pptApp = New PowerPoint.Application
    pptApp.Visible = True
    Set pptPrs = pptApp.Presentations.Open(myppt)
    Set pptPrs = Nothing
    Set pptApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 16:03 | 显示全部楼层
Sub 电话号码添加分隔符()
arr = Sheets("通讯录").Range("a1").CurrentRegion
For x = 2 To UBound(arr)
    For y = 1 To UBound(arr, 2)
        If IsNumeric(arr(x, y)) Then
            s = arr(x, y)
            s1 = Mid(s, 1, 3)
            s2 = Mid(s, 4, 4)
            s3 = Mid(s, 8, 4)
            arr(x, y) = s1 & "-" & s2 & "-" & s3
        End If
    Next y
Next x
[a1].Resize(UBound(arr), UBound(arr, 2)) = arr
Cells.EntireColumn.AutoFit
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-22 17:17 | 显示全部楼层
Sub 根据表格内容创建PPT文档()
    Dim ws As Worksheet
    Dim pptApp As PowerPoint.Application
    Dim pptPrs As PowerPoint.Presentation
    Dim i As Long
    Dim myppt As String
    myppt = ThisWorkbook.Path & "\PowerPoint试验.ppt"
    Set ws = Worksheets(1)
    Set pptApp = New PowerPoint.Application
    pptApp.Visible = True
    Set pptPrs = pptApp.Presentations.Add
    With pptPrs
        For i = 1 To 5
            With .Slides.Add(Index:=i, Layout:=ppLayoutText).Shapes
                .Title.TextFrame.TextRange = ws.Cells(i, 1).Text     '添加标题
                .Range(2).TextFrame.TextRange = ws.Cells(i + 1, 2).Text _
                    & vbCrLf & ws.Cells(i + 1, 3).Text _
                    & vbCrLf & ws.Cells(i + 1, 4).Text        '添加正文
            End With
        Next i
        .SaveAs myppt
'        .Close         '设置此语句可关闭创建的文档
    End With
'    pptApp.Quit       '设置此语句可关闭PowerPoint应用程序
    Set ws = Nothing
    Set pptPrs = Nothing
    Set pptApp = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-22 20:24 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-7-1 00:18 , Processed in 0.038433 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表