| If you need To allow the user to modify a document you generate, Word can be a little misbehaved. An application that can be automated is supposed to keep track of whether a real person is using it, another application is using it, or both. Word doesn't quite do that right. If you use CreateObject("Word.Basic") and Word isn't running, OLE will start Word For you. If you later use AppActivate or whatever to make Word active and allow the user to edit the document, make sure your app doesn't allow its WordBasic object variable to be destroyed, because when it's destroyed, Word will abruptly shut down, even if the user is in the middle of typing a sentence. Needless to say your users would be less than pleased With this behavior. The solution I've taken is this: 1.Before creating the WordBasic object, look for a running copy of Word. I use the FindWindow API Function To search for a window With the class name "OpusApp" (Word's class name). 2.If a Word window is found, use CreateObject and proceed as normal. 3.If no Word window is found, use Shell or whatever to start Word. 4.Create the WordBasic object with CreateObject. If you visit the MS web site and scrounge around, you should be able To find the Type library For Word. If you add a reference to the Word type library to your app, all of the Word Basic code will be early bound and you'll Get a big performance boost for nothing. Through version 7 (Word '95), Word changed the system default printer if you change the printer for an individual document. This is clearly a programming "no-no", but it is what it is and you have to deal With it. If I'm printing and want to send the job to a specific printer, I save the current default in a variable, change printers, print the document, and Then reset the printer back to the saved default. Finally, Word '97 uses VBA instead of the old WordBasic macro language. The general principles remain the same, but the command syntax may have changed considerably. Unfortunately, I don't have Word '97 (we decided explicitly not to do the upgrade at the office) so I can't tell you off-hand how the syntax of these commands may have changed, but at a minimum I would suspect that the named parameter syntax will be the normal VB syntax. They may have also fixed the behavior if you start Word with CreateObject and then allow the user to work with it. Sub MAIN ChDefaultDir "C:\", 0 FileOpen .Name = "AUTOEXEC.BAT", .ConfirmConversions = 0, .ReadOnly = 0, .AddToMru = 0, .PasswordDoc = "", .PasswordDot = "", .Revert = 0, .WritePasswordDoc = "", .WritePasswordDot = "" End Sub This changes To the C: drive, Then opens autoexec.bat. To run it from a VB app, you first need the reference to the WordBasic object. Dim wb As Object Set wb = CreateObject("Word.Basic") OLE takes care of figuring out how To find Word and, If necessary, start it For you. If Word is already running in one or more windows, you'll Get a reference to one of the already running copies (there's no way to determine which one if more than one Word window is open, so don't make any assumptions there). Anyway, you can now use the WordBasic object to send macro commands to Word. Word uses a slightly different syntax for named parameters, so you have to translate that into VB syntax. change the drive letter (I'm not sure off hand what the 0 is for, but Word Basic help will tell you If you're interested) wb.ChDefaultDir "C:\", 0 ' open the file wb.FileOpen Name:="autoexec.bat" The rest of the parameters In the FileOpen statement can be ignored, they're all defaults and only the file name is of interest here. You may have noticed the syntax translation For the named parameters. Word uses .ParamName = Value While VB uses ParamName:=Value. The dot-paramname syntax will cause a compile time Error in VB, so you need To translate Each command (easily enough done, but a PITA If you have a lot of macro code to translate). Anyway, that's the general idea For managing Word. To produce anything specific, you have a couple of options. What I usually do, is start With a template or partially completed document, open it, and insert the text that's specific to the document I'm creating. There are two methods that are typically used to find a specific location in the document and insert data. 1.Use Word's Bookmarks. Bookmarks work pretty well For most things. You open the original document in Word and setup bookmarks at the locations where you know you'll be inserting some text. Then from your VB code you use the WordBasic EditGoto command to navigate to the bookmark and the Insert command to write the text. 2.Use search and replace. An alternative is To use some Type of tag In the text of the document and Then use Word's search and replace commands to insert your text at the location of the tag. This is more flexible because you can put the same text at several locations With a Single statement (a replace all command), but you need to come up with some type of tag syntax that would never occur in real text. I've done this by using a text entry like this in the document: <%TagName%> The less than and greater than signs In combination With the percent sign is unusual enough that I've never encountered it in a regular document. |
Use Ms Word 97 to print a report from vb5 |
Freelance ASP PHP web development | Web developer India Web development India| Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design