Rabu, 13 Februari 2013

RE: [ExcelVBA] Formatting Word document from Excel VBA

 

Final solution: Notice that the "With" uses the application and not the active document.

With wrdApp

.Selection.InsertAfter ("Font Should Be 18")
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Size = 18
.Selection.Font.Bold = wdToggle
.Selection.Collapse wdCollapseEnd
.Selection.InsertParagraphAfter

.Selection.InsertAfter ("Font Should Be 11")
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Size = 11
.Selection.Font.Bold = wdToggle
.Selection.Collapse wdCollapseEnd
.Selection.InsertParagraphAfter
.ActiveDocument.SaveAs (DirectoryName & "Test.doc")
.ActiveDocument.Close ' close the document
End With

From: ExcelVBA@yahoogroups.com [mailto:ExcelVBA@yahoogroups.com] On Behalf Of Tim Lewis
Sent: Wednesday, February 13, 2013 2:33 PM
To: ExcelVBA@yahoogroups.com
Subject: RE: [ExcelVBA] Formatting Word document from Excel VBA

In researching over and over, maybe I should use a range to mark each section of text, and then change that range to whatever formatting needed.

From: ExcelVBA@yahoogroups.com [mailto:ExcelVBA@yahoogroups.com] On Behalf Of twlewis64
Sent: Wednesday, February 13, 2013 2:09 PM
To: ExcelVBA@yahoogroups.com
Subject: [ExcelVBA] Formatting Word document from Excel VBA

I am creating a document from an Excel spreadsheet. All of that works fine. However, I need to format different lines differently. Whenever I run the code, it changes the format of everything in the document to whatever I set the current font to be. I have pasted a test example below. What am I missing?
Thanks for any help!

Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim DirectoryName As String
DirectoryName = "C:\Temp\"

Set wrdApp = New Word.Application
wrdApp.Visible = True

Set wrdDoc = wrdApp.Documents.Add
With wrdDoc

.Content.InsertAfter ("Font Should Be 18")
.Content.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Content.Font.Size = 18
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAfter

.Content.InsertAfter ("Font Should Be 11")
.Content.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Content.Font.Size = 11
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAfter

.SaveAs (DirectoryName & "Test.doc")
.Close ' close the document
End With

wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

[Non-text portions of this message have been removed]

[Non-text portions of this message have been removed]

__._,_.___
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (3)
Recent Activity:
----------------------------------
Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: http://www.mousetrax.com/techtrax to enter the ezine, then search the ARCHIVES for EXCEL VBA.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA

----------------------------------
More free tutorials and resources available at:
http://www.mousetrax.com

----------------------------------
.

__,_._,___

Tidak ada komentar:

Posting Komentar