Convert Macro to run in 2007

A

Anthony B

I've recently been given the task of transferring this Macro into Word 2007.
However although the Macro seems to work. It needs to be run twice.

Everything except the "If Len(Line_$) > 85 Then" routine runs first time.
On the 2nd run this routine does work.

Can anyone point me in the right direction.

Thanks

"
Public Sub DoAdmin()
Dim Line_$
Dim LineCount
'
' Format downloaded Admin Files
'
'
' Start at first line of document
'
WordBasic.StartOfDocument
'
' Get the line as a text string
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = 1
'
' While line length not greater than 80 columns
' Should be in first 150 Lines
' (Include CR/LF and more file to read)
'
While WordBasic.LineDown() And Len(Line_$) <= 85 And LineCount <= 150
'
' Get the next line and check the line size
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = LineCount + 1
Wend
'
' Now work on the document
'
WordBasic.EditSelectAll
'
' Set up font size 8 and font to Courier New
'
WordBasic.FormatFont Points:="8", Underline:=-1, Color:=-1,
StrikeThrough:=-1, Superscript:=-1, Subscript:=-1, Hidden:=-1, SmallCaps:=-1,
AllCaps:=-1, Spacing:="", Position:="", Kerning:=-1, KerningMin:="",
Tab:="0", Font:="Courier New", Bold:=0, Italic:=0
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'

If Len(Line_$) > 85 Then
'
' Remove header And footers For Text report
' reduce margin size And set up landscape
'
WordBasic.FilePageSetup Tab:="0", PaperSize:="1", TopMargin:="0.25" +
Chr(34), BottomMargin:="0.26" + Chr(34), LeftMargin:="1" + Chr(34),
RightMargin:="1" + Chr(34), Gutter:="0" + Chr(34), PageWidth:="11.69" +
Chr(34), PageHeight:="8.27" + Chr(34), Orientation:=1, FirstPage:=0,
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=3, FacingPages:=0,
HeaderDistance:="0" + Chr(34), FooterDistance:="0" + Chr(34),
SectionStart:=2, OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0,
LineNum:=0, StartingNum:="", FromText:="", CountBy:="0", NumMode:=-1
End If
'
' Put the user back at the top of the report
'
WordBasic.StartOfDocument
'
' End of Macro
'
End Sub
"
 
S

StevenM

To: Anthony,

I would help if you would tell us what this macro is supposed to accomplish.

I might be mistaken, but my guess is that this macro:

(1) scans the first 150 lines to see if one of the lines is greater than 80
characters long;

(2) it changes the font; and

(3) if one of the lines scaned was greater than 80 characters, then it
changes the page layout, while deleting the header and footer.

Is that it? Or is there more to it?

I've made the assumption that each line of your document ends with a
paragraph mark. If I'm wrong, ignore my message.

If my assumptions have been correct, you might try running this macro.

Public Sub DoAdmin()
Dim oDoc As Document
Dim nMaxPara As Long
Dim nIndex As Long
Dim nLineLen As Long

Set oDoc = ActiveDocument
'
' While line length not greater than 80 characters
' Should be in first 150 Lines
'
nIndex = 1
nMaxPara = oDoc.Paragraphs.Count
nLineLen = Len(oDoc.Paragraphs(1).Range.Text)
If nMaxPara > 150 Then nMaxPara = 150
While (nIndex <= nMaxPara And nLineLen < 80)
nIndex = nIndex + 1
nLineLen = Len(oDoc.Paragraphs(nIndex).Range.Text)
Wend
'
' Now work on the document
' Set up font size 8 and font to Courier New
'
With oDoc.Range.Font
.Name = "Courier New"
.Size = 8
End With
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'
If nLineLen > 85 Then
' Remove header And footers For Text report
oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
' Reduce margin size And set up landscape
With oDoc.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.25)
.BottomMargin = InchesToPoints(0.26)
.RightMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
End With
End If

' Put the user back at the top of the report
oDoc.Range(0, 0).Select

