×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

modifying and formatting microsoft word document

modifying and formatting microsoft word document

modifying and formatting microsoft word document

(OP)
I have a project that was created by another developer and it's my task to fix it as it isn't working properly.

There are users running an application to process medical claims. If there's anything that doesn't seem right, they are to generate a correspondence letter requesting further information. This is done by the application by selecting the state patient/provider are in and the type of correspondence needed. Because there are so many letters, a macro was built to speed up the process.

Since the creation of the macro, it has been determined that the macro still has some issues that were never resolved, and a third party vendor that receives the letters and mails them out stated the date and address of the provider do not correctly fit the window of the envelope. This is where I come in.

Here is what the letter looks like:



The top address block for the insurance company needs to move down just enough for a bar code to be added (not sure how to add a bar code...maybe a font?). The date and address for Jane Doe needs to be moved down also so they can be clearly seen through the clear plastic window of a billing envelope.

The current code copies the template and pastes it into a new Word doc so it can be edited. It does move things, but it also deletes "Plan of Texas" at the top, doesn't create a bar code, and doesn't move the date and other address down enough to where they can be fully viewed. Any advice will be greatly appreciated.

Here's the current code I need to modify:

Option Explicit

Dim objWrd, strPath, objDoc
Dim strFl, State, FirstWord, p, x, chkMsg

strPath = "%MyDoc%\SF Letters\"
strFl = strPath & "%sfid%" & ".docx"

Set objWrd = GetObject(, "Word.Application")
Set objDoc = objWrd.Documents.Add
objWrd.Visible = True

' Pastes copy of original letter template
objWrd.Selection.PasteAndFormat (16)    'wdFormatOriginalFormatting
objWrd.Selection.HomeKey 6, 0

objDoc.Revisions.RejectAll

objWrd.Selection.HomeKey 6, 0
objWrd.Selection.Find.MatchWholeWord = True
objWrd.Selection.Find.Text = "Plan of "
objWrd.Selection.Find.Execute
objWrd.Selection.EndKey , 20
x = objWrd.Selection
If InStr(x, "Plan of ") > 0 Then
   objWrd.Selection.HomeKey 6, 0
   objWrd.Selection.Find.MatchWholeWord = True
   objWrd.Selection.Find.Text = x
   objWrd.Selection.Find.Execute , , , , , , , , , "Plan"
   objWrd.Selection.MoveRight 1, 1
   objWrd.Selection.InsertAfter vbNewLine
   objWrd.Selection.MoveRight 1, 1
End If

For Each p In objDoc.Paragraphs
    If Trim(Replace(p.Range.Text, Chr(13), "")) <> "" Then
        FirstWord = p.Range.Text
        objWrd.Selection.HomeKey 6, 0
        objWrd.Selection.Find.MatchWholeWord = True
        objWrd.Selection.Find.Text = FirstWord
        objWrd.Selection.Find.Execute
        objWrd.Selection.InsertBefore vbNewLine
        objWrd.Selection.InsertBefore vbNewLine
        Exit For
    End If
Next

objWrd.Selection.MoveLeft 1, 1

objWrd.Activate
objWrd.Activate
chkMsg = MsgBox("Please check the letter, if it is correct?" & vbNewLine & vbNewLine & "Press OK to save the letter.", vbOKCancel + vbSystemModal, "Verify Document!!")
If chkMsg = vbOK Then
    objDoc.SaveAs (strFl)
    objWrd.Quit
End If

Set objDoc = Nothing
Set objWrd = Nothing 

RE: modifying and formatting microsoft word document

Hi,

Seems to me that you’re opening an existing document that your posted code is modifying for a particular mailing.

Moving parts of that document around to fit a window in an envelope should be done in Microsoft Word, not in your script. Its a one-time trial-and-error drill with a ream of paper, printer and the envelope they use. You’ve got to analyze the document layout to determine how to move these blocks of data into the proper position on the page. Does the document use Paragraphs and Tabs, Text Boxes (Inline or not), Tables. Ya gotta look at the doc!

https://pe.usps.com/text/qsg300/Q201a.htm

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: modifying and formatting microsoft word document

(OP)
There are many templates they would have to update, so that's why they built the macro. Currently, it's not doing it right, so the users are manually making a copy of the original, editing how the third party requested, then sending it off. They want me to fix the script so that it does the editing the way they do it manually. I'm just curious if there's an easier way to make these changes other than what's already there?

I've used bookmarks in Word docs before to tell a script where to enter text. I've done some reading and I'm wondering if adding bookmarks to the copy to tell the script what blocks need to be moved and then move them accordingly will work?

RE: modifying and formatting microsoft word document

I don’t know what’s already there. Apparently you don’t either. And you won’t until you open that document in Word and observe what Document Objects are there and exactly how they will need to be manipulated in order to design a solution.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: modifying and formatting microsoft word document

(OP)
Sorry, I meant what's already in the script...a better way of scripting the edits that what's already in the script.

For instance, the current script finds the home location (like pressing the Home key on your keyboard).

objWrd.Selection.HomeKey 6, 0
 
When I try looking for information so that I understand the breakdown of what this is doing:

objWrd - The Word object found in which the editing will occur 
Selection - Selects the Word object 
HomeKey 6, 0 - ??? 

All I find is this for VB instead of VBScript and no explanation on how it equals "HomeKey 6, 0":

pos = Selection.HomeKey(Unit:=wdLine, Extend:=wdMove) 

Do you know of any resources on the internet where I can find the answers that I need? Or, do you know of any scripting examples similar to what I'm trying to do that I can play around with and see if I can make them work better than what I'm currently working with?

RE: modifying and formatting microsoft word document

Well you found what I found.

I’ll tell you what I’d do. I’d insert 2 text boxes at the proper locations for the return address and the recipient address directly in the document and save. Or if you want to do that via code on the fly, you’ll need to Add the appropriate texbox shape (not in-line) and size, and the two location coordinates (Top, Left).

Then my script would simply put the address data in the appropriate text box.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close