PDA

View Full Version : ChangeFileOpenDirectory should point to a folder in the user profile



ACFeddes
01-17-2009, 02:32 PM
Instead of pointing to a fixed folder in for example Program Files, I want to open a file in the My Documents folder from the user that is logged in.

ChangeFileOpenDirectory "C:\Program Files\1-ACF Studytools\Handleiding\"
Documents.Open FileName:="ACF Studies introductie.doc"

I would like the first line ChangeFileOpenDirectory to point to My Documents\1-ACF Studytools\Handleiding\ in the user profile that is logged in. For example if I am logged in, it should point to C:\Users\Alex\Documents\1-ACF Studytools - but when my wife is logge din, it should point to C:\Users\My wife\Documents\1-ACF Studytools zijn.

It has to be a flexible reference to Documents within the documents of the user logged in {userdocs}.

Please advise,
Alex

TonyJollans
01-17-2009, 02:40 PM
Environ("UserName") is the easiest to use. It doesn't necessarily work in all environments but as you mention you and your wife, I presume it's a home PC and it should do fine.

ACFeddes
01-17-2009, 02:50 PM
Environ("UserName") is the easiest to use. It doesn't necessarily work in all environments but as you mention you and your wife, I presume it's a home PC and it should do fine.
Hello Tony,
I tried the following:
ChangeFileOpenDirectory "UserName\1-ACF Studytools\Handleiding\"

But that didn't work.

Could you supply me some sample code of how you would write it in VBA?

Alex

TonyJollans
01-17-2009, 04:14 PM
ChangeFileOpenDirectory "C:\Users\" & Environ("UserName") & "\1-ACF Studytools\Handleiding\"

ACFeddes
01-18-2009, 05:32 AM
Hello Tony,
thanks for the additional explanation - this worked for me.

Questions about your statement:
It doesn't necessarily work in all environments

Can you give some foresight in which environments this will not work?

I want to distribute a template to people, where they can open up certain documents. When I place them in the C:\Program Files\1-ACF Studytools\ everything opens usually in read only and it seems painstaking to remove that read only protection from your Program Files.
Therefore I want to install certain files under Documents\1-ACF Studytools\ for the user logged on, so he can have full access to the document.

Thanks for your good advice so far Tony. I appreciate it.

Another question I'm trying to resolve is this:
I have written certain code to color and add a symbol to selected text, then automatically placing this selected text in a section of the document with a certain category - thereby trying to categorise notes that I draw form text.
I have received some feedback that people already use colorcoding and would like to use their own colors and categories instead of the ones I've defined - is there a way to use a setup document / file with definitions which colors and categories to use

A sample of the code for one categorie is as follows:
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
Selection.Font.ColorIndex = wdAuto
With Selection.Font
.Bold = True
End With

Dim Msg, Style, Title, Response, MyString
Msg = "Wilt u de geselecteerde text markeren EN kopi?ren naar de sectie 'Studie overzicht'? Klik 'No' als u alleen de geselecteerde text wilt markeren."
Style = vbYesNo + vbDefaultButton1
Title = "Kopieer HIGHLIGHT naar Studie overzicht" '> Replace Word

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
Selection.Copy

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Highlight (**)" '> Replace Word
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute

Selection.MoveUp Unit:=wdLine, Count:=1
' Selection.TypeText Text:=Chr(11) & Chr(11)
Selection.TypeParagraph
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1

Selection.PasteAndFormat (wdPasteDefault)


Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1

'> Replace sectie
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=-4030, Unicode _
:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Superscript = True
Selection.Font.Bold = True
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
Selection.Font.ColorIndex = wdAuto

Selection.MoveRight Unit:=wdCharacter, Count:=1
End If


Perhaps I should start a new thread on this.

Please let me know if you understand my question and have some clue how to resolve this

Regards,
Alex

TonyJollans
01-18-2009, 07:06 AM
I have no direct experience of this but I understand that some corporate logons do not cause the Environ variable to populate. It's possibly something to do with domains as opposed to workgroups - but I'm afraid I don't really know much more - perhaps someone with corporate knowledge could help out.

It's not generally done to put your own files into the Program Files directory as it is a machine, rather than a user, structure.


For your other question, a new thread would be better but take a look at the SaveSetting and GetSetting statements - they will allow you to save user-specific settings - you will, of course, have to create a userform or other method of discovering their preferences.

fumei
01-19-2009, 02:01 PM
ACFeddes, please mention when you have cross-posted. This was also posted, and answered (I mentioned using Environs("UserName")), in your same question on Tek-Tips.

Your question regarding color usage is also cross-posted.

lucas
01-19-2009, 02:21 PM
Please read this (http://www.excelguru.ca/node/7) regarding cross-posting and why many people will not help you if they find that you have been cross-posting especially without providing the link......

ACFeddes
01-19-2009, 03:23 PM
Many thanks for the reference to cross posting. I'm new in this and understand the frustration this can produce amongst you people who put in a lot of effort to help newbees in this field.
Apologies therefore.

Tony mentioned that it may not work in all environments, so I though to check out others for a completely safe answer.

But I'll apply the golden rule suggested in the link from excel guru next time.

Thanks for your help so far.

Alex