2016-02-11 62 views
0

我有一個帶有自定義腳本的MS Excel工作表。該腳本的一部分應該編輯MS Word文檔中的信息。在Word表格單元格中設置爲粗體特定字符

需要編輯的東西是存儲在Word文檔的表格單元格中的文本。我設法自己編輯文本,但我需要將部分文本設置爲粗體。

我該怎麼做?

這裏是一個例子。假設我需要在表格單元格(1,1)中輸入「123456789」並將第一個字符「12345」設置爲粗體。像這樣: enter image description here

從Excel中。這裏是我的嘗試:

Dim SavePath as string 

SavePath = "... path ..." 

Set objWord = CreateObject("Word.Application") 
Set objDoc = objWord.Documents.Open(SavePath) 
objWord.Visible = True 

objDoc.Tables(1).Cell(1, 1).Range.Text = "123456789" 

'So far, so good. The next part (how to set part of text to bold) is what I can't figure out. This does not work: 

With objDoc.Tables(1).Cell(1, 1).Range(Start:=0, End:=5) 
    .Content.Font.Bold = True 
End With 

我知道我可以全單細胞設置爲粗體本:

objDoc.Tables(1).Cell(ThisRow, ThisCol).Range.Bold = True 

但我可以在小區內解決特定的字符?

任何人都可以幫助我嗎?

回答

1

試試這個 在Windows 7中試過並測試過。 64,Word 2010 32.

Sub test() 
Set objDoc = ActiveDocument 
objDoc.Tables(1).Cell(1, 1).Range.Text = "123456789" 
Set myrange = objDoc.Tables(1).Cell(1, 1).Range.Paragraphs(1).Range 
MsgBox myrange.Text 
lStartPos = myrange.Characters(1).Start 
lEndPos = myrange.Characters(5).End 
Set myrange = objDoc.Range(lStartPos, lEndPos) 
myrange.Font.Bold = True 
End Sub 

你應該使用這個。

Sub test() 
Dim SavePath As String 

SavePath = "... path ..." 

Set objWord = CreateObject("Word.Application") 
Set objDoc = objWord.Documents.Open(SavePath) 
objWord.Visible = True 
objDoc.Tables(1).Cell(1, 1).Range.Text = "123456789" 
Set myrange = objDoc.Tables(1).Cell(1, 1).Range.Paragraphs(1).Range 
MsgBox myrange.Text 
lStartPos = myrange.Characters(1).Start 
lEndPos = myrange.Characters(5).End 
Set myrange = objDoc.Range(lStartPos, lEndPos) 
myrange.Font.Bold = True 
End Sub 
+0

謝謝!這個想法奏效了。我需要一個循環,所以做了一些改變,但這個想法奏效了! –

相關問題