ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Controlling Word from Excel (https://www.excelbanter.com/excel-programming/287079-controlling-word-excel.html)

tonesmcbutt

Controlling Word from Excel
 
I am trying to create a word document from data in an Excel form.
have managed to set up the references and can get Excel to copy on
piece of data over to Word and format it, but my problem arises whe
trying to copy a second piece of data. When I try copying a secon
piece of data it overwrites the first piece of data. How can I ge
multiple bits of data into Word and format them in different ways, i
different font sizes?

This is the code I have tried to use:-

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim mywdRange As Word.Range

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open("C:\template2.doc")
Set mywdRange = wrdDoc.Words(1)

With mywdRange
.Text = "Test1"
.Font.Size = "10"
.Bold = True
.Text = "Test2"
.Font.Size = "20"
.Bold = False
End With


Any help would be most gratefully received

--
Message posted from http://www.ExcelForum.com


Jon Peltier[_4_]

Controlling Word from Excel
 
Yeah, that Word object model.

Define your range a little differently, so you don't always refer to the
first word in the document.

Dim i As Long
i = wrdDoc.Characters.Count
Set mywdRange = wrdDoc.Range(i - 1, i - 1)

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
http://PeltierTech.com/Excel/Charts/
_______

tonesmcbutt < wrote:

I am trying to create a word document from data in an Excel form. I
have managed to set up the references and can get Excel to copy one
piece of data over to Word and format it, but my problem arises when
trying to copy a second piece of data. When I try copying a second
piece of data it overwrites the first piece of data. How can I get
multiple bits of data into Word and format them in different ways, ie
different font sizes?

This is the code I have tried to use:-

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim mywdRange As Word.Range

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open("C:\template2.doc")
Set mywdRange = wrdDoc.Words(1)

With mywdRange
.Text = "Test1"
.Font.Size = "10"
.Bold = True
.Text = "Test2"
.Font.Size = "20"
.Bold = False
End With


Any help would be most gratefully received.


---
Message posted from http://www.ExcelForum.com/




All times are GMT +1. The time now is 11:14 AM.

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