2017-08-04 82 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

其定义为变体 –