Help writing a macro

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

One of the things I have to do repeatedly is to reformat text in a manner
that is more appropriate to code. I'd like to automate the process in a
macro but I am having a little trouble figuring out how to do it.

Here's the sequence of events I'd like to accomplish:

Search forward for a new line.
Select the new line characters.
Replace the new line with a carriage return.
Assert a shading of light gray.
Assign a font of Courier New, 12 pt to the paragraph.

In Word, this was dead simple and I thought I could just copy the macro over
to FrontPage. However, that didn't work because the FrontPage VBA didn't
recognize all of the Word VBA objects. This is the macro from Word that
replaces the carriage return with a new line. It fails in FrontPage.

Sub LineBreakReplacesParagraph()
'
' LineBreakReplacesParagraph Macro
' Macro recorded 2/29/2004 by Richard Lewis Haggard
'
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=Chr(11)
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

Any suggestions on how to make the macro work in FP 2003?
 
C

clintonG

Google: "FrontPage"+"vba macro" is where I would start looking
look for clues.
 
R

Ronx

I assume you are referring to the text as displayed on a Web page, not to
the HTML code "behind" the page.

The web page does not use carriage returns or linefeeds or other system
characters to denote an end of line or paragraph. Those characters are
treated as whitespace in exactly the same way as a space character.
In HTML:
<br> designates end of line
<p> designates start of a paragraph
</p> designates end of paragraph

The shading and font can be applied using a style sheet, either externally
or embedded in the <head> section of the HTML code.

If you are referring to the HTML code - in FrontPage2003 -
Tools->Page Options - Code Formatting Tab
Set this up as required (you cannot apply shading)
Then open a page, right click in Code View, Reformat HTML
Other versions of FrontPage have similar facilities.
 
L

Lisa Wollin \(Microsoft\)

Hi, Richard,

I can probably help you, but I'm having difficulty understanding exactly
what you want to do. I know the Word object model fairly well, and all the
macro you provided does is insert a soft line break at the end of the line.
As Ronx pointed out, there is no such thing as line feed or carriage return
in HTML, and unfortunately, FrontPage doesn't know the end of the line from
the beginning, so the best that you could do is create a macro that inserts
the <br> tag (which is a soft line break in HTML) and the position of the
insertion point. This is a relatively easy macro. If you are interested,
please let me know. You can contact me directly at (e-mail address removed).
I'd be happy to help you create the macros that you need.

You might also check out the article Working with HTML Using the FrontPage
2003 Object Model at
http://msdn.microsoft.com/library/d...tml/odc_fpworkingwithhtmlprogrammatically.asp.
This article explains in detail how to use the FrontPage object model to
manipulate HTML.

--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
R

Richard Lewis Haggard

Thank you, that's very kind.

As an example, here's what I'm trying to accomplish:

http://www.haggard-and-associates.com/Deveoper Resources/MFC/Error_Report.htm

Typically, I lay out a web page in the normal FrontPage way and when I want
to insert some code, I copy it from MsDev and paste it into the web page.
Invariably, the results are not particularly appealing. The first step is to
replace the end-of-line paragraphs with a line break so that the text has
the right spacing from line to line. This is done by clicking on the line,
hitting the End key, pressing and holding the shift key and then pressing
the right arrow key. That selects the paragraph mark. To replace it with a
line break, I hit the Enter key while still holding the shift key. This is
repeated for each line that was pasted.

OK, now the line spacing is what I want. Now I want to assert Courier New 10
pt. I can't remember off the top of my head if I have to actually select the
entire text or if I simply right click anywhere within the newly modified
lines as assert the desired font.

Finally, I want the code text to be on a light gray background. I right
click anywhere in the modified text lines and then use the Format\Borders
and Shading\Shading command to select a light gray background.

See why I'd like to do this in a macro? I've set up a macro in Word that
does exactly that but was unable to copy that macro into FP and have it
work.
===
"Some days you're the windshield. Some days you're the bug. Get over it."
Richard Lewis Haggard
 
S

Stefan B Rusynko

You are using <p> when you should probably be using <pre>
(the div is your background color)
Change

<div style="background-color: #C0C0C0">
<p class="Code"><font face="Courier New" size="2">
......Your Code Here
</font></p></div>

To
<div style="background-color: #C0C0C0"><pre>
......Your Code Here
</pre></div>

Then in Code view just paste your code inside the pre tags

For new sections just copy the above to a new page and paste your new code over your old code