' End of Macro
End Sub

Steven Craig Miller

Anthony B said:
I've recently been given the task of transferring this Macro into Word 2007.
However although the Macro seems to work. It needs to be run twice.

Everything except the "If Len(Line_$) > 85 Then" routine runs first time.
On the 2nd run this routine does work.

Can anyone point me in the right direction.

Thanks

"
Public Sub DoAdmin()
Dim Line_$
Dim LineCount
'
' Format downloaded Admin Files
'
'
' Start at first line of document
'
WordBasic.StartOfDocument
'
' Get the line as a text string
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = 1
'
' While line length not greater than 80 columns
' Should be in first 150 Lines
' (Include CR/LF and more file to read)
'
While WordBasic.LineDown() And Len(Line_$) <= 85 And LineCount <= 150
'
' Get the next line and check the line size
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = LineCount + 1
Wend
'
' Now work on the document
'
WordBasic.EditSelectAll
'
' Set up font size 8 and font to Courier New
'
WordBasic.FormatFont Points:="8", Underline:=-1, Color:=-1,
StrikeThrough:=-1, Superscript:=-1, Subscript:=-1, Hidden:=-1, SmallCaps:=-1,
AllCaps:=-1, Spacing:="", Position:="", Kerning:=-1, KerningMin:="",
Tab:="0", Font:="Courier New", Bold:=0, Italic:=0
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'

If Len(Line_$) > 85 Then
'
' Remove header And footers For Text report
' reduce margin size And set up landscape
'
WordBasic.FilePageSetup Tab:="0", PaperSize:="1", TopMargin:="0.25" +
Chr(34), BottomMargin:="0.26" + Chr(34), LeftMargin:="1" + Chr(34),
RightMargin:="1" + Chr(34), Gutter:="0" + Chr(34), PageWidth:="11.69" +
Chr(34), PageHeight:="8.27" + Chr(34), Orientation:=1, FirstPage:=0,
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=3, FacingPages:=0,
HeaderDistance:="0" + Chr(34), FooterDistance:="0" + Chr(34),
SectionStart:=2, OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0,
LineNum:=0, StartingNum:="", FromText:="", CountBy:="0", NumMode:=-1
End If
'
' Put the user back at the top of the report
'
WordBasic.StartOfDocument
'
' End of Macro
'
End Sub
"
 
S

StevenM

<< I would help if you would tell us what this macro is supposed to
accomplish. >>

Should read: "It would help ... etc."

StevenM said:
To: Anthony,

I would help if you would tell us what this macro is supposed to accomplish.

I might be mistaken, but my guess is that this macro:

(1) scans the first 150 lines to see if one of the lines is greater than 80
characters long;

(2) it changes the font; and

(3) if one of the lines scaned was greater than 80 characters, then it
changes the page layout, while deleting the header and footer.

Is that it? Or is there more to it?

I've made the assumption that each line of your document ends with a
paragraph mark. If I'm wrong, ignore my message.

If my assumptions have been correct, you might try running this macro.

Public Sub DoAdmin()
Dim oDoc As Document
Dim nMaxPara As Long
Dim nIndex As Long
Dim nLineLen As Long

Set oDoc = ActiveDocument
'
' While line length not greater than 80 characters
' Should be in first 150 Lines
'
nIndex = 1
nMaxPara = oDoc.Paragraphs.Count
nLineLen = Len(oDoc.Paragraphs(1).Range.Text)
If nMaxPara > 150 Then nMaxPara = 150
While (nIndex <= nMaxPara And nLineLen < 80)
nIndex = nIndex + 1
nLineLen = Len(oDoc.Paragraphs(nIndex).Range.Text)
Wend
'
' Now work on the document
' Set up font size 8 and font to Courier New
'
With oDoc.Range.Font
.Name = "Courier New"
.Size = 8
End With
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'
If nLineLen > 85 Then
' Remove header And footers For Text report
oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
' Reduce margin size And set up landscape
With oDoc.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.25)
.BottomMargin = InchesToPoints(0.26)
.RightMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
End With
End If

