如何解决vba excel属性是否有可能返回word.range?
我希望我的class属性返回word.range
对象,然后将其插入到Word文档中。我希望它返回word.range
而不是string
的原因是因为我希望文本是预先格式化的(即,文本的某些部分为粗体)。这是我尝试的代码:
Property Get wordRange() As word.Range
Dim result As word.Range
'invalid use of new keyword
Set result = New word.Range
result.text = "the text here is bold"
result.Bold = True
wordRange = result
End Property
是否可以在“真空”中创建word.range
对象并将其返回?如果是这样,它可以同时包含粗体和常规格式吗?
解决方法
首先@Mathieu Guindon是正确的。 我遇到了麻烦,所以我试图找到解决问题的方法。如果您的类创建Word.Application和Word.Document的单独实例,则可以根据需要使用它。 我添加了类的初始化和终止,因此该类(名为clsWrd)为:
Private wApp As Word.Application
Private wDoc As Word.Document
Property Get wordRange() As Word.Range
Dim result As Word.Range
Set result = wDoc.Paragraphs(1).Range
result.Text = "the text here is bold"
result.Bold = True
Set wordRange = result
End Property
Private Sub Class_Initialize()
Set wApp = New Word.Application
'App.Visible = True
Set wDoc = wApp.Documents.Add
End Sub
Private Sub Class_Terminate()
wApp.Quit False
Set wApp = Nothing
End Sub
这部分说明了它的用法
Sub test()
'Create and initialize the class
Dim nk As clsWrd
Set nk = New clsWrd
'Simulate/demostrate the main word application
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = New Word.Application
wApp.Visible = True
Set wDoc = wApp.Documents.Add
Dim wrngTarget As Word.Range
Set wrngTarget = wDoc.Paragraphs(1).Range
'Use the object
Dim wrngSource As Word.Range
Set wrngSource = nk.wordRange
'wrngSource.Copy
'wrngTarget.Paste
wrngTarget.FormattedText = wrngSource.FormattedText
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。