ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Writing a Word doc from Excel (https://www.excelbanter.com/excel-programming/314421-writing-word-doc-excel.html)

Sheldon

Writing a Word doc from Excel
 
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

Chip Pearson

Writing a Word doc from Excel
 
Post the code you are using.


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


"Sheldon" wrote in message
...
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




Sheldon

Writing a Word doc from Excel
 
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

"Chip Pearson" wrote:

Post the code you are using.


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


"Sheldon" wrote in message
...
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





Chip Pearson

Writing a Word doc from Excel
 
Get rid of the following line of code:
Set wdApp = Nothing

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



"Sheldon" wrote in message
...
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

"Chip Pearson" wrote:

Post the code you are using.


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


"Sheldon" wrote in message
...
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








All times are GMT +1. The time now is 05:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com