' Put the user back at the top of the report
oDoc.Range(0, 0).Select

' End of Macro
End Sub

Steven Craig Miller

Anthony B said:
I've recently been given the task of transferring this Macro into Word 2007.
However although the Macro seems to work. It needs to be run twice.

Everything except the "If Len(Line_$) > 85 Then" routine runs first time.
On the 2nd run this routine does work.

Can anyone point me in the right direction.

Thanks

"
Public Sub DoAdmin()
Dim Line_$
Dim LineCount
'
' Format downloaded Admin Files
'
'
' Start at first line of document
'
WordBasic.StartOfDocument
'
' Get the line as a text string
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = 1
'
' While line length not greater than 80 columns
' Should be in first 150 Lines
' (Include CR/LF and more file to read)
'
While WordBasic.LineDown() And Len(Line_$) <= 85 And LineCount <= 150
'
' Get the next line and check the line size
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = LineCount + 1
Wend
'
' Now work on the document
'
WordBasic.EditSelectAll
'
' Set up font size 8 and font to Courier New
'
WordBasic.FormatFont Points:="8", Underline:=-1, Color:=-1,
StrikeThrough:=-1, Superscript:=-1, Subscript:=-1, Hidden:=-1, SmallCaps:=-1,
AllCaps:=-1, Spacing:="", Position:="", Kerning:=-1, KerningMin:="",
Tab:="0", Font:="Courier New", Bold:=0, Italic:=0
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'

If Len(Line_$) > 85 Then
'
' Remove header And footers For Text report
' reduce margin size And set up landscape
'
WordBasic.FilePageSetup Tab:="0", PaperSize:="1", TopMargin:="0.25" +
Chr(34), BottomMargin:="0.26" + Chr(34), LeftMargin:="1" + Chr(34),
RightMargin:="1" + Chr(34), Gutter:="0" + Chr(34), PageWidth:="11.69" +
Chr(34), PageHeight:="8.27" + Chr(34), Orientation:=1, FirstPage:=0,
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=3, FacingPages:=0,
HeaderDistance:="0" + Chr(34), FooterDistance:="0" + Chr(34),
SectionStart:=2, OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0,
LineNum:=0, StartingNum:="", FromText:="", CountBy:="0", NumMode:=-1
End If
'
' Put the user back at the top of the report
'
WordBasic.StartOfDocument
'
' End of Macro
'
End Sub
"
 
A

Anthony B

Your assumptions and macro are both correct.

However the Header remains until you open the header for editing and close it.

Other than that thank you very much.

StevenM said:
To: Anthony,

I would help if you would tell us what this macro is supposed to accomplish.

I might be mistaken, but my guess is that this macro:

(1) scans the first 150 lines to see if one of the lines is greater than 80
characters long;

(2) it changes the font; and

(3) if one of the lines scaned was greater than 80 characters, then it
changes the page layout, while deleting the header and footer.

Is that it? Or is there more to it?

I've made the assumption that each line of your document ends with a
paragraph mark. If I'm wrong, ignore my message.

If my assumptions have been correct, you might try running this macro.

Public Sub DoAdmin()
Dim oDoc As Document
Dim nMaxPara As Long
Dim nIndex As Long
Dim nLineLen As Long

Set oDoc = ActiveDocument
'
' While line length not greater than 80 characters
' Should be in first 150 Lines
'
nIndex = 1
nMaxPara = oDoc.Paragraphs.Count
nLineLen = Len(oDoc.Paragraphs(1).Range.Text)
If nMaxPara > 150 Then nMaxPara = 150
While (nIndex <= nMaxPara And nLineLen < 80)
nIndex = nIndex + 1
nLineLen = Len(oDoc.Paragraphs(nIndex).Range.Text)
Wend
'
' Now work on the document
' Set up font size 8 and font to Courier New
'
With oDoc.Range.Font
.Name = "Courier New"
.Size = 8
End With
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'
If nLineLen > 85 Then
' Remove header And footers For Text report
oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
' Reduce margin size And set up landscape
With oDoc.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.25)
.BottomMargin = InchesToPoints(0.26)
.RightMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
End With
End If

