How to Pass an Excel Variable from a Macro to a Word Macro

joseluism0

New Member
Joined
Feb 4, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello every one,

I´m trying to use a variable from excel in a word macro but it just doesn't work, the variable I´m trying to move is nname. Hope someone can give me a hand with this.

Excel macro

Option Explicit

Public msword As Object
Public nname As String


Sub prueba()

nname = Range("e3").Value

MsgBox nname

Set msword = CreateObject("word.application")

With msword

.Visible = True
.documents.Open "c:\procedure\template 2.docm"
.Activate
.Run "replace1", nname

End With

Set msword = Nothing

End Sub

Word Macro

Sub replace1()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim oShp As Shape


TryAgain:
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
SearchAndReplaceInStory oShp.TextFrame.TextRange, _
pFindTxt, pReplaceTxt
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)


With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "wellname"
.Replacement.Text = nname

.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

rollis13

Active Member
Joined
Jul 30, 2012
Messages
426
Office Version
  1. 2016
Platform
  1. Windows
All you need is to fix/add 5 lines of code in your Word macro, have a try.
VBA Code:
Option Explicit

Sub replace1(ByVal nname As String)               '<= changed
  
    Dim rngStory As Word.Range
    Dim lngJunk As Long
    Dim oShp   As Shape
    Dim pFindTxt, pReplaceTxt                     '<= added

TryAgain:
    'Fix the skipped blank Header/Footer problem
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    'Iterate through all story types in the current document
    For Each rngStory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
            SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt, nname '<= changed
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6, 7, 8, 9, 10, 11
                    If rngStory.ShapeRange.Count > 0 Then
                        For Each oShp In rngStory.ShapeRange
                            If oShp.TextFrame.HasText Then
                                SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt, nname '<= changed
                            End If
                        Next
                    End If
                Case Else
                    'Do Nothing
            End Select
            On Error GoTo 0
            'Get next linked story (if any)
            Set rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    Next
  
End Sub

Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, _
    ByVal strReplace As String, nname As String) '<= changed

    With rngStory.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "wellname"
        .Replacement.Text = nname
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
  
End Sub
 
Last edited:
Solution

joseluism0

New Member
Joined
Feb 4, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
All you need is to fix/add 5 lines of code in your Word macro, have a try.
VBA Code:
Option Explicit

Sub replace1(ByVal nname As String)               '<= changed
 
    Dim rngStory As Word.Range
    Dim lngJunk As Long
    Dim oShp   As Shape
    Dim pFindTxt, pReplaceTxt                     '<= added

TryAgain:
    'Fix the skipped blank Header/Footer problem
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    'Iterate through all story types in the current document
    For Each rngStory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
            SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt, nname '<= changed
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6, 7, 8, 9, 10, 11
                    If rngStory.ShapeRange.Count > 0 Then
                        For Each oShp In rngStory.ShapeRange
                            If oShp.TextFrame.HasText Then
                                SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt, nname '<= changed
                            End If
                        Next
                    End If
                Case Else
                    'Do Nothing
            End Select
            On Error GoTo 0
            'Get next linked story (if any)
            Set rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    Next
 
End Sub

Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, _
    ByVal strReplace As String, nname As String) '<= changed

    With rngStory.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "wellname"
        .Replacement.Text = nname
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
 
End Sub
Worked perfectly!!!!! thanks a lot
 

rollis13

Active Member
Joined
Jul 30, 2012
Messages
426
Office Version
  1. 2016
Platform
  1. Windows
Glad having been of some help(y).
 

Watch MrExcel Video

Forum statistics

Threads
1,130,154
Messages
5,640,447
Members
417,143
Latest member
boukadidanizar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top