zoukankan      html  css  js  c++  java
  • 使用VBA从工作表中读图片,以及给工作表中写文件

    因为工作的原因,需要用到VBA,碰到读图片和写图片:

    Sub Macro01()                 '从工作表中保存图片
    
        Application.ScreenUpdating = False
        
        Dim pth, shp, n
        pth = ThisWorkbook.Path & "导出图片"
        For Each shp In ActiveSheet.Shapes
        If shp.Type = 13 Then
            n = n + 1
            shp.Copy
            With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart
                .Parent.Select
                .Paste
                .Export pth & shp.TopLeftCell.Offset(0, -1) & ".jpg"
                .Parent.Delete
            End With
        End If
        Next
        Application.ScreenUpdating = True
        
    End Sub
    Sub Macro02()                   '从文件夹中读写图片
        
        Dim fso, shp, j, rng, str1, w, y
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        Application.ScreenUpdating = False
        For Each shp In ActiveSheet.Shapes
        If shp.Type = 11 Then shp.Delete
        Next shp
        For j = 5 To 70
            Cells(j, 6).Select
            Set rng = Selection
            str1 = ThisWorkbook.Path & "导出图片" & Cells(j, 6) & ".jpg"
            If fso.FileExists(str1) Then
            ActiveSheet.Pictures.Insert(str1).Select
            
            With Selection
                .Top = rng.Offset(0, 1).Top
                .Left = rng.Offset(0, 1).Left
                .Height = rng.Offset(0, 1).Height
                .Width = rng.Offset(0, 1).Left - rng.Left - 2
            End With
            End If
        Next j
        
        Application.ScreenUpdating = True
        
    End Sub
    Sub Macro04()                       '删除工作表中的图片
          
        Application.ScreenUpdating = False
        
        Dim oSP As Shape
        For Each oSP In ActiveSheet.Shapes
            If oSP.Type = 11 Then
            oSP.Delete
            End If
        Next
        
        Application.ScreenUpdating = True
          
    End Sub
                  
    申明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    父目录权限子目录权限关系
    微信自媒体账号涉违规大规模被封
    微信支付接口申请指南
    微信公众平台开发(83) 生成带参数二维码
    百度天气预报接口
    微信支付全面开放
    公众平台商户接入(微信支付)功能申请教程
    移动互联网流量变现模式调研问卷
    天气预报接口
    微信公众平台开发(82) 天气预报
  • 原文地址:https://www.cnblogs.com/lsyb-python/p/11967828.html
Copyright © 2011-2022 走看看