我在word文檔中設置了某些書籤,我想從txt文件插入文本。以下是我的代碼:word vba在帶有樣式的書籤上插入文本
ActiveDocument.Bookmarks(myTextMark).Range.InsertFile FileName:=locations, ConfirmConversions:=False
我發現插入的文本是我的單詞的默認設置。是否可以使用字體名稱,大小,顏色設置插入的文本,並設置段落縮進?
我在word文檔中設置了某些書籤,我想從txt文件插入文本。以下是我的代碼:word vba在帶有樣式的書籤上插入文本
ActiveDocument.Bookmarks(myTextMark).Range.InsertFile FileName:=locations, ConfirmConversions:=False
我發現插入的文本是我的單詞的默認設置。是否可以使用字體名稱,大小,顏色設置插入的文本,並設置段落縮進?
我不能告訴,因爲你不包括圍繞InsertFile
樣本足夠的代碼,但我猜你的代碼取代文檔中的書籤。這使得很難在插入文本的放置位置處進行標記。這裏的訣竅是弄清楚文檔的哪個部分要更改字體。這可以通過多種方式完成。
我會建議如下,你首先將光標設置爲後的的書籤,然後插入文字。這樣一來,書籤仍然存在,你插入的文本後,您可以使用它與當前位置ADRESS剛剛插入的文本:
Option Explicit
Sub InsertAndUpdateText()
Const myTextMark = 1
Const locations = "C:\test.txt"
'***** Select bookmark
ActiveDocument.Bookmarks(myTextMark).Range.Select
'***** Set the cursor to the end of the bookmark range
Selection.Collapse Direction:=WdCollapseDirection.wdCollapseEnd
'***** Insert text
Selection.InsertFile FileName:=locations, ConfirmConversions:=False
'***** Create new Range object
Dim oRng As Range
'***** Set oRng to text between the end of the bookmark and the start of the current position
Set oRng = ActiveDocument.Range(ActiveDocument.Bookmarks(myTextMark).Range.End, Selection.Range.Start)
'***** Do whatever with the new range
oRng.Style = ActiveDocument.Styles("Normal")
oRng.Font.Name = "Times New Roman"
Set oRng = Nothing
End Sub
BTW,關於你的評論,對書籤的字體可以也可以通過使用用於插入文本的相同範圍對象(即ActiveDocument.Bookmarks(myTextMark).Range.Font = "Times New Roman"
)進行更改,但這隻會更改書籤的字體,而不是新插入的文本。
非常感謝Olle。真的行。 – hadesmajesty
是的。你試過什麼了? –
感謝Olle。我知道 wdApp.Selection.Range.Font.Name =「Times New Roman」 應該與crusor選擇一起使用。但是,我用書籤插入文本。是否可以選取插入的文本作爲選擇或類似 書籤(myTextMark).Font.Name =「Times New Roman」? – hadesmajesty