Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-22-2024, 09:58 AM
Trey@Shaffer-Family.com Trey@Shaffer-Family.com is offline vba-MSWord, Execute string as command Windows 10 vba-MSWord, Execute string as command Office 2016
Novice
vba-MSWord, Execute string as command
 
Join Date: Sep 2024
Posts: 1
Trey@Shaffer-Family.com is on a distinguished road
Default vba-MSWord, Execute string as command

At a high level, I am building a format command, within VBA, like,
"ActiveDocument.Words(intWordPointerL).Font.It alic = True"

I then want to execute the command.

At first it seemed like "Eval" or "Application.Run" might be a solution, but I haven't gotten them to work.
Seems like they are looking for existing function/sub references, rather than a free form string.
I'd like something like the PowerShell Invoke-Expression command.

To simplify, I'm working with the base command, preformed,
"ActiveDocument.Words(intWordPointerL).Font."
Then appending one of a variety of qualifiers, like "Italic = True".


Tried using "Eval".
Tried using Application.Run.
Both seem to require a reference to and existing Macro.

If I can't invoke the command directly, can I create a macro on the fly and then invoke it?

I found this answer to a similar question relative to Excel,
VBA - Execute string as command in Excel - Stack Overflow

If it works in Excel, it could probably be adapted to work in Word.
I'm not strong in VBA.
I don't understand how to convert this Excel-centric code for MS-Word usage.

================================================== ===========================
Sub test()
Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_S tdModule)
VBComp.Name = "NewModule"
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("NewModule").C odeModule
Dim l_counter As Long


l_counter = 1
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub MyNewProcedure()" & Chr(13) & "UserForm1.chb_g_ba" & l_counter & ".Visible = True" & Chr(13) & "End Sub"
End With
'run the new module
Application.Run "MyNewProcedure"
UserForm1.Show
'Delete the created module
ThisWorkbook.VBProject.VBComponents.Remove VBComp
End Sub
Reply With Quote
  #2  
Old 09-22-2024, 10:26 AM
Charles Kenyon Charles Kenyon is offline vba-MSWord, Execute string as command Windows 11 vba-MSWord, Execute string as command Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,474
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I moved your post to the vba forum.


However, I strongly urge you to learn to use Styles rather than direct formatting.
The Importance of Styles in Microsoft Word

You can put all of your formatting into a style and simply apply the style. You can assign a keyboard shortcut to a style just as you can to a macro. This will make for documents that are more efficient (smaller) and easier to edit.
Reply With Quote
  #3  
Old 09-22-2024, 10:40 AM
Charles Kenyon Charles Kenyon is offline vba-MSWord, Execute string as command Windows 11 vba-MSWord, Execute string as command Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,474
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Cross posted and answered at Redirecting
For cross-posting etiquette, please read: A Message to Forum Cross-Posters
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Show table list by using chek box command and save document on sharepoint by using command button Zhibek Word VBA 3 08-16-2018 06:19 PM
vba-MSWord, Execute string as command Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Using MSWord 2003 docs in MSWord 2010 Joe Watson Word 7 05-31-2014 11:27 AM
vba-MSWord, Execute string as command Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft