我在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