"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message | Thank you, that's very kind.
|
| As an example, here's what I'm trying to accomplish:
|
| http://www.haggard-and-associates.com/Deveoper Resources/MFC/Error_Report.htm
|
| Typically, I lay out a web page in the normal FrontPage way and when I want
| to insert some code, I copy it from MsDev and paste it into the web page.
| Invariably, the results are not particularly appealing. The first step is to
| replace the end-of-line paragraphs with a line break so that the text has
| the right spacing from line to line. This is done by clicking on the line,
| hitting the End key, pressing and holding the shift key and then pressing
| the right arrow key. That selects the paragraph mark. To replace it with a
| line break, I hit the Enter key while still holding the shift key. This is
| repeated for each line that was pasted.
|
| OK, now the line spacing is what I want. Now I want to assert Courier New 10
| pt. I can't remember off the top of my head if I have to actually select the
| entire text or if I simply right click anywhere within the newly modified
| lines as assert the desired font.
|
| Finally, I want the code text to be on a light gray background. I right
| click anywhere in the modified text lines and then use the Format\Borders
| and Shading\Shading command to select a light gray background.
|
| See why I'd like to do this in a macro? I've set up a macro in Word that
| does exactly that but was unable to copy that macro into FP and have it
| work.
| ===
| "Some days you're the windshield. Some days you're the bug. Get over it."
| Richard Lewis Haggard
|
| | > Hi, Richard,
| >
| > I can probably help you, but I'm having difficulty understanding exactly
| > what you want to do. I know the Word object model fairly well, and all
| > the
| > macro you provided does is insert a soft line break at the end of the
| > line.
| > As Ronx pointed out, there is no such thing as line feed or carriage
| > return
| > in HTML, and unfortunately, FrontPage doesn't know the end of the line
| > from
| > the beginning, so the best that you could do is create a macro that
| > inserts
| > the <br> tag (which is a soft line break in HTML) and the position of the
| > insertion point. This is a relatively easy macro. If you are interested,
| > please let me know. You can contact me directly at
| > (e-mail address removed).
| > I'd be happy to help you create the macros that you need.
| >
| > You might also check out the article Working with HTML Using the FrontPage
| > 2003 Object Model at
| > http://msdn.microsoft.com/library/d...tml/odc_fpworkingwithhtmlprogrammatically.asp.
| > This article explains in detail how to use the FrontPage object model to
| > manipulate HTML.
| >
| > --
| > Lisa Wollin
| > Programmer Writer
| > Microsoft Corporation
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| > rights.
| > Use of included code samples are subject to the terms specified at
| > http://www.microsoft.com/info/cpyright.htm.
| > "Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
| > | >> One of the things I have to do repeatedly is to reformat text in a manner
| >> that is more appropriate to code. I'd like to automate the process in a
| >> macro but I am having a little trouble figuring out how to do it.
| >>
| >> Here's the sequence of events I'd like to accomplish:
| >>
| >> Search forward for a new line.
| >> Select the new line characters.
| >> Replace the new line with a carriage return.
| >> Assert a shading of light gray.
| >> Assign a font of Courier New, 12 pt to the paragraph.
| >>
| >> In Word, this was dead simple and I thought I could just copy the macro
| > over
| >> to FrontPage. However, that didn't work because the FrontPage VBA didn't
| >> recognize all of the Word VBA objects. This is the macro from Word that
| >> replaces the carriage return with a new line. It fails in FrontPage.
| >>
| >> Sub LineBreakReplacesParagraph()
| >> '
| >> ' LineBreakReplacesParagraph Macro
| >> ' Macro recorded 2/29/2004 by Richard Lewis Haggard
| >> '
| >> Selection.EndKey Unit:=wdLine
| >> Selection.TypeText Text:=Chr(11)
| >> Selection.Delete Unit:=wdCharacter, Count:=1
| >> End Sub
| >>
| >> Any suggestions on how to make the macro work in FP 2003?
| >>
| >> --
| >> ----------
| >> Richard Lewis Haggard
| >>
| >>
| >
| >
|
|
 
L

Lisa Wollin \(Microsoft\)

Hi, Richard,

Stefan is correct, you can modify the HTML, and I would also suggest you use
the PRE element instead of a P element. I believe the problem is that you
are pasting the code in Design view instead of Code view. Here's the
process I've used to get code into FrontPage pages, with the exception of
HTML code which if pasted in Code view will paste as code instead of text,
which isn't what I want. Okay, here're the steps.

- Open (or create) the page you want to add the code to.
- Save the page, switch to Code view.
- Position your insertion point where you want the cod to be and run the
InsertHTML macro below.
- Copy the code from your dev environment (which I'm assuming is VS).
- Select the text "Paste Code Here" and paste the code that you just copied.

Pasting while in Code view preserves the line breaks and doesn't add extra
HTML that you have to eventually remove. The PRE element preserves your
code formatting when displayed in a browser. The one thing I would
recommend is that n VS you have specified to turn tabs into spaces. Tabs
don't work well in PRE elements. They don't work the same in HTML as they
do in other applications, and within the PRE element, each tab will
translate to a single space rather than the two, three, or four spaces that
they do in VS. (This can make programming in VS require a few more
keystrokes when you are backspacing or deleting, but the alternative is to
require more keystrokes in FP when copying and pasting the code, so IMHO,
it's a fair tradeoff.)

I used Stefan's example in the macro below to provide the shading that you
want for your code. I would suggest one other change. If you have an
external cascading style sheet, I would suggest creating a section for the
PRE element. If you don't use the PRE element anywhere else in your Web
site except code examples, you can change the appearance of all occurences
of the element. Alternatively, you can create a CSS class that will change
only the PRE elements for which you define the class. The following CSS
would work to change all occurences of the PRE element, and you wouldn't
need the DIV element surrounding your PRE element and you don't need to do
any additional formatting changes.

pre
{
background-color: #C0C0C0;
}

Hope this helps. Again, if you need anything further please let me know.

--MACRO--
Private Sub InsertHTML()
Dim objRange As IHTMLTxtRange
Dim strText As String

On Error GoTo ErrorHandler

ActiveDocument.parseCodeChanges
Set objRange = ActiveDocument.Selection.createRange
strText = "<div style=""background-color: #C0C0C0""><pre>Paste Code
Here</pre></div?"

objRange.pasteHTML strText
ActiveDocument.parseCodeChanges

ExitSub:
Exit Sub

ErrorHandler:
MsgBox "You need to save your document before you can insert the tag."
GoTo ExitSub

End Sub

Note: You can easily add this macro to your keyboard so that you don't
always have to run it from the VBE. For instructions see:
http://blogs.msdn.com/lisawoll/archive/2004/07/14/183612.aspx. The
instructions to add a button that references a macro are toward the end
under Creating a Toolbar.
--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 

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