PDA

View Full Version : Word Fields and VBA



simoncarter
03-07-2007, 07:44 AM
Hello,

I am struggling with some thing,

I have a txt file called sitename.txt with some text in, and I am trying to get the text out of the file and then get the macro I am running to save it as that name.

How do I do this?

Simon

fumei
03-07-2007, 02:10 PM
What have you tried? Have you used the macro recorder?

Try something, and if you have specific problems post back with your code, and what exactly you are having problems with.

You should be able to do most of this with the macro recorder.

fumei
03-07-2007, 02:11 PM
Oh...and what is with the Subject line????

"Word Fields and VBA"

You mention absolutely nothing about fields in your post.

simoncarter
03-08-2007, 03:44 AM
Hi,

The software to get a report puts vairous text files with bits of data in each,

On one button on the software I have asked it to open a word document with various fields in, i.e. the text out of the each file.

I have also asked the macro to automatically save the file with the next available number with auto incriment and also then save the file with what the faulty site is. I have posted the code so you can take a look,

Now remember im not a coder so I may have repeated myself and maybe not know what all of it does, but it works like a treat even though it took a day to sort out :).


Sub AutoNew()
Dim SettingsFile As String
Dim Order As String
Dim sView As String
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s


'Get number of next file in settings.ini
SettingsFile = "I:\ini\Settings.ini"
Order = System.PrivateProfileString(SettingsFile, "", "")

'If there is no Number reference in the ini file
'Create one and set the number to 1, otherwise increment the number
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString(SettingsFile, "", "") = Format(Order, 0#)
sView = ActiveWindow.View 'store the current view
With ActiveWindow
'.View = wdPrintView 'set the document view to print layout
'.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection
'.Font.Name = "Arial" 'Set the font characteristics
'.Font.Bold = True 'of the inserted header
'.Font.Italic = False 'if required
'.Font.Size = "10"
'.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
'.View.SeekView = wdSeekMainDocument 'Close the header view
'.View = sView 'and restore the initial view
End With

Selection.Fields.Update
Selection.Fields.Update
Selection.WholeStory
Selection.Fields.Update





' Gets sitename and stores to s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("C:\Data\Temp\sitename.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine
'MsgBox s
ts.Close
' gets filename array and writes to tmp file
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("C:\Data\Temp\testfile.txt", True)
a.WriteLine (Format(Order, "000# "))
a.Close

'MsgBox (Date$)
Datee = Date$
'MsgBox (Order)
'docname = ("Incident Report " & Format(Order, "000# ") & s & " " & Datee)
Selection.Fields.Update
Selection.Fields.Update
Selection.WholeStory
Selection.Fields.Update
ActiveDocument.SaveAs FileName:="I:\" & "Incident Report " & Format(Order, "000# ") & s & " " & Datee
End Sub

fumei
03-08-2007, 10:07 PM
1. Please use the underscore character when you post code. For example:ActiveDocument.SaveAs FileName:="I:\" & "Incident Report " & _
Format(Order, "000# ") & s & " " & Datee


It makes it easier to read for those who do not have a monster monitor at high resolution.

2. Why are you using AutoNew?

3. When posting, if you have a lot of commented out code, then why post it?

Glad you got something working for you. Just a day???? Hey, you are doing good!