2017-08-04 88 views
0
For Each cell In rng 
    workSheetName = Format(SaturdayIsComing(), "mm-dd-yyyy") & " " & cell.Value 
    If WorksheetExists(workSheetName) Then 
     Dim localRange, localCell As Range 
     Set localRange = Worksheets(workSheetName).Range("D8:D19") 
     Dim contents As Variant 
     contents = "" 
     Dim firstLine As Boolean 
     firstLine = True 
     For Each localCell In localRange 
      If Len(localCell.Value) > 0 Then 
       If firstLine Then 
        contents = contents & localCell.Value & Chr(11) 
       Else 
        contents = contents & Chr(9) & Chr(9) & Chr(9) & localCell.Value & Chr(11) 
       End If 
      Else 
       contents = fixString(contents) 
      End If 
      If Len(contents) > 0 Then 
       firstLine = False 
      End If 
     Next localCell 

     For Each cc In wDoc.SelectContentControlsByTag(cell.Value & "Notes") 
      If Len(contents) > 0 Then 
       cc.Range.Text = fixString(contents) 
      Else 
       cc.Range.Text = "No Issues Found" 
      End If 
     Next 
    Else 
     errorCodesString = errorCodesString & cell.Value & ":" 
    End If 
Next cell 

輸出到Word值傳遞到Word使用VBA

Forgot to terminate the meeting 
This is a test message\'s 

如果我的單元格中包含'然後我得到一個錯誤說

One of the values passwed to this method or property is incorrect 

我知道' 是在VBA中發表評論。如何在保留某人添加到Excel單元格中的筆記的同時解決此問題?

回答

0

嘗試改變cell.ValueReplace(cell.Value, "'", "")

或者是它的內容已經在它的撇號?有點混亂。

嘗試改變contentsReplace(contents , "'", "")

+0

的內容可能包含「等標點符號。用戶可以在鍵盤上輸入任何東西,如#1西班牙的雨......或我最喜歡的 –

+0

您的解決方案是從字符串中刪除'(撇號)。有沒有辦法保留它? –

0

你需要寫一段代碼來搜索的報價,無論是單引號(')或雙引號(「)不同,將這些前添加一個反斜槓或性格讓翻番''代替'和'「代替」,然後在將內容分配給cc.Range.Text之前對其進行運行。

該例程還可以檢查不正確字符串的其他實例並修復它們。

像這樣的事:

Function fixString(ByVal strIn As Variant) As String 
Dim i As Integer 

Const strIllegals = "\'""" 
For i = 1 To Len(strIllegals) 
strIn = Replace(strIn, Mid$(strIllegals, i, 1), "\" & Mid$(strIllegals, i, 1)) 
Next i 
fixString = strIn 
End Function 

Conversion Picture

+0

我得到一個ByRef參數類型不匹配 –

+0

是將內容定義爲一個字符串? –

+0

其定義爲變體 –