在Excel VBA中使某些文本加粗
我正在使用VBA将excel表格导出到word中。word文档有一个书签。代码是这样的,它首先将类型作为标题写入,然后在该类型下写入所有描述。我希望标题是粗体和格式。我有以下代码,但它不工作。如果有人能提出建议的话在Excel VBA中使某些文本加粗,vba,excel,Vba,Excel,我正在使用VBA将excel表格导出到word中。word文档有一个书签。代码是这样的,它首先将类型作为标题写入,然后在该类型下写入所有描述。我希望标题是粗体和格式。我有以下代码,但它不工作。如果有人能提出建议的话 If Dir(strPath & "\" & strFileName) <> "" Then 'Word Document open On Error Resume Next Set objWDApp = GetObject(,
If Dir(strPath & "\" & strFileName) <> "" Then
'Word Document open
On Error Resume Next
Set objWDApp = GetObject(, "Word.Application")
If objWDApp Is Nothing Then Set objWDApp = CreateObject("Word.Application")
With objWDApp
.Visible = True 'Or True, if Word is to be indicated
.Documents.Open (strPath & "\" & strFileName)
Set objRng = objWDApp.ActiveDocument.Bookmarks("Bookmark").Range
.Styles.Add ("Heading")
.Styles.Add ("Text")
With .Styles("Heading").Font
.Name = "Arial"
.Size = 12
.Bold = True
.Underline = True
End With
With .Styles("Text").Font
.Name = "Arial"
.Size = 10
.Bold = False
.Underline = False
End With
End With
On Error GoTo 0
i = Start_Cell
idx(1) = i
n = 2
Do ' Search for first empty cell in the table
i = i + 1
If i > Start_Cell + 1 And Cells(i, QB_Type).Value = Cells(i - 1, QB_Type) Then GoTo Loop1
idx(n) = i
n = n + 1
Public Sub-WriteToWord(对象、文本)
用objRng
.在正文后插入
以
End Sub
从中尝试.Selection.Style.Name=“Heading”
编辑2
以下代码按预期工作。您需要修改它以满足您的需要。我成功地将文本添加到现有word文档中,然后将其加粗
Option Explicit
Public Sub Test()
' Add a reference to Microsoft Word x.0 Object Library for early binding and syntax support
Dim w As Word.Application
If (w Is Nothing) Then Set w = New Word.Application
Dim item As Word.Document, doc As Word.Document
' If the document is already open, just get a reference to it
For Each item In w.Documents
If (item.FullName = "C:\Path\To\Test.docx") Then
Set doc = item
Exit For
End If
Next
' Else, open the document
If (doc Is Nothing) Then Set doc = w.Documents.Open("C:\Path\To\Test.docx")
' Force change Word's default read-only/protected view
doc.ActiveWindow.View = wdNormalView
' Delete the preexisting style to avoid an error of duplicate entry next time this is run
' Could also check if the style exists by iterating through all styles. Whichever method works for you
doc.Styles.item("MyStyle").Delete
doc.Styles.Add "MyStyle"
With doc.Styles("MyStyle").Font
.Name = "Arial"
.Size = 12
.Bold = True
.Underline = wdUnderlineSingle
End With
' Do your logic to put text where you need it
doc.Range.InsertAfter "This is another Heading"
' Now find that same text you just added to the document, and bold it.
With doc.Content.Find
.Text = "This is another Heading"
.Execute
If (.Found) Then .Parent.Bold = True
End With
' Make sure to dispose of the objects. This can cause issues when the macro gets out mid way, causing a file lock on the document
doc.Close
Set doc = Nothing
w.Quit
Set w = Nothing
End Sub
通过向对象库添加引用,可以获得intellisense支持和编译错误。它将帮助您在开发早期确定样式不是Word.Application对象的有效属性 录制一个宏,该宏选择单元格并按所需方式设置字体。停止录制并查看宏,选择您录制的宏,然后单击“编辑”按钮查看代码,它将准确显示您需要执行的操作。我附近没有计算机,因此无法测试,但“标题”周围有括号会导致问题吗?也许把它们取下来试试。VBA对括号和方法/属性调用很挑剔。@TylerStandishMan不,这没有帮助。。。错误是对象不支持此属性…我认为这与paranthesis@KenWhite通过这样做,我选择了一个范围,然后将该范围设置为粗体,这在我的问题中不是这样的。为什么你不能选择一个范围(包括所有标题)并将其同时设置为粗体?不,这也没有帮助…相同的错误“对象不支持此属性”@Tyler StandishManNo…我也试过了,但没有任何帮助…它再次说明对象不支持此属性…我现在无法理解问题可能是什么…这是否可能?当我将标题放入字符串
strTMP
中,然后使用doc.Selection.style(“标题”)
…可能是没有正确选择范围吗…?只是猜测一下,因为您没有将文本写入文档,所以可能选择的内容不是任何内容。此外,文档选择不是有效的属性。doc.Select是,但这似乎不是您需要的。请参阅最新编辑。我已经成功地添加了文本并将其加粗。正如我在回答中所说的,您需要修改它以满足您的需要,但它是一个工作模板。
Option Explicit
Public Sub Test()
' Add a reference to Microsoft Word x.0 Object Library for early binding and syntax support
Dim w As Word.Application
If (w Is Nothing) Then Set w = New Word.Application
Dim item As Word.Document, doc As Word.Document
' If the document is already open, just get a reference to it
For Each item In w.Documents
If (item.FullName = "C:\Path\To\Test.docx") Then
Set doc = item
Exit For
End If
Next
' Else, open the document
If (doc Is Nothing) Then Set doc = w.Documents.Open("C:\Path\To\Test.docx")
' Force change Word's default read-only/protected view
doc.ActiveWindow.View = wdNormalView
' Delete the preexisting style to avoid an error of duplicate entry next time this is run
' Could also check if the style exists by iterating through all styles. Whichever method works for you
doc.Styles.item("MyStyle").Delete
doc.Styles.Add "MyStyle"
With doc.Styles("MyStyle").Font
.Name = "Arial"
.Size = 12
.Bold = True
.Underline = wdUnderlineSingle
End With
' Do your logic to put text where you need it
doc.Range.InsertAfter "This is another Heading"
' Now find that same text you just added to the document, and bold it.
With doc.Content.Find
.Text = "This is another Heading"
.Execute
If (.Found) Then .Parent.Bold = True
End With
' Make sure to dispose of the objects. This can cause issues when the macro gets out mid way, causing a file lock on the document
doc.Close
Set doc = Nothing
w.Quit
Set w = Nothing
End Sub