' Put the user back at the top of the report
oDoc.Range(0, 0).Select

' End of Macro
End Sub

Steven Craig Miller

Anthony B said:
I've recently been given the task of transferring this Macro into Word 2007.
However although the Macro seems to work. It needs to be run twice.

Everything except the "If Len(Line_$) > 85 Then" routine runs first time.
On the 2nd run this routine does work.

Can anyone point me in the right direction.

Thanks

"
Public Sub DoAdmin()
Dim Line_$
Dim LineCount
'
' Format downloaded Admin Files
'
'
' Start at first line of document
'
WordBasic.StartOfDocument
'
' Get the line as a text string
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = 1
'
' While line length not greater than 80 columns
' Should be in first 150 Lines
' (Include CR/LF and more file to read)
'
While WordBasic.LineDown() And Len(Line_$) <= 85 And LineCount <= 150
'
' Get the next line and check the line size
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = LineCount + 1
Wend
'
' Now work on the document
'
WordBasic.EditSelectAll
'
' Set up font size 8 and font to Courier New
'
WordBasic.FormatFont Points:="8", Underline:=-1, Color:=-1,
StrikeThrough:=-1, Superscript:=-1, Subscript:=-1, Hidden:=-1, SmallCaps:=-1,
AllCaps:=-1, Spacing:="", Position:="", Kerning:=-1, KerningMin:="",
Tab:="0", Font:="Courier New", Bold:=0, Italic:=0
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'

If Len(Line_$) > 85 Then
'
' Remove header And footers For Text report
' reduce margin size And set up landscape
'
WordBasic.FilePageSetup Tab:="0", PaperSize:="1", TopMargin:="0.25" +
Chr(34), BottomMargin:="0.26" + Chr(34), LeftMargin:="1" + Chr(34),
RightMargin:="1" + Chr(34), Gutter:="0" + Chr(34), PageWidth:="11.69" +
Chr(34), PageHeight:="8.27" + Chr(34), Orientation:=1, FirstPage:=0,
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=3, FacingPages:=0,
HeaderDistance:="0" + Chr(34), FooterDistance:="0" + Chr(34),
SectionStart:=2, OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0,
LineNum:=0, StartingNum:="", FromText:="", CountBy:="0", NumMode:=-1
End If
'
' Put the user back at the top of the report
'
WordBasic.StartOfDocument
'
' End of Macro
'
End Sub
"
 
S

StevenM

To: Anthony,

<< However the Header remains until you open the header for editing and
close it. >>

I have no idea why that would happen, nor how to fix it. It didn't happen
when I ran the macro on my computer.

The only thing I can suggest is to play around with it and see if you can
find something which will work on your machine.

For example, instead of:

oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete

Replace it with:

Call DeleteAllHeadersFooters

While adding the following to your module.
'
' Delete All Headers and Footers
' Code posted by "avkokin"
'
Sub DeleteAllHeadersFooters()
Dim oSection As Section
Dim oHeaderFooter As HeaderFooter

For Each oSection In ActiveDocument.Sections
For Each oHeaderFooter In oSection.Headers
oHeaderFooter.Range.Delete
Next oHeaderFooter
For Each oHeaderFooter In oSection.Footers
oHeaderFooter.Range.Delete
Next oHeaderFooter
Next oSection
End Sub

Another option would be to post my code in a new message with a description
of the problem and see if someone more knowledgeable than I can find you a
solution.

Steven Craig Miller

Anthony B said:
Your assumptions and macro are both correct.

However the Header remains until you open the header for editing and close it.

Other than that thank you very much.

StevenM said:
To: Anthony,

I would help if you would tell us what this macro is supposed to accomplish.

I might be mistaken, but my guess is that this macro:

(1) scans the first 150 lines to see if one of the lines is greater than 80
characters long;

