How to work with Word from Excel using VBA

MPW

Well-known Member
Joined
Oct 7, 2009
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Hey All,

Using Office 2003 in XP.

I am using Excel to Generate a report in Word.
I am having 2 separate issues.
Problem 1. I want to underline certain words and the spaces after them.
Example: Approved By:
I can turn it bold but the underline option has eluded me.
Here is a chunk of the code:
PHP:
Set r = wrdDoc.Range
With r.Find
   Do While .Execute(Findtext:="Approved By:                ",  _
   Forward:=True) = True
      r.Font.Underline = wdUnderlineSingle
I have tried recording a macro to do it and have rewritten it multiple times but nada. I think that it is because I am doing it from Excel but don't know for sure.

Problem 2. I am also trying to create a footer in the same document that will contain the Name of the Excel Workbook that it came from on the left side of the Footer and the "Page " & # & of Pages " & ## on the right side of the Footer.

I normally work just with Excel. Word is very a different creature. Both of these thing have really stumped me. Any help on either or both of these issues will be great. Thanks

MPW
 
Untested. HTH. Dave
Code:
Dim rfooter As Object
Dim oHF As Object
Set oHF = WordApp.activedocument.Sections(1).Footers(1)
   With oHF
      .LinkToPrevious = False
      Set rfooter = oHF.Range
      With rfooter
         .Text = "Page "
         .collapse Direction:=0
         .Fields.Add Range:=rfooter, Type:=33
      End With
   End With
Set rfooter = Nothing
Set oHF = Nothing

'set print preview for footer view
WordApp.activedocument.ActiveWindow.View.Type = 3
WordApp.activedocument.ActiveWindow.View.Zoom.Percentage = 100
edit: WordApp is the word application object
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Well Glory,

I apparently failed to set the library the 1st time around and most of my problems stemmed from that. I did finally get it set and the world is right again.

I used the last suggestion from theaudioguy and it worked fine.

Dave, I decided to leave well enough alone since the code was working. However, I was wondering what
Code:
Type:=33
does if you care to explain it.

Other than that question I consider this post answered.

Thanks for all the help.
 
Upvote 0
I really hate that early binding thing. If you do the late binding thing.... there's no need for any library reference (please spare me the debate wrath). You can change all of those Word specific things to numerical constants. The type 33 thing some how numbers your pages. Dave
 
Upvote 0
Thanks Dave,

No debate here, I don't know enough about either camp to take a side. I hear a lot about binding but didn't really understand the concept until I found this link: http://word.mvps.org/faqs/interdev/earlyvslatebinding.htm
I don't suppose you are the same Dave that wrote it since he liked the early binding, but it did break it down for me. Apparently I have been doing a mixture of both for some time! Do you think he missed anything?

Anyway, I do like simple, and less reliance on 'libraries' sounds good. If type 33 is for the page number what type is for the total number of pages?

Mark
 
Upvote 0
I agree. I saw somebody else use it, so I decided to use it. Hopefully we won't get anybody telling us we did it wrong.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top