Office【宏(VBA)】内部【读写】word文档最佳方案

一旦看到要创建部件、执行读取过程中有新窗口打开关闭,那这个方法就操作各种隐患,绝对不是最优解。

1.读取office文件及各种图片、文档

Set doc = Application.ActiveDocument
doc.Tables(1).Cell(3, 2).Range.InsertFile "C:\1.doc", "", False, False, False

这里是在word文档中插入的表格插入了内容,在表格里同样适用

doc.Tables(1).Cell(3, 2).Range就是单元格对象

2.保存内容

这也是非常简单

Application.Quit

看似关闭,实则因为更改了文本,他会询问是否保存

但如果只想保存word文档中的部分插入的表格呢?

如图,画叉的是不要的 

首先,我们新建文档

Set wordApp = New Word.Application

      With wordApp
          .Visible = True
         .Documents.Add
      End With

再新建表格

Set Tbl = .ActiveDocument.Tables.Add(.ActiveDocument.Range(0, 0), numrows:=3, numcolumns:=2) '在文档开头插入一个3行2列的表格
 
  With Tbl.Borders   '设置表格边框线为单实线
        .InsideLineStyle = wdLineStyleSingle
        .OutsideLineStyle = wdLineStyleSingle
  End With

再同步内容

Set wordApp = New Word.Application

      With wordApp
         .Visible = True
         .Documents.Add
         
Set Tbl = .ActiveDocument.Tables.Add(.ActiveDocument.Range(0, 0), numrows:=3, numcolumns:=2) 
         .ActiveDocument.Tables(1).Cell(1, 1).Range = Application.ActiveDocument.Tables(1).Cell(1, 1).Range
         .ActiveDocument.Tables(1).Cell(2, 1).Range = Application.ActiveDocument.Tables(1).Cell(2, 1).Range
         .ActiveDocument.Tables(1).Cell(3, 1).Range = Application.ActiveDocument.Tables(1).Cell(3, 1).Range
         
         .ActiveDocument.Tables(1).Cell(1, 2).Range = Application.ActiveDocument.Tables(1).Cell(1, 2).Range
         .ActiveDocument.Tables(1).Cell(2, 2).Range = Application.ActiveDocument.Tables(1).Cell(2, 2).Range
End With

最后,就是保存了

With wordApp
    .Quit
End With

以下是完整代码

Private Sub CommandButton2_Click()
      Set wordApp = New Word.Application

      With wordApp
         .Visible = True
         .Documents.Add
         
Set Tbl = .ActiveDocument.Tables.Add(.ActiveDocument.Range(0, 0), numrows:=3, numcolumns:=2)
 
      With Tbl.Borders   '设置表格边框线为单实线
        .InsideLineStyle = wdLineStyleSingle
        .OutsideLineStyle = wdLineStyleSingle
      End With
         .ActiveDocument.Tables(1).Cell(1, 1).Range = Application.ActiveDocument.Tables(1).Cell(1, 1).Range
         .ActiveDocument.Tables(1).Cell(2, 1).Range = Application.ActiveDocument.Tables(1).Cell(2, 1).Range
         .ActiveDocument.Tables(1).Cell(3, 1).Range = Application.ActiveDocument.Tables(1).Cell(3, 1).Range
         
         .ActiveDocument.Tables(1).Cell(1, 2).Range = Application.ActiveDocument.Tables(1).Cell(1, 2).Range
         .ActiveDocument.Tables(1).Cell(2, 2).Range = Application.ActiveDocument.Tables(1).Cell(2, 2).Range

         .ActiveDocument.Tables(1).Cell(3, 2).Range.InsertFile myapppath, "", False, False, False'!!!!!这里的myapppath是上面插入的文件的路径!!!!!
         .Quit
     End With
     Set wordApp = Nothing
     Application.StatusBar = False
End Sub

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值