Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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 500 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
 
LVL 1

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses

598 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