PDA

View Full Version : Solved: Manipulating Word from Excel



jazzyt2u
08-27-2008, 05:48 PM
I have a few things I need assistance on.

1. I need to be able to copy a word from excel and then find a word in MS Word and replace that word with data from Excel.

It finds the word but doesn't replace it. Also the replacement word is a variable. So I don't know what to use for that in place of an actual word.


Sub ReplacingWordsInWord()
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("H:\Test2\WordTestMacros.doc")
oWord.Visible = True
oWord.Selection.Find.ClearFormatting
oWord.Selection.Find.Replacement.ClearFormatting
With oWord.Selection.Find
.Text = "What"
.Replacement.Text = "Hearts"
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oWord.Selection.Find.Execute Replace:=wdReplaceAll


2. I can't get into the header the following coding isn't working:

oWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader


3. Once I replace a word in MSWord how can I apply the header style. I'm having problems with the following code. It's generating a run-time error code 424 Object required. I defined the object with the coding above.



oWord.Selection.Style = ActiveDocument.Styles("Heading 1")
oWord.Selection.Font.Color = wdColorWhite
oWord.Selection.Font.Size = 8


Please help: pray2:

macropod
08-27-2008, 09:59 PM
Hi Jazzy,

For your first macro, try:
Sub ReplacingWordsInWord()
Dim strOldTxt As String
Dim strNewTxt As String
Dim oWord As Object
Dim oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("H:\Test2\WordTestMacros.doc")
oWord.Visible = True
strOldTxt = ActiveSheet.Range("A1").Value
strNewTxt = ActiveSheet.Range("B1").Value
With oDoc.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strOldTxt
.Replacement.Text = strNewTxt
.Forward = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2
End With
End SubNote that I've deleted the .Wrap line (its unnecessary in this context) and I've changed 'wdReplaceAll' to '2'. Since you're using late binding, you need to use the 'Replace' constant value rather than its name. I believe you'll find the same applies to 'wdSeekCurrentPageHeader' and 'wdColorWhite'.

I've also shown how to make the find & replace text variable - in this case by referencing particular cells in the active worksheet.

jazzyt2u
08-29-2008, 11:37 AM
Thank you so much it worked like a charm :bow:


How do I find out what the constant value is for wdSeekCurrentPageHeader and wdColorWhite?

And how might I phrase it?


oWord.ActiveWindow.ActivePane.View.SeekView = (constant value instead of wdSeekCurrentPageHeader)

macropod
08-30-2008, 10:00 PM
Hi Jazzy,

Simple: Insert the constants into Word's object browser (FWIW wdSeekCurrentPageHeader returns 9 and wdColorWhite returns &HFFFFFF).

jazzyt2u
09-02-2008, 11:18 AM
Thank you very much macropod!!!!!!!!!! :bow: