Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modifying and formatting microsoft word document

Status
Not open for further replies.

lpeder6

Programmer
Jan 30, 2019
10
US
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:

Corro_Letter_uftos8.jpg


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:

[pre]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[/pre]
 
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!


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
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?
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
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).

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

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

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

[pre]pos = Selection.HomeKey(Unit:=wdLine, Extend:=wdMove)[/pre]

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?
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top