ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Performing ms word operations from ms Excel. (https://www.excelbanter.com/excel-programming/347519-re-performing-ms-word-operations-ms-excel.html)

Dave Peterson

Performing ms word operations from ms Excel.
 
Most/All my MSWord coding comes from MSWord's macro recorder.

I record what I need to do in MSWord, then try to modify it to work via excel.

For instance, this prints a .doc file:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\document1.doc"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

There are a couple of things that can make your coding easier.

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object

Set a reference within the VBE (tools|references) to "microsoft word xx.0 Object
library".

Then you'll get the intellisense and autocomplete for those nicely declared
objects.

After you've tested it, remove that reference and use the generic "as object".
And if you used any of the Word constants, change them to their value.

If you record your macro in word and have trouble incorporating it in excel,
post back with your efforts. Maybe someone will be able to help.

andy wrote:

Hi,

I would like to code a macro in a Excel document that would perform a file
conversion of a word doc to html format. I'm able to activate the word
document using Application.ActivateMicrosoftApp xlMicrosoftWord.

But how to I access to the word macro objects to perform the necessary
automation within word?


--

Dave Peterson


All times are GMT +1. The time now is 11:33 PM.

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