-2
我有一個相當簡單的excel vba腳本來從單個頁面模板生成word文檔。這只是一個榮耀的搜索,並替換爲單元格值替換模板鍵。將Excel CHAR(10)帶入word文檔
當我嘗試和多行單元格複製到字,或使用方法插入VBCR或vbNewLine:
strReplace = TagArray(i, j)
strReplace = Replace(strReplace, Chr(10), vbCr)
strReplace = Replace(strReplace, vbNewLine, vbCr)
這只是表明了字作爲一個ASCII字符框。如果我只讓Chr(10)或Chr(10)Chr(13)通過,字就會丟棄它,並且沒有換行符。
如何保存多行單元?
無法弄明白,所以我借從這裏的技術:
Generate Word Documents (in Excel VBA) from a series of Document Templates
,並擺脫了替換(),並改變了它太:
'Iterate through all of the keys to be replaced
For j = 0 To KeyCount - 1
strReplace = TagArray(i, j)
clipBoard.SetText IIf(strReplace = vbNullString, "", strReplace)
clipBoard.PutInClipboard
For Each storyRange In OutputDoc.StoryRanges
Do
With storyRange.Find
.MatchWildcards = True
.Text = KeyArray(j)
.Replacement.Text = "^c"
'.Replacement.Text = strReplace
.Execute Replace:=2
End With
Set storyRange = storyRange.NextStoryRange
Loop While Not storyRange Is Nothing
Next
Next j
Next i
將'vbCr'更改爲'vbCrLf'。 A *回車* vbCrLf是Chr(13)和Chr(10);一個*換行* vbLf是Chr(10)。這是按Enter和按Shift + Enter在Word中的區別。 – Jeeped
在xlsx中仍然有一個盒子 - 這是在殺我;當我用這個純文本時,vbNewLine工作正常。 – Justin
我最終從這個優秀的帖子複製了一下:http://stackoverflow.com/questions/19962231/from-a-vba-excel-file-open-a-word-template-enter-a-number-and-打印並通過剪貼板傳遞所有內容 – Justin