Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-08-2012, 08:54 PM
lbf200n lbf200n is offline Please help me to combine these code together Windows 7 64bit Please help me to combine these code together Office 2007
Novice
Please help me to combine these code together
 
Join Date: Dec 2012
Posts: 3
lbf200n is on a distinguished road
Default Please help me to combine these code together

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
Reply With Quote
  #2  
Old 12-08-2012, 09:21 PM
macropod's Avatar
macropod macropod is offline Please help me to combine these code together Windows 7 64bit Please help me to combine these code together Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
However, I doubt the validity of saving a document immediately it's created (and before you'd actually input any relevant invoice content) as a PDF.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-08-2012, 10:54 PM
lbf200n lbf200n is offline Please help me to combine these code together Windows 7 64bit Please help me to combine these code together Office 2007
Novice
Please help me to combine these code together
 
Join Date: Dec 2012
Posts: 3
lbf200n is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 12-09-2012, 04:22 PM
macropod's Avatar
macropod macropod is offline Please help me to combine these code together Windows 7 64bit Please help me to combine these code together Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Thread Tools
Display Modes


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
Please help me to combine these code together Combine two forms into one lwisniewski Word VBA 3 12-24-2010 03:45 PM
Please help me to combine these code together 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft