Getting started to create a document from zero.

A

Alex St-Pierre

Hi,
I would like to create a document from Excel. I did the following code
(works well) and I'm wondering if it's the best way to make a Word Document?
Thanks!
Alex

Sub CreateWordDocument()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim NewStyle As Word.Style
Dim oRange As Word.Range
Dim i As Integer

i = 1
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
Set appWord = CreateObject("Word.Application")
End If
appWord.Visible = True
Set docWord = appWord.Documents.Add
docWord.Styles("Bold Text").Delete
docWord.Styles("Italic Text").Delete
On Error GoTo 0

Set NewStyle = ActiveDocument.Styles.Add("Bold Text")
With NewStyle
.Font.Bold = True
.Font.Italic = False
End With

Set NewStyle = ActiveDocument.Styles.Add("Italic Text")
With NewStyle
.Font.Bold = False
.Font.Italic = True
End With

Set oRange = ActiveDocument.Range(0, 0)
With oRange
.Style = "Bold Text"
.Text = "Bold Text" & Chr(13)
End With

appWord.Selection.EndKey unit:=wdStory
i = i + 1

Set oRange = docWord.Paragraphs(i).Range
With oRange
.Style = "Italic Text"
.Text = "Italic Text" & Chr(13)
End With

'...

Set appWord = Nothing
Set docWord = Nothing
Set NewStyle = Nothing
Set oRange = Nothing
End Sub
 
J

Jezebel

1) Instead of setting up the styles every time, better is to create a
template that already has the styles you want, then create your document
based on that template.

2) Don't use the Selection object. Do everything with Ranges.

3) Don't use the Integer data type. Longs are more efficient.

4) If you're using early binding, don't use late binding to instantiate Word
....

On error resume next
Set WordApp = Word.Application
On error goto 0
If WordApp is nothing then
set WordApp = new Word.Application
end if
 
A

Alex St-Pierre

Thanks for your answer. Since the project will be used by a lot of people, I
have a procedure that open Word references at the opening of the excel file
and close the reference before the close of the excel file. To modify the
reference, it required that the user has clicked the security option:
confident with visual basic project. I don't know if it possible to make a
..inf that modification the registration key of Excel?
1) Instead of setting up the styles every time, better is to create a
template that already has the styles you want, then create your document
based on that template.
My problem is that the program will be used by a lot of people around
different office. It will be hard to put a template on a server with the same
path.
2) Don't use the Selection object. Do everything with Ranges.
How can I go paragraph to paragraph without using selection?
4) If you're using early binding, don't use late binding to instantiate Word
Yes, I'll use early binding and use directly Set WordApp = Word.Application

Thanks!
Alex
 
J

Jezebel

Alex St-Pierre said:
Thanks for your answer. Since the project will be used by a lot of people,
I
have a procedure that open Word references at the opening of the excel
file
and close the reference before the close of the excel file. To modify the
reference, it required that the user has clicked the security option:
confident with visual basic project. I don't know if it possible to make a
.inf that modification the registration key of Excel?

Don't understand the question.

My problem is that the program will be used by a lot of people around
different office. It will be hard to put a template on a server with the
same
path.

Why? Sharing files is usually the reason you have a server in the first
place.

How can I go paragraph to paragraph without using selection?

You don't need to 'go' to paragraphs at all. Just refer to them --

With Doc.Paragraphs(n).Range
...
 
A

Alex St-Pierre

Since the project will be used by a lot of people, I
have a procedure that open Word references at the opening of the excel file
and close the reference before the close of the excel file. To modify the
reference, it required that the user has clicked the security option:
confident with visual basic project. I don't know if it possible to make a
.inf that modification the registration key of Excel?
Sometimes, when you work on different Excel version, if you open the file
into an other version, the word library is missing in the references. This
gives me some errors when I execute a macro(ex: CHR(13) and UCASE function
not found.... Then, if I remove the missing librairies (ex:Word or other),
all works good) So, to avoid that, I unload the word reference inside
Workbook_BeforeClose events. My problem is that I cannot play with librairies
(load in Workbook_Open or unload) if the click Tools/Macro/Security/"Make
confidence with Visual Basic Editor" is not clicked. I'm wondering if there's
a simple method like double-click on a registry icon that could do the same
thing then clicking on this option?
Why? Sharing files is usually the reason you have a server in the first
place.
The problem is that we have about 8 offices and I'm not sure if they are
synchronized and identicals; probably not. Also, if all is inside excel,
someone could make the report without access to the network.
You don't need to 'go' to paragraphs at all. Just refer to them --
With Doc.Paragraphs(n).Range
Thanks, what I understand is to use Chr(13) to change paragraph and
thereafter, I use Set oRange = docWord.Paragraphs(i).Range to get the new
paragraph.

Set oRange = ActiveDocument.Range(0, 0)
With oRange
.Style = "Bold Text"
.Text = "Bold Text" & Chr(13)
End With
i = i + 1
Set oRange = docWord.Paragraphs(i).Range
With oRange
.Style = "Italic Text"
.Text = "Italic Text" & Chr(13)
End With
 

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

Top