Solved

Writing a Word Macro that will open a document and make it an active document on which I can run some other Macros

Posted on 2008-06-22
5
202 Views
Last Modified: 2010-04-30
I need help writing a Macro that will open a document such as c:\Important Documents\Target.doc and make Target.doc an active document on which I will then be able to run some other Macros (named such as TypingMacro1 and TypingMacro2). These macros (TypingMacro1 and TypingMacro2) will type some sentences in the Target.doc and then Save and Close  Target.doc.
I am able to write TypingMacro1 and TypingMacro2 myself. I only need code for the Macro that will open
c:\Important Documents\Target.doc for such that I can run my other macros on it before saving and closing it. Thanks for the help.
0
Comment
Question by:FaheemAhmadGul
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21841939
You can do that in one line.

Documents.Open "c:\Important Documents\Target.doc"

But I suggest that you capture the document object, so that you can refer to it alter without having to know which is the active document.
Dim Doc as Document

Set Doc = Documents.Open("c:\Important Documents\Target.doc")

Open in new window

0
 

Author Comment

by:FaheemAhmadGul
ID: 21842043
Perhaps I did not express my problem clearly. Please see my code and the problem I explain after the code ends.
The code I have in the click event of one of the VB Forms associated with my Parent Document  Progress Writer  the document which is the active document when the code starts) is as follows.

1.      Private Sub CommandButton2_Click()
2.      Documents.Open "c:\Important Documents\Target.doc"
3.      Selection.EndKey Unit:=wdStory
4.      Selection.Find.ClearFormatting
5.      Selection.TypeParagraph
6.      Application.Run MacroName:="BoldCallibri12"
7.      Selection.TypeText Text:="John K: "
8.      Selection.TypeText Text:=Chr(11)
9.      Selection.TypeText Text:="Date of Visit: "
10.      Unload Me              `this unloads the form on which CommandButton2 was located       DateOfVisit.Show       `this loads a new form which has buttons which on clicking type       
        11.      Unload Me            `this unload the form named DateOfVisit
        12.      ProgressNotes.Show        `this loads a new form ProgressNotes
End Sub
.  
This code opens the c:\Important Documents\Target.doc" for me and Types text given in line 7. (John K) and in line 9. (Date of Visit:) in the Target.doc but after this (when forms DateofVisit and ProgressNotes) load all the typing occurs in the Parent Document (Progress Writer) with which my original form that is ProgressNotes is associated. I want c:\Important Documents\Target.doc to  remain the active document all the time while different forms in my project load and unload. All the forms basically have lots of command buttons which on being clicked type different things in the active document.
Thanks for your help.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 total points
ID: 21842282
When you are handling several documents, it is best to have a document object for each document.

If you stick to using a range object, you don't have to worry about which one is active.
Private Sub CommandButton2_Click()

    Dim Doc1 As Document

    Dim rng As Range

    

    Set Doc1 = Documents.Open("c:\Important Documents\Target.doc")

    Set rng = Doc1.Bookmarks("\EndOfDoc").Range

    rng.InsertParagraphAfter

    Application.Run MacroName:="BoldCallibri12"

    rng.InsertAfter "John K: " & Chr(11) & "Date of Visit: "

    Unload Me

End Sub

Open in new window

0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 31469538
This is perfect. Many thanks. Once again I am grateful. Best Wishes - Faheem
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21849379
Thanks. It is always a pleasure to help a quick learner.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now