PDA

View Full Version : Default path of word document



espencer
08-11-2005, 07:25 AM
Hello,
I'm trying to write a macro to take the word document I have open and insert other word document to it. These other document are in the same folder as the opened document. How would I code the macro to use the opened document as the default path for the other document to open.

fumei
08-11-2005, 07:34 AM
The path of the current document is:

ActiveDocument.Path

so, if the file is c:\test\thisdoc.doc, the above returns:

c:\test

remember to add the slash. Use Application.PathSeparator.

Therefore ActiveDocument.Path & Application.PathSeparator =
c:\test\

Therefore, ActiveDocument.Path & Application.PathSeparator & _
nextdoc, will give you the next document you want to load.

That being said, I suggest you use the Dir function. That way you can define the folder to work with, and simply process each file in it.

espencer
08-11-2005, 08:07 AM
Listed below is the macro that I'm trying to run after opening note 1 in word. I want to change the InsertFile line to refer to the default path of Note 1. Would appreciate help in coding this small macro as to where to place the ActiveDocument.Path coding.

Thanks for all the help
------------------------------------------------------------------------------

Sub DefaultfootnoteCombo()

' Set up for footnote 2
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.PageSetup.LeftMargin = InchesToPoints(1)
Selection.PageSetup.RightMargin = InchesToPoints(1)
Selection.PageSetup.TopMargin = InchesToPoints(1)
Selection.PageSetup.BottomMargin = InchesToPoints(1)
Selection.InsertFile FileName:="C:\footnote\temp & " " note02.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdSectionBreakNextPage

' Set up for footnote 2
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.PageSetup.LeftMargin = InchesToPoints(.6)
Selection.PageSetup.RightMargin = InchesToPoints(1.4)
Selection.PageSetup.TopMargin = InchesToPoints(.7)
Selection.PageSetup.BottomMargin = InchesToPoints(1.4)
Selection.InsertFile FileName:="C:\footnote\temp & " " note03.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdSectionBreakNextPage

xCav8r
08-11-2005, 08:16 AM
Sub DefaultfootnoteCombo()

' Set up for footnote 2
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.PageSetup.LeftMargin = InchesToPoints(1)
Selection.PageSetup.RightMargin = InchesToPoints(1)
Selection.PageSetup.TopMargin = InchesToPoints(1)
Selection.PageSetup.BottomMargin = InchesToPoints(1)
Selection.InsertFile FileName:=ActiveDocument.Path & Application.PathSeparator & "note02.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdSectionBreakNextPage

' Set up for footnote 2
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.PageSetup.LeftMargin = InchesToPoints(.6)
Selection.PageSetup.RightMargin = InchesToPoints(1.4)
Selection.PageSetup.TopMargin = InchesToPoints(.7)
Selection.PageSetup.BottomMargin = InchesToPoints(1.4)
Selection.InsertFile FileName:= ActiveDocument.Path & Application.PathSeparator & "note03.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdSectionBreakNextPage

espencer
08-11-2005, 08:46 AM
THANKS ALOT. This works but my next question to this would be what do I need to change in the code for the document to insert after note01. The macro places note 01 at the bottom. I need the command to add a blank sheet at the end of note01. Is this possible?

Thank you for all of your help

MOS MASTER
08-12-2005, 02:33 PM
Hi, :yes

I've interperted your question this way:

I think you want a blank page after the existing document body and between each new document you insert? (Let's hope I'm right)

This code does that:
Sub DefaultfootnoteCombo()
Dim sRoot As String

sRoot = ActiveDocument.Path & Application.PathSeparator

With Selection
'insert blank page
.InsertBreak Type:=wdSectionBreakNextPage
.InsertBreak Type:=wdSectionBreakNextPage
' Set up for footnote 2
With .Sections(1).PageSetup
.Orientation = wdOrientPortrait
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
End With
.InsertFile FileName:=sRoot & "note02.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
.InsertBreak Type:=wdSectionBreakNextPage
.InsertBreak Type:=wdSectionBreakNextPage

' Set up for footnote 2
With .Sections(1).PageSetup
.Orientation = wdOrientPortrait
.LeftMargin = InchesToPoints(0.6)
.RightMargin = InchesToPoints(1.4)
.TopMargin = InchesToPoints(0.7)
.BottomMargin = InchesToPoints(1.4)
End With
.InsertFile FileName:=sRoot & "note03.doc", Range:="" _
, ConfirmConversions:=False, Link:=False, Attachment:=False
.InsertBreak Type:=wdSectionBreakNextPage
End With
End Sub


HTH, :whistle:

espencer
08-15-2005, 03:41 AM
Even though this would be the ideal way, the customer wants each nte to be inserted without a blank page. What I have so far is the if I get the cursor to the bottom of note01 the macro works, but when I open note01 the cursor is at the top of the page. My question is How do I get the cursor to appear at the bottom of note01 with a vb command before I insert the rest of the notes with the macro? Thanks for the suggestion.

fumei
08-15-2005, 11:37 AM
I suggest you really start using the macro recorder!

Simply record a macro using the keyboard command for: go to the end of the document. If you do not know that...it is Ctrl-End.

Now look at the macro code. There it is - how to get to the end of the document.

