周一至周五 : 08:30 - 17:30 客服专员电话/微信:17301649370 QQ:2902102332
微信咨询

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
关于vba实现生成word文件的问题
显示全部楼层 倒序浏览 发表日期 2018-07-09 08:52:54 4624次阅读 5次回复
qqq.jpg
参照论坛里http://bbs.kuaibiao.cn/forum.php?mod=viewthread&tid=3750说的。代码编译运行的时候提示:3行 Imports“System.Runtime.InteropServices.Automation”中指定的命名空间或类型不包含任何公共成员,或者找不到该命名空间或类型。要确保定义了该命名空间或类型且其中至少包含一个公共成员;还要确保导入的元素名不使用任何别名。
代码如下:

Imports SpreadsheetGear
Imports System.Windows.forms
Imports System.Runtime.InteropServices.Automation
Namespace ExcelViewVBDotnet
    Public Class StandardInterface '自定义名称
        '预留位置1
        '预留位置2
        Dim AW As SpreadsheetGear.IWorkbook,Range As SpreadsheetGear.IRange
        Public Sub Workbook_Open(OldRoot As Object,NewRoot As Object,Excel As SpreadsheetGear.Windows.Forms.WorkbookView)
           Excel.GetLock()
           AW = Excel.ActiveWorkbook
           Range = AW.ActiveWorksheet.Range
           Excel.ReleaseLock()

        End Sub'打开时执行事件

        Public Sub RangeSelection(sender As Object,e As SpreadsheetGear.Windows.Forms.RangeSelectionChangedEventArgs)
            'MessageBox.show("测试")

        End Sub'单元格选择后执行的事件

        Public Sub RangeChanged(sender As Object,e As SpreadsheetGear.Windows.Forms.RangeChangedEventArgs)

        End Sub'单元格编辑完成后执行事件

        Public Sub ButtunClick(sender As Object,e As SpreadsheetGear.Windows.Forms.ShapeActionEventArgs)
                if e.shape.name = "BTEST" then
                          '判断是否OOB模式运行
                          if Application.Current.IsRunningOutOfBrowser = True then
                                 '实例化Word接口
                                 Dim _myWord As object = AutomationFactory.CreateObject("Word.Application")
                                 '添加文档
                                 Dim _myDocument As object = _myWord.Documents.Add()
                                 '激活文档
                 _myDocument.Activate()
                 '在选择位置插入文字
                                 AW.WorkbookSet.GetLock()
                 _myWord.Selection.TypeText(range("F_2753").Text)
                                 AW.WorkbookSet.ReleaseLock()
                                 '显示Word
                             _myWord.Visible = true
                          Else
                             messagebox.show("必须在桌面启动才能使用!")
                          End if
               End if
        End Sub'按钮/标签点击事件

        Public Sub FollowHyperlink(sender As Object)

        End Sub '暂不支持

    End Class
    '注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。

End Namespace


客服能指点一下吗?
  1. Option Strict Off
  2. Imports SpreadsheetGear
  3. Imports KbInterfaces
  4. Imports System.Windows.Resources
  5. Imports System.Windows.Threading
  6. Imports System.Windows
  7. Imports System.Windows.Co**ols
  8. Imports Newtonsoft.Json.Linq
  9. Imports System.Runtime.InteropServices.Automation

  10. Namespace ExcelViewVBDotnet
  11. Public Class StandardInterface '自定义名称
  12. Implements Interfaces.IExcelWorkBook
  13. Public Property ActiveWorkbook() As IWorkbook Implements Interfaces.IExcelWorkBook.Workbook
  14. Dim AW As SpreadsheetGear.IWorkbook, Range As SpreadsheetGear.IRange
  15. Public Sub Workbook_Open(OldRoot As Grid, NewRoot As Grid, Excel As SpreadsheetGear.Windows.Co**ols.WorkbookView) Implements Interfaces.IExcelWorkBook.Workbook_Open
  16. Excel.GetLock()
  17. AW = Excel.ActiveWorkbook
  18. Range = AW.ActiveWorksheet.Range
  19. Excel.ReleaseLock()

  20. End Sub '打开时执行事件

  21. Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Co**ols.RangeSelectionChangedEventArgs) Implements Interfaces.IExcelWorkBook.RangeSelection

  22. End Sub '单元格选择后执行的事件

  23. Public Sub RangeChanged(sender As Object, e As SpreadsheetGear.Windows.Co**ols.RangeChangedEventArgs) Implements Interfaces.IExcelWorkBook.RangeChanged

  24. End Sub '单元格编辑完成后执行事件

  25. Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Co**ols.ShapeActionEventArgs) Implements Interfaces.IExcelWorkBook.ButtunClick
  26. ActiveWorkbook.WorkbookSet.GetLock()

  27. If e.Shape.Name = "Button 1" Then
  28. '判断是否OOB模式运行
  29. If Application.Current.IsRunningOutOfBrowser = True Then
  30. '实例化Word接口
  31. Dim _myWord As Object = AutomationFactory.CreateObject("Word.Application")

  32. With _myWord
  33. .Visible = True
  34. .documents.open("D:\word\abc.doc")

  35. 'If _myWord.ActiveDocument.Bookmarks.Exists("Party_Co") = True Then
  36. ' MessageBox.Show(123)
  37. 'End If
  38. AW.WorkbookSet.GetLock()
  39. _myWord.ActiveDocument.bookmarks("Party_Co").range.Text = Range("F_4068").Text
  40. AW.WorkbookSet.ReleaseLock()
  41. _myWord.ActiveDocument.SaveAs("D:\word" & Range("F_4068").Text)
  42. _myWord.ActiveDocument.Close()

  43. _myWord.Quit()
  44. _myWord = Nothing

  45. End With

  46. Else
  47. MessageBox.Show("必须在桌面启动才能使用!")
  48. End If
  49. End If
  50. ActiveWorkbook.WorkbookSet.ReleaseLock()
  51. End Sub '按钮/标签点击事件

  52. Public Sub FollowHyperlink(sender As Object) Implements Interfaces.IExcelWorkBook.FollowHyperlink

  53. End Sub '暂不支持


  54. Public Function ReText(ReTextA As IRange)
  55. ReText = StrReverse(ReTextA.ToString)
  56. Return (ReText)
  57. End Function

  58. Private Function DataTemplate() As String
  59. Throw New NotImplementedException
  60. End Function

  61. Private Function wdGoToBookmark() As Object
  62. Throw New NotImplementedException
  63. End Function

  64. Private Function strBookMark() As Object
  65. Throw New NotImplementedException
  66. End Function

  67. Private Sub SaveAs(p1 As Object)
  68. Throw New NotImplementedException
  69. End Sub

  70. End Class
  71. '注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。

  72. End Namespace
