Link to home
Start Free TrialLog in
Avatar of Maliki Hassani
Maliki HassaniFlag for United States of America

asked on

VBA to VB Script - Code modify

Hello Experts,

First off, I am new to VBScript so please bare with me.  I have a VBA script in MS Word, that I need to have it recoded to work in a VB script.  I was wondering if someone could help me out.  Here are the 4 scripts:

Option Explicit


Sub SetBold()

    Const Mark As String = "BHN-"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart(") -", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetRestBold()

    Const Mark As String = "Score:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart(" - ", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel6()

    Const Mark As String = "LEVEL 6 Events:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 6 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub
Sub SetBoldLevel5()

    Const Mark As String = "LEVEL 5 Events:"
    
    Dim Rng As Range
    Dim Rstart As Long, Rend As Long
    Dim Fnd As Boolean
    
    Rstart = 1
    Do
        Rstart = GetStart(Mark, ActiveDocument.Range(Rstart), Rstart)
        If Rstart = 0 Then Exit Do
        
        Set Rng = ActiveDocument.Range(Start:=Rstart + Len(Mark) + 1)
        Rend = GetStart("LEVEL 5 Events:", Rng, Rstart + Len(Mark) + 1)
        Fnd = CBool(Rend)
        If Rend = 0 Then Rend = Rstart + Len(Mark) - 1
        
        Set Rng = ActiveDocument.Range(Start:=Rstart, End:=Rend)
        Rng.Bold = True
        Rstart = Rend + 1
    Loop While Fnd
End Sub

Open in new window




Below is the VBScript that I am working with.  Notice I added a section for insert VBA code.

'Language = VBScript
'Script to process, format, and email the Weekly Event Doc


Sub FormatReport

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Documents and Settings\lspaz\Desktop\Test Report Automation\XXX.docx")
***INSERT VBA CODE
objWord.Quit

Open in new window


Since I am only bolding specific text..  Perhasps I should simplify this and write it in VB Script?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please note I added the missing getStart function and some code at the start to get an open document to make testing easier but deleting lines 6,7 & 10-12 will of course re-instate your code for the specific document in a new instance of word

Chris
Avatar of Maliki Hassani

ASKER

That is brilliant!  I was having such a hard time  Thank you so much..  It works, I will let you know if I have any questions though!
Thank you!