Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Business Applications
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
MACRO help please

Reply  
Thread Tools
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
24-May-2006, 06:34 PM #1
MACRO help please
hi there,

Can anyone please give me a hand with this macro? (BTW I have absolutly no idea about macros, or VB scripting, I found this on the web and tried to modify it for my use).

At the moment I have got the macro working to automatically assign a running number to the file name of each save document e.g. 0001.doc, 0002.doc. But the problem I am having is that all of the saved files are ending up in my documents folder?!! Is there anyway I can define what location/folder/drive? the new files save to?

Sub OnFileNew()
' Automatically creates a new document in a sequentially-numbered order
' based upon the stored sequence number in MySeq text file

' Dimension the variables
Dim strMyFileName As String

' Open MySeq file and load the sequence number
MySeq = System.PrivateProfileString("C:\Documents and Settings\Luqman & Inesa\Application Data\Microsoft\Word\MySeq.txt", _
"", "MySeq")

If MySeq = "" Then
MySeq = 1
Else
MySeq = MySeq + 1
End If

' Update the sequence number in MySeq
System.PrivateProfileString("C:\Documents and Settings\Luqman & Inesa\Application Data\Microsoft\Word\MySeq.txt", "", _
"MySeq") = MySeq

' Load the filename string with leading zeroes
strMyFileName$ = Format(MySeq, "000#")



' Set the filename to the new sequence number
ActiveDocument.SaveAs FileName:=strMyFileName

End Sub

------------------

Also is it possible to setup a macro that with automatically save the file name of a mailmerge (word) document to the first line that is on the document.

e.g. for a document with this info

-----------
David Smith 24-05-06

42 Clarkson avenue
London
N14 8BP
-----------

the file name of the mailmerge document will save as David Smith 24-05-06.doc (with a single click) to a specified folder (if possible)


Thanks in advanced.
__________________
My main system:
Pentium D 945 3.4GHZ 2x2mb
MSI P965 Neo
4GB 800mhz DDR2 RAM
Samsung SpinPoint F1 HD103UJ 1TB Hard Drive SATAII *32MB Cache*
ATI X1300 256MB PCI-E
LG CD/DVD/RW
LG DVD ROM
DAX SN450 PSU
XP PRO SP2

Secondary system:
Pentium D 805 2.66 GHz 2MB (1 Mb per Core)
ASRock 775DUAL-VSTA SKT 775 VIA PT880Pro/ultra
512MB DDR RAM
80GB Seagate barracuda 7200 IDE HD
NVIDIA FX5200 128MB AGP
LG CD/DVD/RW
Casecom Silver Mid-Tower
XP PRO SP1

Last edited by dvk01; 23-Jul-2006 at 05:27 PM..
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
25-May-2006, 07:19 PM #2
bump
bomb #21's Avatar
Distinguished Member with 8,082 posts.
 
