2011-11-23 86 views
1

我有fllowing代碼輸入文本從Excel單元格到Word並保留其格式

strMyFormat1 = oWB.Worksheets(strSelectedSheet).Range(strStemLocation).NumberFormat 

WordApp.Selection.TypeText Format(oWB.Worksheets(strSelectedSheet).Range(strStemLocation), strMyFormat1) & vbCr 

但它不會保留Excel單元格的

例如格式如果Excel Cell是大膽的我想,要進入我的Word文檔時

我知道它的工作原理與複製和粘貼,但我不想這樣做,

回答

0

有許多部分單元格的格式,但保留這裏有一些讓你開始:

Dim rng As Excel.Range 
Set rng = oWB.Worksheets(strSelectedSheet).Range(strStemLocation) 
With Selection 
    .Text = rng.Text 
    .Font.Bold = rng.Font.Bold 
    .Font.Color = rng.Font.Color 
End With 

注:使用範圍對象來獲取格式化版本的Text財產;使用Value來獲取未格式化的版本。另請注意,範圍對象(rng)應該是單個單元格。

+0

我不是此刻的代碼,但這看起來真棒 - 然後我會如何寫這個單詞? – Rob

+0

好吧這沒有工作它使整個輸出字粗體 - 我只需要單詞輸入格式的單元格的內容 – Rob

+0

該代碼片段假定您已經有一個引用到一個Word文檔中的「選擇」您想添加單元格文本的位置。看到我的第二個答案,可能是更多你需要的另一個例子。 –

1

此示例程序創建一個新的Word文檔,並將每個單元格的內容在活動的Excel工作表,保持(某些)格式:

Sub ExportToWord_Example2() 
    Dim WordApp As Word.Application 
    Dim doc As Word.Document 
    Dim rng As Range 

    Set WordApp = CreateObject("Word.Application") 
    With WordApp 
     .Visible = True 
     Set doc = .Documents.Add 
    End With 
    For Each rng In ActiveSheet.UsedRange 
     With doc.Paragraphs(doc.Paragraphs.Count).Range 
      .Text = rng.Text 
      .Font.Bold = rng.Font.Bold 
      .Font.Color = rng.Font.Color 
     End With 
     doc.Range.InsertParagraphAfter 
    Next rng 
End Sub 
相關問題