在Excel中创buildstringVBA输出到Word将返回“False”,而不是string
我试图在Excel VBA中build立一个string,然后输出到一个Word文件。
我创build的基本string工作正常,但这些复杂的string作为“假”来到Word中,所以我不知道我做错了什么。
我正在使用Excel / Word 2013。
示例excel文件和word模板在这里: https : //dl.dropboxusercontent.com/u/5611192/Sales%20Package%20-%20Test%20for%20Word%20Proposal.xlsm
https://dl.dropboxusercontent.com/u/5611192/Proposal.docx
不工作的string用以下循环构build:
With Sheet1 For RowCnt = Firstrow To Lastrow If .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "" Then strProducts = strProducts & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And Cells(RowCnt, 13).Value = "1" Then strOptions1 = strOptions1 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "2" Then strOptions2 = strOptions2 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "3" Then strOptions3 = strOptions3 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "4" Then strOptions4 = strOptions4 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "5" Then strOptions5 = strOptions5 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "6" Then strOptions6 = strOptions6 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11) End If Next RowCnt End With strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice strOptions1 = strOptions1 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption1Price strOptions2 = strOptions2 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption2Price strOptions3 = strOptions3 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption3Price strOptions4 = strOptions4 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption4Price strOptions5 = strOptions5 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption5Price strOptions6 = strOptions6 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption6Price
解决scheme
以第一个string为例:
strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice
这部分是你的问题:
Range("A47").Font.Bold = True
将其更改为:
Iif(Range("A47").Font.Bold = True,True,False)
对于其他string也是如此。
最终的代码
根据您的意见反馈,您的代码应如下所示:
strProducts = strProducts & Chr(11) & Range("A47").Value & Chr(11) & "Purchase, including installation: " & strPrice Range("A47").Font.Bold = True