Join Date: Jul 2005
Location: The void AKA edge of the Fens
Experience: I bent my wookie :(
25-May-2006, 07:52 PM #3
I guess you got it here:

http://pubs.logicalexpressions.com/p...icle.asp?ID=32

The one here:

http://word.mvps.org/FAQs/MacrosVBA/...edFilename.htm

seems much more straightforward (note the PathAndFileName = "C:\Data\temp" line)
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
26-May-2006, 05:36 AM #4
ah thanks.

I dont suppose you could tell me how I could get the macro to also paste the strMyFileName onto the document before it savess it?
bomb #21's Avatar
Distinguished Member with 8,082 posts.
 
Join Date: Jul 2005
Location: The void AKA edge of the Fens
Experience: I bent my wookie :(
26-May-2006, 06:41 AM #5
Advising on tweaking Word macros is so not my area -- cristobal or Tony J could probably help you. Nevertheless ...

... just by tinkering I found that:

Selection.HomeKey Unit:=wdStory

takes you to the top of the doc (Word 2K), and:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True


inserts a filename field at the insertion point. So if the macro is "global" that might work after the save (or even before, if the filename field is smart enough to auto-update as & when).

HTH,
bomb
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
26-May-2006, 08:45 AM #6
bomb #21 - thanks for your help! I really appreciate it.

I wasnt able to use the macro from the mvps site, as it kept coming up with a syntax error for some reason. But your suggestion for tweaking the macro code really helped with updating the document with the current running number.

The only thing that I'm unable to do so far is set the location for where the new documents should be saved. I've tried using the location code from the other macro but I've so far been unable to adapt it.

Code:
Sub NumberSeq()
' Automatically creates a new document in a sequentially-numbered order
' based upon the stored sequence number in MySeq text file

' Dimension the variables
Dim strMyFileName As String


' Open MySeq file and load the sequence number
MySeq = System.PrivateProfileString("C:\Documents and Settings\Luqman & Inesa\Application Data\Microsoft\Word\MySeq.txt", _
     "", "MySeq")

If MySeq = "" Then
     MySeq = 1
Else
     MySeq = MySeq + 1
End If

' Update the sequence number in MySeq
System.PrivateProfileString("C:\Documents and Settings\Luqman & Inesa\Application Data\Microsoft\Word\MySeq.txt", "", _
     "MySeq") = MySeq

' Load the filename string with leading zeroes
strMyFileName$ = Format(MySeq, "000#")


' Set the filename to the new sequence number
ActiveDocument.SaveAs FileName:=strMyFileName


' This inserts strMyFileName at the top of the document
Selection.HomeKey Unit:=wdStory

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True

' Saves the document
ActiveDocument.Save

End Sub
__________________
My main system:
Pentium D 945 3.4GHZ 2x2mb
MSI P965 Neo
4GB 800mhz DDR2 RAM
Samsung SpinPoint F1 HD103UJ 1TB Hard Drive SATAII *32MB Cache*
ATI X1300 256MB PCI-E
LG CD/DVD/RW
LG DVD ROM
DAX SN450 PSU
XP PRO SP2

Secondary system:
Pentium D 805 2.66 GHz 2MB (1 Mb per Core)
ASRock 775DUAL-VSTA SKT 775 VIA PT880Pro/ultra
512MB DDR RAM
80GB Seagate barracuda 7200 IDE HD
NVIDIA FX5200 128MB AGP
LG CD/DVD/RW
Casecom Silver Mid-Tower
XP PRO SP1
cristobal03's Avatar
Senior Member with 3,019 posts.
 
Join Date: Aug 2005
Experience: Advanced
26-May-2006, 11:41 AM #7
That's the craziest thing I ever heard of. You don't need to store a list of numbers in a separate file just to increment the filenames unless you're saving the files in different locations, or deleting the older ones periodically, or something like that. Is this the case?

Also, if your documents are based on a template you could add that FILENAME field to your template instead of putting it in programmatically. That'd give you a bit more control over its placement (the document's footer is a pretty common place for file information).

chris.
cristobal03's Avatar
Senior Member with 3,019 posts.
 
Join Date: Aug 2005
Experience: Advanced
26-May-2006, 11:45 AM #8
[bump]

Sorry, I misread your question. You're trying to set the destination folder, yes?

chris.
cristobal03's Avatar
Senior Member with 3,019 posts.
 
Join Date: Aug 2005
Experience: Advanced
26-May-2006, 11:52 AM #9
[bump 2]

I'll assume that's the case.

You need to change the line
ActiveDocument.Save
to
ActiveDocument.SaveAs "C:\tmp" & strMyFileName$ & ".doc"
where C:\tmp is the path to the destination directory.

HTH

chris.

[edit]
The SaveAs method has no option for prompting overwrites. Therefore you might want to check to make sure you aren't overwriting any files. This shouldn't be the case, using that PrivateProfileString, but then again the code you're using isn't at all what I would use if I were keeping all the files in a single directory.
[/edit]

[edit 2]
Added the .doc extension, though I don't think it's necessary.
[/edit 2]

Last edited by cristobal03; 26-May-2006 at 12:12 PM..
bomb #21's Avatar
Distinguished Member with 8,082 posts.
 
Join Date: Jul 2005
Location: The void AKA edge of the Fens
Experience: I bent my wookie :(
26-May-2006, 12:00 PM #10
Quote:
Originally Posted by cristobal03
You don't need to store a list of numbers in a separate file just to increment the filenames
Exactly what I thought, & why I suggested the far less complicated one.

All I can say is that:

Sub SaveIncrementedFilename()

Dim PathAndFileName As String, n As Long

PathAndFileName = "C:\Documents and Settings\Andy\My Documents\test"
If Dir(PathAndFileName & ".doc") = "" Then
ActiveDocument.SaveAs (PathAndFileName & ".doc")
Else
n = 1
Do While Dir(PathAndFileName & n & ".doc") <> ""
n = n + 1
Loop
ActiveDocument.SaveAs PathAndFileName & n & ".doc"
End If

Selection.HomeKey Unit:=wdStory

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True

ActiveDocument.Save

End Sub


works fine for me.

You'll figure it out Chris.
cristobal03's Avatar
Senior Member with 3,019 posts.
 
Join Date: Aug 2005
Experience: Advanced
26-May-2006, 12:14 PM #11
Yeah, it really threw me because PrivateProfileString is used to make registry changes or change system settings. It can be used to store things in text files but. Like I said, it would only be necessary if you had to increment files that weren't kept in the same place. Or, say, the previous file had been removed but you still needed to retain the next enumeration.

chris.
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
26-May-2006, 12:47 PM #12
ahhh it works great, thank you

The actual template files will be kept in a seperate folder to that of the location of where I want the increment files to be stored. (well separate mapped network drive to be exact).

The reason for this macro code is that I've got a database program that store patient information. The program can generate order forms, but only as a word mailmerge file.

I thought that if I had a macro code like this, it would make things easier for me to
- keep a copy of the generated ordersheet on my system that I can always refer back to.
- also print out a copy of the order sheet with the increment number to use as an order number to refer back to this exact order. I'm planning on setting the macro to also print a copy of the document to a remote printer (located in my lab).

Anyway everything seems to be working better then I could imagine. So I would like to thank you guys again for your help.

Much appreciated.

Regards

Zeo(UK)

p.s. is there away that I could get the macro to close the document after it saves the copy? I've tried ActiveDocument.Close - but i dont think thats right (it was just a guess)
__________________
My main system:
Pentium D 945 3.4GHZ 2x2mb
MSI P965 Neo
4GB 800mhz DDR2 RAM
Samsung SpinPoint F1 HD103UJ 1TB Hard Drive SATAII *32MB Cache*
ATI X1300 256MB PCI-E
LG CD/DVD/RW
LG DVD ROM
DAX SN450 PSU
XP PRO SP2

Secondary system:
Pentium D 805 2.66 GHz 2MB (1 Mb per Core)
ASRock 775DUAL-VSTA SKT 775 VIA PT880Pro/ultra
512MB DDR RAM
80GB Seagate barracuda 7200 IDE HD
NVIDIA FX5200 128MB AGP
LG CD/DVD/RW
Casecom Silver Mid-Tower
XP PRO SP1
cristobal03's Avatar
Senior Member with 3,019 posts.
 
Join Date: Aug 2005
Experience: Advanced
26-May-2006, 12:51 PM #13
ActiveDocument.Close will close the document not the Application. I believe Application.Quit might be what you mean. Just add those two lines, one after another, at the end of your routine.

chris.
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
31-May-2006, 09:24 AM #14
I've got another problem,

Is there any macro I can use that will automatically run after a document is mailmerged that would enable "Form protect" feature?

The problem that I am having is that I designed a template for an order sheet on MS Word. My datebase program fills in most of the usual information about the client such as Name, address, prescription etc.

For the variable part of the order I inserted some drop down forms in the document that I can select what type of frame, lenses etc that the client wants ordered.

I noticed that the drop down menus dont work (unable to select) unless I enable protection for the forms. The problem that I am having is that when I enabled protection on the main template. I'm unable to mailmerge from my database as it comes up with an error saying:
Code:
-2147216899 The OpenDataSource2000 method or property is not available because the object refers to a protected area of the document. in frmWrite.MergeDocument

So I was wondering if it was possible to insert a macro that would run form protection automatically after the document opened and merged.

Or is there maybe another method I could get around this?
__________________
My main system:
Pentium D 945 3.4GHZ 2x2mb
MSI P965 Neo
4GB 800mhz DDR2 RAM
Samsung SpinPoint F1 HD103UJ 1TB Hard Drive SATAII *32MB Cache*
ATI X1300 256MB PCI-E
LG CD/DVD/RW
LG DVD ROM
DAX SN450 PSU
XP PRO SP2

Secondary system:
Pentium D 805 2.66 GHz 2MB (1 Mb per Core)
ASRock 775DUAL-VSTA SKT 775 VIA PT880Pro/ultra
512MB DDR RAM
80GB Seagate barracuda 7200 IDE HD
NVIDIA FX5200 128MB AGP
LG CD/DVD/RW
Casecom Silver Mid-Tower
XP PRO SP1
zeo's Avatar
zeo zeo is offline
Member with 252 posts.
 
Join Date: Jan 2003
01-Jun-2006, 07:06 PM #15
bump
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:16 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.