MOS MASTER
08-15-2005, 11:42 AM
I totally misunderstood your question I presume. (And not sure I got it this time)

Use this command to go to the end of the document:
Selection.EndKey Unit:=wdStory


HTH, :whistle:

espencer
08-16-2005, 05:13 AM
That's the command and thanks. This macro works now with all of the helpful change. I would like to know if theirs a way to run this macro without the VB editor being opened. I've try running the macro without editor being opened and nothing happens. Please advise.

fumei
08-16-2005, 07:02 AM
I must repeat....you HAVE to start looking up things yourself in Help. This information is available. It is very very very easy to run a macro without the VB Editor being opened. Did you even try typing in "run macro" in the Word Help box? It is just a few keystrokes to do that, and the answer is right there.

In fact, it is quite important to test this, as things can sometimes run differently if the code is run from the VB Editor, as opposed to running from outside the Editor.


I've try running the macro without editor being opened and nothing happens. Please advise.

I can certainly advise, but can not so, as you do not state what you DID do. You tried running the macro without the editor being opened". OK...how? What DID you do? Because, as I mentioned, it is very easy to run macros outside the editor. In fact, macros should be designed TO run outside of the editor. You only run macros from the editor to test them. No one runs macros from the editor at a real world level. The editor is a DEVELOPMENT environment.

So...what did you do? Perhaps if you stated what you did try, it would make it easier to advise you on what you may have done wrong.

espencer
08-16-2005, 07:14 AM
I'm sorry for the confusion on this. I'm new to this and asking not to write the code but to point me to some topics that might help me. I will do some reading on run macro topic. I was wondering if it was something I've forgot to add to the coding. Thanks for all of your help and I will do more research on macro. I'm trying to learn to learn as much as I can in a short timeframe to create macros that will make processing some things the user do quicker and better. Again I appreciate all the help you and other have done.

MOS MASTER
08-16-2005, 10:08 AM
You're welcome! :yes

For your other questione Gerry helped you on your way.

This will speed you up:
http://office.microsoft.com/en-us/assistance/HP052023061033.aspx

HTH, :whistle:

fumei
08-16-2005, 10:38 AM
I hope I did not sound critical. As a newcomer to coding it is important to get started with basic concepts. You build from there.

Remember this. ALL of us, including the Master Mouse...or Moose...sorry, were beginners once. Those of us who have been at it a while will certainly help you as much as we can. We like to help. However, it makes us feel better, and I guarantee you it will make YOU feel better, if you find things out yourself. Any real problems you have, of course post them here. That is what "here" is for.

Some suggestions.

1. make sure you have Help for VBA installed. It is NOT installed by default. If it has not been installed, install it.

2. Change the VB editor options (Tools > Options) to Require Variable Declarations. By defualt this is OFF - turn it ON. This means you will have to declare varaiables explicitly, or the VBE complains at you. Explicit variables will in the long run (and the short run too!) seriously help you write better code.

3. use the macro recorder a LOT. Record even simple things, just so you get used to the syntax, and way VBA is composed.

4. learn how to make comments, and WRITE THEM. It may seem tedious, and i suppose it is, but if you are learning it can really help to increase your understanding.

5. while there are many ways of indenting code lines so it is easier to read, figure out a format you like, and stick with it. Consistency helps a lot, both for yourself and for others who may see your code.

6. VBA Help has a lot of examples. Copy and paste freely. Test them, see what works, keep notes of what doesn't and why. We here can help you with that. Post things that seem to be messed up.

7. REALLY think things out, and best of all, write them out. Step by step. Again, this may be tedious, but it really helps. For example:

Example A:
1. Copy every second table in a document to a new document
2. save the new document with a user entered filename.

Example B:
1. Open the source document
2. make a table object
3. count how many tables there are in the source document
4. This step is repeated (number of tables / 2) times
4 a) set table object to be the second table in the document
4 b) copy the table
4 c) create a new document
4 d) paste the table object contents into the new document
4 e) insert a space so there will be a space between last inserted table, and the next one
4 f) return to source document
4 g) destroy the table object
4 h) set the table object to be the next table + 1
5. return to new document
6. save document with user enter filename using an inputbox

Example B is the same intent as A - copy every second table to a new document, but it breaks it down into identifable steps that you can work on. This is a simple example, and could be easily made better (so everyone else...yes, yes I know.....)

8. Do the same thing for involved tasks. Learn how to call other procedures. That way you really can make separate procedures, separate chunks that are MUCH easier to debug - figure out what went wrong when it blows up in your face.

9. realize with good humour that things WILL blow up in your face. Things WILL get frustrating and annoying. Shrug. That's programming. Get used to it, learn, keep notes.

10. If possible, start NOW to build a separate code holding file. It is not a good idea to store your code in the default location - normal.dot. If you need some help with how to do that, post here. This topic is not covered well in Help, if at all.

11. Have fun.

Absolutely, learning this stuff will makes things process better, or faster. Plus you will learn more about good document creation, which after all, is a main objective...or should be.

Oh....and....USE Styles!!!!!!!

MOS MASTER
08-16-2005, 10:47 AM
Remember this. ALL of us, including the Master Mouse...or Moose...sorry, were beginners once.

Excellent! :rofl: :rotlaugh:

And for the record....I learn a new trick every day! (You never stop learning) :yes