(2) it changes the font; and

(3) if one of the lines scaned was greater than 80 characters, then it
changes the page layout, while deleting the header and footer.

Is that it? Or is there more to it?

I've made the assumption that each line of your document ends with a
paragraph mark. If I'm wrong, ignore my message.

If my assumptions have been correct, you might try running this macro.

Public Sub DoAdmin()
Dim oDoc As Document
Dim nMaxPara As Long
Dim nIndex As Long
Dim nLineLen As Long

Set oDoc = ActiveDocument
'
' While line length not greater than 80 characters
' Should be in first 150 Lines
'
nIndex = 1
nMaxPara = oDoc.Paragraphs.Count
nLineLen = Len(oDoc.Paragraphs(1).Range.Text)
If nMaxPara > 150 Then nMaxPara = 150
While (nIndex <= nMaxPara And nLineLen < 80)
nIndex = nIndex + 1
nLineLen = Len(oDoc.Paragraphs(nIndex).Range.Text)
Wend
'
' Now work on the document
' Set up font size 8 and font to Courier New
'
With oDoc.Range.Font
.Name = "Courier New"
.Size = 8
End With
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'
If nLineLen > 85 Then
' Remove header And footers For Text report
oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
' Reduce margin size And set up landscape
With oDoc.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.25)
.BottomMargin = InchesToPoints(0.26)
.RightMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
End With
End If

' Put the user back at the top of the report
oDoc.Range(0, 0).Select

' End of Macro
End Sub

Steven Craig Miller

Anthony B said:
I've recently been given the task of transferring this Macro into Word 2007.
However although the Macro seems to work. It needs to be run twice.

Everything except the "If Len(Line_$) > 85 Then" routine runs first time.
On the 2nd run this routine does work.

Can anyone point me in the right direction.

Thanks

"
Public Sub DoAdmin()
Dim Line_$
Dim LineCount
'
' Format downloaded Admin Files
'
'
' Start at first line of document
'
WordBasic.StartOfDocument
'
' Get the line as a text string
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = 1
'
' While line length not greater than 80 columns
' Should be in first 150 Lines
' (Include CR/LF and more file to read)
'
While WordBasic.LineDown() And Len(Line_$) <= 85 And LineCount <= 150
'
' Get the next line and check the line size
'
Let Line_$ = WordBasic.[GetBookmark$]("\Line")
Let LineCount = LineCount + 1
Wend
'
' Now work on the document
'
WordBasic.EditSelectAll
'
' Set up font size 8 and font to Courier New
'
WordBasic.FormatFont Points:="8", Underline:=-1, Color:=-1,
StrikeThrough:=-1, Superscript:=-1, Subscript:=-1, Hidden:=-1, SmallCaps:=-1,
AllCaps:=-1, Spacing:="", Position:="", Kerning:=-1, KerningMin:="",
Tab:="0", Font:="Courier New", Bold:=0, Italic:=0
'
' If the report has lines greater than 80 columns with format characters
' then need to print as landscape report
'

If Len(Line_$) > 85 Then
'
' Remove header And footers For Text report
' reduce margin size And set up landscape
'
WordBasic.FilePageSetup Tab:="0", PaperSize:="1", TopMargin:="0.25" +
Chr(34), BottomMargin:="0.26" + Chr(34), LeftMargin:="1" + Chr(34),
RightMargin:="1" + Chr(34), Gutter:="0" + Chr(34), PageWidth:="11.69" +
Chr(34), PageHeight:="8.27" + Chr(34), Orientation:=1, FirstPage:=0,
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=3, FacingPages:=0,
HeaderDistance:="0" + Chr(34), FooterDistance:="0" + Chr(34),
SectionStart:=2, OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0,
LineNum:=0, StartingNum:="", FromText:="", CountBy:="0", NumMode:=-1
End If
'
' Put the user back at the top of the report
'
WordBasic.StartOfDocument
'
' End of Macro
'
End Sub
"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top