复制代码

电话/微信:18049989370 QQ:857188287
你是,要将快表中的报表输出至word 模板吗?
andy-lan

2018-10-3 09:34:17

是的。已经搞定了。

精彩评论5

客服能指点一下吗?
  1. Option Strict Off
  2. Imports SpreadsheetGear
  3. Imports KbInterfaces
  4. Imports System.Windows.Resources
  5. Imports System.Windows.Threading
  6. Imports System.Windows
  7. Imports System.Windows.Co**ols
  8. Imports Newtonsoft.Json.Linq
  9. Imports System.Runtime.InteropServices.Automation

  10. Namespace ExcelViewVBDotnet
  11. Public Class StandardInterface '自定义名称
  12. Implements Interfaces.IExcelWorkBook
  13. Public Property ActiveWorkbook() As IWorkbook Implements Interfaces.IExcelWorkBook.Workbook
  14. Dim AW As SpreadsheetGear.IWorkbook, Range As SpreadsheetGear.IRange
  15. Public Sub Workbook_Open(OldRoot As Grid, NewRoot As Grid, Excel As SpreadsheetGear.Windows.Co**ols.WorkbookView) Implements Interfaces.IExcelWorkBook.Workbook_Open
  16. Excel.GetLock()
  17. AW = Excel.ActiveWorkbook
  18. Range = AW.ActiveWorksheet.Range
  19. Excel.ReleaseLock()

  20. End Sub '打开时执行事件

  21. Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Co**ols.RangeSelectionChangedEventArgs) Implements Interfaces.IExcelWorkBook.RangeSelection

  22. End Sub '单元格选择后执行的事件

  23. Public Sub RangeChanged(sender As Object, e As SpreadsheetGear.Windows.Co**ols.RangeChangedEventArgs) Implements Interfaces.IExcelWorkBook.RangeChanged

  24. End Sub '单元格编辑完成后执行事件

  25. Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Co**ols.ShapeActionEventArgs) Implements Interfaces.IExcelWorkBook.ButtunClick
  26. ActiveWorkbook.WorkbookSet.GetLock()

  27. If e.Shape.Name = "Button 1" Then
  28. '判断是否OOB模式运行
  29. If Application.Current.IsRunningOutOfBrowser = True Then
  30. '实例化Word接口
  31. Dim _myWord As Object = AutomationFactory.CreateObject("Word.Application")

  32. With _myWord
  33. .Visible = True
  34. .documents.open("D:\word\abc.doc")

  35. 'If _myWord.ActiveDocument.Bookmarks.Exists("Party_Co") = True Then
  36. ' MessageBox.Show(123)
  37. 'End If
  38. AW.WorkbookSet.GetLock()
  39. _myWord.ActiveDocument.bookmarks("Party_Co").range.Text = Range("F_4068").Text
  40. AW.WorkbookSet.ReleaseLock()
  41. _myWord.ActiveDocument.SaveAs("D:\word" & Range("F_4068").Text)
  42. _myWord.ActiveDocument.Close()

  43. _myWord.Quit()
  44. _myWord = Nothing

  45. End With

  46. Else
  47. MessageBox.Show("必须在桌面启动才能使用!")
  48. End If
  49. End If
  50. ActiveWorkbook.WorkbookSet.ReleaseLock()
  51. End Sub '按钮/标签点击事件

  52. Public Sub FollowHyperlink(sender As Object) Implements Interfaces.IExcelWorkBook.FollowHyperlink

  53. End Sub '暂不支持


  54. Public Function ReText(ReTextA As IRange)
  55. ReText = StrReverse(ReTextA.ToString)
  56. Return (ReText)
  57. End Function

  58. Private Function DataTemplate() As String
  59. Throw New NotImplementedException
  60. End Function

  61. Private Function wdGoToBookmark() As Object
  62. Throw New NotImplementedException
  63. End Function

  64. Private Function strBookMark() As Object
  65. Throw New NotImplementedException
  66. End Function

  67. Private Sub SaveAs(p1 As Object)
  68. Throw New NotImplementedException
  69. End Sub

  70. End Class
  71. '注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。

  72. End Namespace
复制代码

电话/微信:18049989370 QQ:857188287
你是,要将快表中的报表输出至word 模板吗?
andy-lan

2018-10-3 09:34:17

是的。已经搞定了。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

快表软件是国内较早研究表格类软件开发平台的团队之一,迄今已有十多年的行业经验.致力于为企事业单位提供实用可靠的数字化平台。
  • 微信公众号

  • 微信小商店

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2022-2024, XiRong Soft. | 快表软件
  • 沪ICP备13033196号 | 营业执照 |上海西戎软件科技有限公司|沪公网安备31011502002146号|沪ICP备13033196号 |