String too long, copying data from Excel into a Word Document

社会主义新天地 提交于 2020-07-18 08:24:13

问题


I'm trying to replace a placeholder in a text document with a string from an Excel cell.

It works fine for strings less than 255 characters but not when they are greater.

Code:

Sub Sheet003ADes()
    'Sheet 3A- Multi-Family Housing -----------------------------

    With Selection.Find
        .ClearFormatting
        .Text = "[[3A DESCRIPTION]]"
        .Replacement.ClearFormatting
        .Replacement.Text = Worksheets("3A- Multi-Family Housing").Range("A4").Value 'Insert 3A Activity Description
        .Execute Replace:=wdReplaceAll, Forward:=True, _
        Wrap:=wdFindContinue
    End With


End Sub

回答1:


Greg Maxey (Word VBA MVP) has some tips you might find useful.

http://gregmaxey.com/word_tip_pages/find_replace_long_string.html

There is a 255-character limit on the Replacement.Text, so you must be ignoring this with an On Error Resume Next (get rid of this line, and you'll see the error like below).

His suggestion is to leverage the Clipboard, which works fine as long as you're only doing stuff between Word documents or within a Word document (e.g., to .Copy a range or selection), but from other application like Excel, I think you'll probably need to use an MsForms.DataObject as an intermediary to put the text in the clipboard, before you can use the trick he outlined.

Something like:

Const wdReplaceAll As Long = 2
Const wdFindContinue As Long = 1
Dim longString As String
Dim wd As Object, doc As Object, sel As Object
Dim dataObj As New DataObject  '## Requires reference to MSForms
'## Alternatively:
' Dim dataObj as Object
' Set dataObj = CreateObject("MSForms.DataObject")

Set wd = GetObject(, "Word.Application")
Set doc = wd.ActiveDocument

longString = Worksheets("3A- Multi-Family Housing").Range("A4").value

dataObj.SetText longString
dataObj.PutInClipboard

Set sel = doc.Range
sel.Select

With doc.Range.Find
    .ClearFormatting
    .Text = "[[3A Description]]"
    .Replacement.ClearFormatting
    .Replacement.Text = "^c"
    .Execute Replace:=wdReplaceAll, Forward:=True, _
        Wrap:=wdFindContinue
End With



回答2:


Thanks using the Clipboard did the trick.

Here is the current working version.

Sub InputContractData()
'
' You must pick Microsoft Excel Object Library from Tools>References
' in the Visual Basic editor to execute Excel commands.

' InputContractData Macro
'
'

'Define Excel and Workbook Information
Dim objExcelApp As Excel.Application
Dim objCDCDataWorkbook As Workbook
Dim CDCDataFile
Dim CDCDataFilePath
Dim CDCDataFileName

'Define Word and Document Information
Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document

'Open Excel Program
Set objExcelApp = New Excel.Application

Set objWordApp = Word.Application
Set objWordDoc = objWordApp.ActiveDocument
objExcelApp.Visible = True
objWordApp.Visible = True
CDCDataFile = objExcelApp.GetOpenFilename("Excel Files (*.xlsx), *xlsx")
Set objCDCDataWorkbook = objExcelApp.Workbooks.Open(CDCDataFile)
CDCDataFilePath = Left(CDCDataFile, InStrRev(CDCDataFile, "\"))

CDCDataFileName = Dir(CDCDataFile)

Call Sheet001
Call Sheet002
Call Sheet003ADes
Call Sheet003AFunding
Call Sheet003ATasks
Call Sheet003Accomplishments
Call Sheet010
Call Sheet010A
Call Sheet010E
Call Sheet010F
Call Sheet010G
Call Sheet010D
Call Sheet010C
Call SheetLowModCT

'Save Document in same folder as CDC Workbook
   objWordDoc.SaveAs CDCDataFilePath & "\DraftContract.docx"

' Close the new Word document.
    objWordApp.ActiveDocument.Close
' Close the Word application.
    objWordApp.Quit

End Sub

Sub Sheet003ADes()
'Sheet 3A- Multi-Family Housing -----------------------------
' Long String requires copying to clipboard and pasting in text
' Add MS Forms Reference

Dim longString As String
Dim sel As Object
Dim obj3ADes As New DataObject

longString = Worksheets("3A- Multi-Family Housing").Range("A4").Value 'Copy 3A Activity Description

obj3ADes.SetText longString
obj3ADes.PutInClipboard

With Selection.Find
    .ClearFormatting
    .Text = "[[3A DESCRIPTION]]"
    .Replacement.ClearFormatting
    .Replacement.Text = "^c" 'Paste 3A Activity Description
    .Execute Replace:=wdReplaceAll, Forward:=True, _
        Wrap:=wdFindContinue
End With

End Sub


来源:https://stackoverflow.com/questions/43742174/string-too-long-copying-data-from-excel-into-a-word-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!