Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Mar 2002
    Location
    Sydney Australia
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Write Method (Word 2000)

    Code:

    Dim myRange

    ' select all text in document
    Selection.HomeKey Unit:=wdStory
    Selection.EndKey Unit:=wdStory, Extend:=wdExtend

    ' Assign the selection to a variable
    Set myRange = Selection.Range
    myRange.SetRange Start:=myRange.Start, End:=myRange.End

    ' Open this document
    Open "C:My Documentscopy.htm" For Append As #1

    ' Append the selected text to copy.htm
    Write #1, myRange
    Close #1

    End Sub

    The source document contains HTML code. When I open C:My Documentscopy.htm, I find that wherever there were were double quotes in the HTML code in the Word document, there are now two pairs of double quotes in copy.htm, which plays havoc with the rendering of the HTML. For example:

    <meta name="description" content="demo">

    <style type="text/css">

    becomes:

    <meta name=""description"" content=""demo"">

    <style type=""text/css"">

    Firstly, can anyone explain why this happens?

    Secondly can anyone explain how to stop it happening, or suggest an alternative approach for copying the contents of a document to a text (.htm) file?

    Thanks in advance for any assistance offered.

    PeteF

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Write Method (Word 2000)

    Why? It's the way Write # works.
    Solution? Use Print # instead of Write #. It does not try to "interpret" the data.

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Write Method (Word 2000)

    You could save a lot of lines of code by using
    Print #1, ActiveDocument.Content.Text

    Instead of all the code that defines a range and a selection.

    StuartR

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Write Method (Word 2000)

    The Write # statement is intended to work with the Input # statement, which reads data from a sequential file opened in Input or Binary mode, and assigns the data to variables. The reason why data written by Write # is delimited is so it can be read correctly by the Input # statement. These "primitive" file input/output (I/O) statements were included in early versions of BASIC that were developed in the 1980's. According to the HTML 2.0 specification, "HTML has been in use by the World Wide Web (WWW) global information initiative since 1990." Since the earlier versions of BASIC predated the widespread use of HTML, it is safe to say these I/O statements were not designed with exporting to HTML file format in mind.

    As previously recommended, use Print # statement in place of Write # to avoid unwanted delimiters. Refer to VBA Help for more info.

    HTH

  5. #5
    New Lounger
    Join Date
    Mar 2002
    Location
    Sydney Australia
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write Method (Word 2000)

    Thank you HansV for your "why", Stuart R for your code, which did the job perfectly (and saved a few lines), and MarkD for that interesting and detailed explanation.

    Your help and interest is greatly appreciated.

    PeteF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •