Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用大文本填充Word文档_Excel_Vba_Ms Word - Fatal编程技术网

Excel 用大文本填充Word文档

Excel 用大文本填充Word文档,excel,vba,ms-word,Excel,Vba,Ms Word,我试图用替换文本填充Word文档,替换文本太大,无法放入。有人能帮我吗?我尝试了几个小时,却找不到工作方法 我的代码: Dim lastRow As Integer Dim lastCol As Integer lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row lastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column Dim wApp As

我试图用替换文本填充Word文档,替换文本太大,无法放入。有人能帮我吗?我尝试了几个小时,却找不到工作方法

我的代码:

Dim lastRow As Integer
Dim lastCol As Integer

lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
lastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column

Dim wApp As Object
Dim wDoc As Word.Document

Set wApp = CreateObject("Word.Application")

wApp.Visible = True

For srow = 2 To lastRow
    Set wDoc = wApp.Documents.Open("C:\Temp\Template.docx")
    For scol = 1 To lastCol
        With wDoc.Content.Find
            .Text = "$" & ActiveSheet.Cells(1, scol) & "$"
            ***.Replacement.Text = ActiveSheet.Cells(srow, scol)*** 'This is where the code stops. Thats too big.
            .Execute Replace:=wdReplaceAll
        End With
    Next
    wApp.ActiveDocument.SaveAs "C:\Temp\1.class\" & ActiveSheet.Cells(srow, 1) & ".docx"
Next

单词查找和替换表达式的长度不能超过255个字符。尝试以下几点:

Sub Demo()
Dim lastRow As Long, lastCol As Long, sRow As Long, sCol As Long
Dim xlSht As Worksheet, wdApp As Object, wdDoc As Object
Dim StrFnd As String, StrRep As String

lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
lastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
Set xlSht = ActiveSheet
Set wdApp = CreateObject("Word.Application")

With wdApp
  .Visible = True
  Set wdDoc = .Documents.Open("C:\Temp\Template.docx", , False, False, , , , , , , , False)
  With wdDoc
    For sRow = 2 To lastRow
      For sCol = 1 To lastCol
        StrFnd = "$" & xlSht.Cells(1, sCol).Text & "$"
        StrRep = xlSht.Cells(sRow, sCol).Text
        If Len(StrRep) < 256 Then
          .Range.Find.Execute StrFnd, StrRep, , , , , , 1, , , 2
        Else
          With .Range
            With .Find
              .Text = StrFnd
              .Replacement.Text = StrRep
              .Wrap = 0
              .Execute
            End With
            Do While .Find.Found = True
              .Text = StrFnd
              .Collapse 0
              .Find.Execute
            Loop
          End With
        End If
      Next
      .SaveAs "C:\Temp\1.class\" & ActiveSheet.Cells(sRow, 1).Text & ".docx"
    Next
  End With
  .Quit
End With
Set wdDoc = Nothing: Set wdApp = Nothing: Set xlSht = Nothing
End Sub
子演示()
调暗lastRow为长,lastCol为长,sRow为长,sCol为长
尺寸xlSht作为工作表,wdApp作为对象,wdDoc作为对象
Dim StrFnd作为字符串,StrRep作为字符串
lastRow=ActiveSheet.Range(“B”和Rows.Count).End(xlUp).Row
lastCol=ActiveSheet.Cells(2,Columns.Count).End(xlToLeft).Column
设置xlSht=ActiveSheet
Set wdApp=CreateObject(“Word.Application”)
使用wdApp
.Visible=True
设置wdDoc=.Documents.Open(“C:\Temp\Template.docx”,False,False,,,,False)
使用wdDoc
对于sRow=2到最后一行
对于sCol=1到lastCol
StrFnd=“$”和xlSht.Cells(1,sCol.Text&“$”
StrRep=xlSht.Cells(sRow,sCol).Text
如果Len(StrRep)<256,则
.Range.Find.Execute StrFnd、StrRep、、1、、2
其他的
用。范围
和…一起找
.Text=StrFnd
.Replacement.Text=StrRep
.Wrap=0
.执行
以
Do While.Find.Find=True
.Text=StrFnd
.崩溃0
.Find.Execute
环
以
如果结束
下一个
.SaveAs“C:\Temp\1.class\”和ActiveSheet.Cells(sRow,1.Text和“.docx”
下一个
以
退出
以
Set wdDoc=Nothing:Set wdApp=Nothing:Set xlSht=Nothing
端接头

文本有多大?如果它太大了,那么另一种方法是
查找
文本,选择它,然后使用
wApp.Selection.Range.text=ActiveSheet.Cells(srow,scol).Value
:)不确定为什么在这里进行结束投票?在标记行中得到的错误消息的确切措辞是什么?我得到运行时错误“5”:无效的过程调用或参数。Range.Find.Execute StrFnd,strep。。。。。我不完全理解某些行的功能:(代码已更正。要理解给定行的功能,请在VBA帮助文件中检查有关方法的Excel或Word条目(视情况而定)。