vb6 excel 实时错误‘91’对象变量或with块变量未设置

LIXIYO 2016-07-27 03:33:52
一段代码\,实现的效果是:将几幅图片传入excel表格内,以供用户打印
下面程序目的是,取一幅图片放到excel表格内并使图片居中显示,
Public Sub CopyBmpOne(i As Integer)
With XlApp.Selection
dCellW = Selection.Width
' ' dCellW = XlSheet.Cells(5, 1).Selection.Height

dCellH = Selection.Height
' End With

Clipboard.Clear
Clipboard.SetData img1Show(i).Picture


XlSheet.PasteSpecial Format:="位图", Link:=False, DisplayAsIcon:=False
'ActiveSheet.Pictures.Insert(img1Show(0).Picture).Select

dPicW = Selection.ShapeRange.Width
dPicH = Selection.ShapeRange.Height
'重新定位
Selection.ShapeRange.IncrementLeft (dCellW - dPicW) / 2
Selection.ShapeRange.IncrementTop (dCellH - dPicH) / 2
End With
End Sub


我在使用过程中是反复调用的
XlSheet.Cells(5, 1).Select
Call CopyBmpOne(0)

XlSheet.Cells(5, 3).Select
Call CopyBmpOne(1)

XlSheet.Cells(5, 5).Select
Call CopyBmpOne(2)


XlSheet.Cells(5, 7).Select
Call CopyBmpOne(3)


第一次使用时没有问题,关闭退出,再打开也没问题。
当打开excel表格,再关闭表格,再次调用这段程序时就出错“实时错误‘91’对象变量或with块变量未设置”“实时错误‘91’对象变量或with块变量未设置”
为什么会出现这个问题呢?
...全文
907 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
LIXIYO 2016-07-28
  • 打赏
  • 举报
回复
怎么一调整帖子,代码都乱了
Public Sub CopyBmpOne(i As Integer)
    With XlApp.Selection
      dCellW = Selection.Width
    '  ' dCellW = XlSheet.Cells(5, 1).Selection.Height
       
     dCellH = Selection.Height
    '  End With
      
      Clipboard.Clear
      Clipboard.SetData img1Show(i).Picture
      
      
      XlSheet.PasteSpecial Format:="位图", Link:=False, DisplayAsIcon:=False
      'ActiveSheet.Pictures.Insert(img1Show(0).Picture).Select
      
      dPicW = Selection.ShapeRange.Width
      dPicH = Selection.ShapeRange.Height
      '重新定位
      Selection.ShapeRange.IncrementLeft (dCellW - dPicW) / 2
      Selection.ShapeRange.IncrementTop (dCellH - dPicH) / 2
    End With
End Sub
这个是用来导入图片到excel并且使图片在单元格内居中显示 实际使用时我会重复调用
XlSheet.Cells(5, 1).Select
    Call CopyBmpOne(0)
   
    XlSheet.Cells(5, 3).Select
    Call CopyBmpOne(1)
        
    XlSheet.Cells(5, 5).Select
    Call CopyBmpOne(2)
    
    
    XlSheet.Cells(5, 7).Select
    Call CopyBmpOne(3)
出错在dCellW = Selection.Width这里。会提示实时错误‘91’对象变量或with块未设置
无·法 2016-07-28
  • 打赏
  • 举报
回复
点击调试,看错误定位在哪一行呢
LIXIYO 2016-07-28
  • 打赏
  • 举报
回复
哪位高手能给解惑下?谢谢~!
LIXIYO 2016-07-28
  • 打赏
  • 举报
回复
如果把这句屏蔽dCellW = Selection.Width 那么错误就在下一句dCellH = Selection.Height 再屏蔽就是这两条出错dPicW = Selection.ShapeRange.Width dPicH = Selection.ShapeRange.Height 我感觉是这个selection 有问题了 。但不知道为什么错。怎么改正
LIXIYO 2016-07-28
  • 打赏
  • 举报
回复
今天人很少啊!有高手在么?指点下!

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