View Full Version : Writing to word from excel
grandflavour
11-24-2008, 04:56 PM
Hello everyone,
I've been searching on the internet for a way to insert text into word from excel but have failed to get an answer. Most sites use copy but the dilemma I face is that I don't want cells to be the source.
For example.. I would like to insert a heading into word that is bold and 20 pts. I need to do this from excel. Is there a function I can use?
Any help or links to articles greatly appreciated.
Hello grandflavour,
You mention that you are not using cell values as sources, but didn't mention what the source was going to be. For instance, let's say you want to create some type of report, and you are going to create a header for the report. After this is done, how does the Word doc tie into the Workbook?
Mark
Kenneth Hobs
11-24-2008, 08:08 PM
If you are not going to use any data from Excel, then I am sure why you are using Excel.
The first step in a project like this would be to record your VBA macro in MSWord. Then, if you still want to use Excel's VBA then add your filename and paste your recorded MSWord VBA code to the part where I did TypeText.
Sub AddBookMarksToDoc()
'Add Reference for early binding: Microsoft Word 11.00 Object Libray (MSWord.olb)
Dim cRow As Long, s As String
cRow = ActiveCell.Row
'Early Binding
'Dim wa As Word.Application
'Set wa = New Word.Application
'Late Binding
Dim wa As Object
' Set wa = CreateObject("Word.Application")
'Use an existing instance of MSWord (not recommended)
s = "c:\myfiles\MSWord\Car Information Page.doc"
MsgBox s, , Dir(s)
Set wa = GetObject(s, "Word.Document")
wa.Selection.TypeText "test"
wa.Visible = True
wa.ScreenUpdating = False
With wa.Selection
'.typetext
End With
wa.ScreenUpdating = True
Set wa = Nothing
End Sub
grandflavour
11-25-2008, 12:39 AM
Thanks for the responses guys. Appreciated it.
So basically I just tried the selection.typetext which worked:
wa.Selection.TypeText "test" + vbCrLf
Now the problem comes of formatting it. I tried
with wa.selection
.font.bold = true
end with
and a few variations but nothing that worked. Any ideas?
Hey grandflavour,
This uses early binding, as I wasn't sure if you are using early or late.
Sub BuildHeader_EarlyBound()
Dim _
oWord As Word.Application, _
oDoc As Word.Document
Set oWord = CreateObject(Class:="Word.Application")
'// Creating the object may leave it hidden. //
oWord.Visible = True
Set oDoc = oWord.Documents.Add(NewTemplate:=False, _
DocumentType:=wdNewBlankDocument)
With oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .Font
.Name = "Century Gothic"
.Size = 20
.Bold = True
End With
.Text = "My Header"
End With
Set oWord = Nothing
Set oDoc = Nothing
End Sub
If that doesn't help, how's about including your procedure thus far?
Hope this helps,
Mark
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.