简体   繁体   English

使用word-VBA将字符串从excel粘贴到Word

[英]Pasting a String from excel to Word using word-VBA

I have my DB in Excel and want to consult it to create reports in Word. 我在Excel中拥有数据库,并希望查阅它以在Word中创建报告。 I have searched and tried different options but none seem to work. 我已经搜索并尝试了其他选项,但似乎都没有用。 Any suggestions would be of great help. 任何建议都会有很大帮助。 I essentially want to paste the content of the second Message box into the word document. 我本质上是想将第二个消息框的内容粘贴到word文档中。

Dim ctl As Control  
Dim some As String
Dim objExcel As Object
Dim objWord As Object
On Error Resume Next

    Set objExcel = GetObject(, "Excel.Application")
    If objExcel Is Nothing Then
        Set objExcel = CreateObject("Excel.Application")
    End If

    Set objWord = GetObject(, "Word.Application")
    If objWord Is Nothing Then
        Set objWord = CreateObject("Word.Application")
    End If

    On Error GoTo 0

    objExcel.Workbooks.Open ("File_Dir")
    objExcel.Visible = False
    objWord.Documents.Open ("File_Dir")
    objWord.Visible = True

    For Each ctl In Me.Controls
         Select Case TypeName(ctl)
             Case "CheckBox"
                 If ctl.Value = True Then
                     MsgBox ctl.Name
                     MsgBox objExcel.Names(ctl.Name).RefersToRange.Value
                     some = objExcel.Names(ctl.Name).RefersToRange.Value

                 End If
         End Select
        Next ctl
    objExcel.Quit
    Set objExcel = Nothing
    MsgBox "complete"

You can read the documentation, are you using interop? 您可以阅读文档,是否正在使用互操作?

    Dim ctl As Control  
    Dim some As String
    Dim objExcel As Object
    Dim objWord As Object
    On Error Resume Next

    Set objExcel = GetObject(, "Excel.Application")
    If objExcel Is Nothing Then
            Set objExcel = CreateObject("Excel.Application")
    End If

    Set objWord = GetObject(, "Word.Application")
    If objWord Is Nothing Then
            Set objWord = CreateObject("Word.Application")
    End If

    On Error GoTo 0

    objExcel.Workbooks.Open ("File_Dir")
    objExcel.Visible = False
    objWord.Documents.Open ("File_Dir")
    objWord.Visible = True

    'give a counter for paragraph
    Dim ctr as Integer = 1

    For Each ctl In Me.Controls
        Select Case TypeName(ctl)
            Case "CheckBox"
                If ctl.Value = True Then
                     MsgBox ctl.Name
                     some = objExcel.Names(ctl.Name).RefersToRange.Value
                     MsgBox some

                     'You can write data to Word document here
                     'You must add paragraph
                     objWord.Documents.addParagraph()
                     objWord.Documents.Paragraph(ctr).Range(ctl.Name)
                     ctr = ctr + 1

                End If
        End Select
    Next ctl
    objExcel.Quit
    Set objExcel = Nothing
    MsgBox "complete"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM