![]() |
|
#1
|
|||
|
|||
|
I am new to this form, and i have read a large amount of the posts when i was needing help to create a word document with macro functions. I have got these three macros that can do there things for me: auto calculate date, auto generate number and auto save with the name formate that i like, all these are copied or copy and changed a little with my poor coding knowledge. Could you please firstly check if there is any inappropriate in the code. and what i really need is to combine all these three macros into one, so every time i dont have to run all of these three individually, one click will do all the work for me ![]() Codes here: Code:
Option Explicit
Sub AutoNew()
Dim InvoiceFile As String, InvNum As String
'Save ini file in the Word startup folder.
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, by using the following line, the Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
'If there is no InvoiceNumber reference in the ini file
'Create one and set the number to 1, otherwise increment the number
If InvNum = "" Then
InvNum = 1
Else
InvNum = InvNum + 1
End If
System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = InvNum
With ActiveDocument
.CustomDocumentProperties("InvNum") = InvNum
.Fields.Update
End With
End Sub
Code:
Option Explicit
Sub DateAdd()
Dim myDate As Date
Dim myRng As Range
'Set the starting date with the value of a field
Set myRng = ActiveDocument.Fields(3).Result
myDate = myRng
'Tip - Here the field index is one because it is the first field in the document.
'If you don't know the field index number? Just select the field and run the following line of code:
'MsgBox Selection.Fields(1).Index
With ActiveDocument.Variables
.Item("Date1").Value = Format(myDate + 7, "dd MMMM yyyy")
End With
ActiveDocument.Fields.Update
lbl_Exit:
Exit Sub
End Sub
Code:
Sub SaveMe()
Dim StrPath As String, InvNum2 As String
With ActiveDocument
InvNum2 = .CustomDocumentProperties("InvNum")
End With
StrPath = "G:\Sales Invoice\"
ActiveDocument.Saveas FileName:=StrPath & "Invoice #" & InvNum2, Fileformat:=wdFormatPDF
End Sub
Last edited by macropod; 12-08-2012 at 09:16 PM. Reason: Removed personalisation |
|
#2
|
||||
|
||||
|
Whether there is any 'inappropriate' code really depends on whether the document contains the things referred to in, for example ".Fields(3).Result" and ".Variables.Item("Date1").Value". Combined, the code could become:
Code:
Sub AutoNew()
Dim InvoiceFile As String, InvNum As String, myDate As Date, StrPath As String
'Set the file save path.
StrPath = "G:\Sales Invoice\"
'Save ini file in the Word startup folder.
InvoiceFile = Options.DefaultFilePath(wdStartupPath) & "\Invoice.ini"
'or, by using the following line, the Workgroup folder
'InvoiceFile = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\Invoice.ini"
InvNum = System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum")
'If there is no InvoiceNumber reference in the ini file
'Create one and set the number to 1, otherwise increment the number
If InvNum = "" Then
InvNum = 1
Else
InvNum = InvNum + 1
End If
System.PrivateProfileString(InvoiceFile, "InvoiceNumber", "InvNum") = InvNum
With ActiveDocument
'Set the starting date with the value of a field
myDate = .Fields(3).Result
.Variables("Date1").Value = Format(myDate + 7, "dd MMMM yyyy")
.CustomDocumentProperties("InvNum") = InvNum
.Fields.Update
.SaveAs2 FileName:=StrPath & "Invoice #" & InvNum, Fileformat:=wdFormatPDF
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thanks for the help Paul, and yes, you were right, i put the code into word, it worked, the file name came out as i needed, but it seems kinda saved before the actual content got update, so pretty much it is saving the same content into different file names.
I think i better just use those three codes individually :| Thanks for your kindly help Paul. |
|
#4
|
||||
|
||||
|
You could just omit/comment-out the '.SaveAs2 FileName:=StrPath & "Invoice #" & InvNum, Fileformat:=wdFormatPDF' line in the macro I posted, thus combining the first two macros.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to combine many paragraphs in one paragraph? | Jamal NUMAN | Word | 25 | 03-08-2013 04:31 AM |
| How to combine individual tab into one master tab | angie.chang | Excel | 1 | 07-27-2012 10:06 PM |
Combine two forms into one
|
lwisniewski | Word VBA | 3 | 12-24-2010 03:45 PM |
Combine pst files?
|
markg2 | Outlook | 2 | 04-26-2010 03:09 PM |
| How do you combine two contact folders? | waikoloavrm | Outlook | 0 | 04-12-2010 02:31 PM |