Results 1 to 12 of 12

Thread: [RESOLVED] Word Macro - replace hard returns

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Resolved [RESOLVED] Word Macro - replace hard returns

    Hi,

    I'm new to VB but am writing a macro which is adding xml tags around Word content.

    I need to change a hard return with the text <lineBreak/>, but get an error each time.

    I have tried to search for "^|", and have also tried to search for Chr(10), but nothing seems to work. Am I using the wrong code to detect the shift-enter key combination?

    If I change the search to "^p" or Chr(13) it works fine.

    Many thanks
    pramenjatjek

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word Macro - replace hard returns

    Welcome to the Forums.

    "^p" or "Chr(13) & Chr(10)" is the solution. Did you need more help with this?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: Word Macro - replace hard returns

    Thank you... and yes please

    I tried the combination of Chr(13) & Chr(10), but it still doesn't work. This is the sub

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = Chr(13) & Chr(10)
    With .Replacement
    .ClearFormatting
    .Text = "<lineBreak/>"
    End With
    .Execute Replace:=wdReplaceAll, Format:=True
    End With

    Thanks again,
    Pramenjatjek

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word Macro - replace hard returns

    Ok, so what do you mean by "doesnt work"?

    What is the error message?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: Word Macro - replace hard returns

    What I expected to happen was for the linebreak arrow (left pointy arrow) to be replaced with the word <lineBreak/>. But instead, nothing happens. It runs, but it appears to ignore it altogether.

    When I did a search in the Word doc itself for the left pointy arrow to see that it can be detected, the code that it searches for is ^|. When I put that in the .Text line, it gives the error "Run-time error '5625'; and a little box.

    Hopefully this explanation helps?

    Pramenjatjek

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word Macro - replace hard returns

    Do you mean this character --> ¶
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: Word Macro - replace hard returns

    No, the other one which leaves no space between 2 lines. It looks like a left-pointing arrow with a vertical hook at the right-side of the arrow.

    Same function as <br> in html....

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word Macro - replace hard returns

    That sounds like the Enter key but I dont see it in word 2003. Which version are you running?

    An Enter key is actually the chr(13) and Chr(10) char combination.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: Word Macro - replace hard returns

    It's Word 2003, and the VB version is Microsoft Visual Basic 6.3.

    It's the combination of the shift + enter key that I'm trying to capture.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word Macro - replace hard returns

    Ah, a Text Wrapping Break is what its called. sorry, just really tired tonight (trying to recover from a cold).

    There has to be a short cut equilivalent for it just like the paragraph "^p". I'll see what I can find.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: Word Macro - replace hard returns

    Thanks so much and get well soon.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    8

    Re: [RESOLVED] Word Macro - replace hard returns

    This is the one that did the trick

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = ChrW(11)
    With .Replacement
    .ClearFormatting
    .Text = "<lineBreak/>"
    .Font.Color = wdColorGray40
    End With
    .Execute Replace:=wdReplaceAll, Format:=True, MatchCase:=True, MatchWholeWord:=True
    End With

Posting Permissions

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



Click Here to Expand Forum to Full Width