Writing a Word doc from Excel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In Excel 2000, I can write a Word document without a problem. However, my
code to write the doc is in a separate routine. I need to enter this routine
several times to print any errors found in my editing. The first time in I
can create the Word object, write the headings, write the error msg (even
create a footer). The second time in (all subsequent times), I can bypass
the 'create the word object' and bypass the writing of the header. BUT, the
new error message does not write. Do I need to reestablish the connection to
the word object? Or what else can be wrong?

Thanks
 
Here is the code

Public Sub PrintErrorReportToWord(strErrorMsg As String)

On Error Resume Next

If gblnErrRptStarted = False Then
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
End If

With wdApp
.Visible = True
.Documents.Add DocumentType:=0

.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Size = 16
.Selection.TypeText "Error Report for"
.Selection.typeparagraph
.Selection.Font.Size = 12
.Selection.TypeText gstrConfigFile
.Selection.Font.Bold = False
.Selection.typeparagraph
.Selection.TypeText " "
.Selection.typeparagraph
.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
.Selection.Font.Bold = False
.Selection.Font.Size = 8
.Selection.TypeText " " & Now()
.Selection.typeparagraph
.Selection.TypeText " "
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.Font.Size = 12
.Selection.typeparagraph
.Selection.TypeText " "
.Selection.typeparagraph

End With

gblnErrRptStarted = True
End If

With wdApp

.Selection.TypeText " "
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.Font.Size = 10
.Selection.typeparagraph
.Selection.TypeText strErrorMsg
.Selection.typeparagraph

End With

AddFooter wdApp

Set wdApp = Nothing

End Sub
 
Get rid of the following line of code:
Set wdApp = Nothing

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top