2016-03-26 148 views
1

我試圖重寫一個順序文件 具體線路例如,如果文件有:寫雙引號

"1" 
"Kii" 
"Kii" 
"Kii" 
"Kii" 
"2" 
"Troy Martinez" 
"Edoy" 
"Edoy" 
"69" 
"3" 
"Snoop Dogg" 
"Weed" 
"President Troy" 
"420" 

而且我有這樣的代碼覆蓋

Private Sub OverWrite() 
    Dim Count As Integer 

    On Error GoTo ErrSub 

    LineCount = 1 
    Open App.Path & "\Data.txt" For Input As #1 
    Do While Not EOF(1) 
     If LineCount < ((IDCount - 1) * 5) + 1 Or LineCount >= (IDCount * 5) + 1 Then 
      For Count = 0 To 4 
       Input #1, TextTemp 
       FileText = FileText & """ & Text1(Count) & """ & vbCrLf 
       LineCount = LineCount + 1 
      Next Count 
     Else 
      For Count = 0 To 4 
       Input #1, TextTemp 
       FileText = FileText & """ & TextTemp & """ & vbCrLf 
       LineCount = LineCount + 1 
      Next Count 
     End If 
    Loop 
    Close 
    Open App.Path & "\Data.txt" For Output As #1 
    Print #1, FileText 
    Close 
ErrSub: 
    Resume Next 
End Sub 

假設我IDCount是2,文本文件變成

" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & TextTemp & " 
" & TextTemp & " 
" & TextTemp & " 
" & TextTemp & " 
" & TextTemp & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 
" & Text1(Count) & " 

我該如何解決這個問題?由於 我很小白

編輯:我的選擇顯的是

Option Explicit 
    Dim IDCount As Integer 
    Dim LineCount As Integer 
    Dim FileText As String 
    Dim TextTemp 

回答

2

4引號("""")逃單引號(")這樣:

FileText = FileText & """" & Text1(Count) & """" & vbCrLf 

chr$(34)也輸出"

+0

這裏有個小錯誤,雖然這個例子是正確的:兩個引號轉義了一個單引號,而不是四個引號。例如,如果路徑是「c:\」Microsoft SQL Server「\」,那麼'myPath =「c:\」「Microsoft SQL Server」「\」'是正確的。這很容易混淆,因爲四個引號是連接單引號的方式。當然,你知道這一點,但我指出了OP的好處。 – BobRodes