Add Text to the Document
Sub AddText()
Selection.TypeText "Hello from the System"
End Sub
इस VBA code का उपयोग Microsoft Word में current cursor position पर automatic रूप से text insert के लिए किया जाता है।
यह इस प्रकार काम करता है:
- Selection.TypeText command, Word को active document में जहाँ भी cursor वर्तमान में स्थित है, वहाँ text type करने के लिए कहता है।
- "Hello from the System" quote signs के अंदर का text ही Word डालेगा।
जब आप इस macro को चलाते हैं, तो Word cursor की location पर तुरंत "Hello from the System" type कर देगा - चाहे वह किसी खाली document में हो या existing text में।
संक्षेप में, यह macro manual input के बिना किसी specific text को Word document में automatic रूप से type कर देता है।
Create a New Document and Add Text
Sub CreateNewDocument()
Dim doc As Document
Set doc = Documents.Add
doc.Range.text = "This is a new document created through VBA!"
End Sub
इस VBA code का उपयोग Microsoft Word में automatic रूप से एक नया document बनाने और उसमें text जोड़ने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- यह Word document का represent करने के लिए एक variable doc declare करता है।
- Set doc = Documents.Add एक नया blank Word document बनाता है।
- doc.Range.Text = "This is a new document created through VBA!" नए document में specified text insert करता है।
जब आप इस macro को चलाते हैं, तो Word एक नई file खोलता है और उसमें वह संदेश automatic रूप से लिख देता है।
संक्षेप में, यह code आपको VBA automation के माध्यम से एक नया Word document शीघ्रता से बनाने और भरने में मदद करता है।
Save Document Automatically
Sub SaveDoc()
Dim filePath As String
filePath = "C:\Users\YourName\Desktop\MyDocument.docx"
ActiveDocument.SaveAs2 filePath
End Sub
यह VBA code Microsoft Word में आपके computer पर किसी specific location पर current document को automatic रूप से save के लिए उपयोग किया जाता है।
यह इस प्रकार काम करता है:
- यह एक variable filePath define करता है जो file के full path और name को store करता है जहाँ file save की जाएगी - यहाँ, "C:\Users\YourName\Desktop\MyDocument.docx"।
- ActiveDocument.SaveAs2 filePath command वर्तमान में open document को उस file name और location का उपयोग करके save करता है।
जब आप इस macro को चलाते हैं, तो Word आपके open document को आपके Desktop पर "MyDocument.docx" के रूप में save करता है।
संक्षेप में, यह code save की प्रक्रिया को automatic करता है, जिससे manual रूप से "Save As" पर click करने की आवश्यकता समाप्त हो जाती है।
Find and Replace Text
Sub FindAndReplace()
With Selection.Find
.text = "OldText"
.Replacement.text = "NewText"
.Execute Replace:=wdReplaceAll
End With
End Sub
इस VBA code का उपयोग Microsoft Word में पूरे document में text को automatic रूप से find और replace के लिए किया जाता है।
यह इस प्रकार काम करता है:
- With Selection.Find ब्लॉक Find और Replace सुविधा को active करता है।
- .Text = "OldText" उस word या phrase को specify करता है जिसे आप ढूँढ़ना चाहते हैं।
- .Replacement.Text = "NewText" यह define करता है कि उसे कौन replace करेगा।
- .Execute Replace:=wdReplaceAll action को perform करता है और selected area या entire document में "OldText" की सभी occurrences को "NewText" से बदल देता है।
संक्षेप में, यह macro text को automatic रूप से तेज़ी से update करता है, जिससे Find and Replace dialog box का manual रूप से उपयोग करने की तुलना में समय की बचत होती है।
Apply Bold & Font Size
Sub ApplyBoldFont()
Selection.Font.Bold = True
Selection.Font.Size = 14
End Sub
इस VBA code का उपयोग Microsoft Word में automatic रूप से bold formatting लागू करने और selected text का font size बदलने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- Selection.Font.Bold = True वर्तमान में selected text को bold बनाता है।
- Selection.Font.Size = 14 selected text के font size को 14 point में बदल देता है।
जब आप इस macro को चलाते हैं, तो आपके Word document में highlight किया गया कोई भी text तुरंत bold और larger हो जाएगा।
संक्षेप में, यह code text को एक specific style के साथ तेज़ी से format करने में मदद करता है, जिससे toolbar options का manual रूप से उपयोग करने में लगने वाला समय बचता है।
Add Page Numbers
Sub AddPageNumbers()
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _
.PageNumbers.Add PageNumberAlignment:=wdAlignParagraphCenter
End Sub
इस VBA code का उपयोग Microsoft Word में किसी document में page numbers automatic रूप से जोड़ने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument.Sections(1) document के first section को refer करता है।
- .Footers(wdHeaderFooterPrimary) उस भाग के footer area तक पहुँचता है।
- .PageNumbers.Add footer में page number insert करता है।
- PageNumberAlignment:=wdAlignParagraphCenter page number को page के center में align करता है।
जब आप इस macro को चलाते हैं, तो page numbers first section के प्रत्येक page के नीचे center में दिखाई देती हैं।
संक्षेप में, यह Word footer में centered page numbers जोड़ने को automatic करता है।
Insert Header & Footer
Sub AddHeaderFooter()
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.text = "My Header"
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.text = "My Footer"
End Sub
इस VBA code का उपयोग Microsoft Word में किसी document में automatic रूप से header और footer जोड़ने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument.Sections(1) active document के first section को refer करता है।
- .Headers(wdHeaderFooterPrimary).Range.Text = "My Header" प्रत्येक page (header area) के top पर "My Header" text insert करता है।
- .Footers(wdHeaderFooterPrimary).Range.Text = "My Footer" प्रत्येक page (footer area) के bottom में "My Footer" जोड़ता है।
जब आप इस macro को चलाते हैं, तो first section के प्रत्येक page पर ये text automatic रूप से दिखाई देंगे।
संक्षेप में, यह Word में शीघ्रता से एक सरल header और footer बनाता है।
Insert Table
Sub InsertTable()
Dim tbl As Table
Set tbl = ActiveDocument.Tables.Add(Selection.Range, 3, 3)
tbl.Borders.Enable = True
End Sub
इस VBA code का उपयोग Microsoft Word में किसी document में automatic रूप से table insert करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- यह एक table object का represent करने के लिए एक variable tbl declare करता है।
- Set tbl = ActiveDocument.Tables.Add(Selection.Range, 3, 3) current cursor position (Selection.Range) पर 3 rows और 3 columns वाली एक नई table बनाता है।
- tbl.Borders.Enable = True सभी table cells के चारों ओर borders जोड़ता है।
जब आप इस macro को चलाते हैं, तो आपके Word document में एक neatly border वाली 3×3 table तुरंत बन जाती है।
संक्षेप में, यह code manual Insert Table option का उपयोग किए बिना एक formatted table को तेज़ी से insert करता है।
Protect Document (Password)
Sub ProtectDoc()
ActiveDocument.Protect wdAllowOnlyReading, Password:="1234"
End Sub
यह VBA code Microsoft Word में किसी document की सुरक्षा के लिए उपयोग किया जाता है ताकि उसे केवल पढ़ा जा सके, edit नहीं किया जा सके।
यह इस प्रकार काम करता है:
- ActiveDocument.Protect वर्तमान में खुले Word document पर protection लागू करता है।
- wdAllowOnlyReading का अर्थ है कि users केवल document देख सकते हैं और उसमें कोई बदलाव नहीं कर सकते।
- Password:="1234" protection हटाने के लिए आवश्यक password (in this case, "1234") set करता है।
जब आप इस macro को चलाते हैं, तो document केवल पढ़ने के लिए उपलब्ध हो जाता है और दिए गए password द्वारा सुरक्षित हो जाता है।
संक्षेप में, यह code password-based protection enable करके आपकी Word file को unwanted edits से सुरक्षित रखने में मदद करता है।
Loop Through Document Paragraphs
Sub ShowParagraphText()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
Debug.Print para.Range.text
Next para
End Sub
इस VBA code का उपयोग Microsoft Word में current document के सभी paragraph text को Immediate Window (VBA editor में) में display करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- यह document में प्रत्येक paragraph का प्रतिनिधित्व करने के लिए एक variable para declare करता है।
- For Each para In ActiveDocument.Paragraphs active document के प्रत्येक paragraph को loop करता है।
- Debug.Print para.Range.Text प्रत्येक paragraph के text को view या debug करने के लिए Immediate Window में print करता है।
- यह loop तब तक जारी रहता है जब तक सभी paragraph processed नहीं हो जाते।
संक्षेप में, यह macro आपको प्रत्येक paragraph के content को सीधे VBA editor में list करने या review करने में मदद करता है।
Show MsgBox with Document Word Count
Sub ShowWordCount()
MsgBox "Total Words: " & ActiveDocument.words.Count
End Sub
इस VBA code का उपयोग Microsoft Word में current document की total word count को message box में display करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument.Words.Count खुले Word document में सभी words की count करता है।
- MsgBox command इस number को एक pop-up message box में show करता है जिसके पहले "Total Words:" label लगा होता है।
जब आप इस macro को चलाते हैं, तो आपके document में words की कुल संख्या दिखाने वाला एक message box दिखाई देता है, बिल्कुल built-in word count सुविधा की तरह।
संक्षेप में, यह एक साधारण VBA command का उपयोग करके total word count को शीघ्रता से display करता है।
Convert Word to PDF
Sub ExportPDF()
ActiveDocument.ExportAsFixedFormat "C:\Test\Export.pdf", 17
End Sub
इस VBA code का उपयोग Microsoft Word में current document को PDF file के रूप में automatic रूप से export करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument वर्तमान में खुले Word document को refer करता है।
- ExportAsFixedFormat document को another format में परिवर्तित करता है - यहाँ, एक PDF।
- "C:\Test\Export.pdf" save की जाने वाली PDF file का location और name specify करता है।
- Number 17 constant wdExportFormatPDF को represent करता है, जो Word file को PDF format में export करने के लिए कहता है।
जब आप इस macro को चलाते हैं, तो Word आपके खुले document को C:\Test folder में Export.pdf के रूप में automatic रूप से save कर लेता है।
Export Current Document as PDF
Sub ExportAsPDF()
Dim filePath As String
filePath = ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf"
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=filePath, _
ExportFormat:=wdExportFormatPDF
MsgBox "PDF Exported Successfully!"
End Sub
इस VBA code का उपयोग Microsoft Word में active document को PDF file के रूप में export करने और एक confirmation message dispay करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- यह नए PDF के location और name को store करने के लिए एक variable filePath define करता है।
- ActiveDocument.Path current document का folder देता है, और ActiveDocument.Name & ".pdf" PDF नाम बनाता है।
- ExportAsFixedFormat open document को PDF file में convert करता है और उसे उसी folder में save करता है।
- Save करने के बाद, "PDF Exported Successfully!" message box दिखाई देता है।
संक्षेप में, यह आपकी Word file को automatic रूप से PDF के रूप में save कर देता है।
Close Document Without Save Prompt
Sub CloseWithoutSave()
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub
इस VBA cod का उपयोग Microsoft Word में किसी भी change को save किये बिना current document को बंद करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument उस document को refer करता है जो वर्तमान में open और active है।
- Close method का उपयोग उस document को बंद करने के लिए किया जाता है।
- SaveChanges:=wdDoNotSaveChanges, Word को बंद करने से पहले किए गए किसी भी edits को save करने से रोकता है।
जब आप इस macro को चलाते हैं, तो खुला Word document तुरंत बंद हो जाएगा, और कोई भी save नहीं किया गया कार्य हटा दिया जाएगा।
संक्षेप में, यह code आपको बिना save किये गए document को जल्दी से बंद करने में मदद करता है, जो automation या testing के लिए उपयोगी है।
Remove Extra Spaces from Entire Document
Sub RemoveExtraSpaces()
With ActiveDocument.Range.Find
.text = " "
.Replacement.text = " "
.Forward = True
.Wrap = wdFindContinue
Do While .Execute(Replace:=wdReplaceAll)
Loop
End With
End Sub
यह VBA code Microsoft Word में किसी document से extra spaces automatic रूप से हटाने के लिए उपयोग किया जाता है।
यह इस प्रकार काम करता है:
- With ActiveDocument.Range.Find पूरे document के लिए Find और Replace tool को activate करता है।
- .Text = " " double spaces की खोज करता है।
- .Replacement.Text = " " उन्हें single space से बदल देता है।
- .Wrap = wdFindContinue यह सुनिश्चित करता है कि search whole document में जारी रहे।
- Do While loop तब तक दोहराता रहता है जब तक सभी extra spaces हटा नहीं दिए जाते।
संक्षेप में, यह macro आपके document को automatic रूप से multiple spaces को single space से बदलकर साफ़ कर देता है।
Convert All Text to Uppercase
Sub ConvertToUppercase()
Selection.Range.Case = wdUpperCase
End Sub
इस VBA code का उपयोग Microsoft Word में selected text को automatic रूप से uppercase letters में बदलने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- Selection.Range document में वर्तमान में highlight किए गए या selected text को refer करता है।
- .Case = wdUpperCase उस text के case को सभी capital letters में बदल देता है।
जब आप इस macro को चलाते हैं, तो कोई भी selected text - उदाहरण के लिए, "hello world" - तुरंत "HELLO WORLD" बन जाएगा।
संक्षेप में, यह code "Change Case" button का manual रूप से उपयोग किए बिना selected text को तेज़ी से capital letters में बदल देता है, जिससे यह title या heading को कुशलतापूर्वक format करने के लिए उपयोगी हो जाता है।
Highlight All Occurrences of a Word
Sub HighlightWord()
With Selection.Find
.text = "Word"
.Format = True
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With
End Sub
इस VBA cod का उपयोग Microsoft Word में document में किसी specific word की सभी occurrences को automatic रूप से highlight करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- With Selection.Find के साथ, Find और Replace function activate होता है।
- .Text = "Word" खोजे जाने वाले शब्द को specify करता है - यहाँ, "Word"।
- .Replacement.Highlight = True, Word को मिले text पर highlighting लागू करने के लिए कहता है।
- .Execute Replace:=wdReplaceAll उस शब्द के सभी उदाहरणों को highlight किए गए versions से बदल देता है।
जब आप इस macro को चलाते हैं, तो document में प्रत्येक "Word" highlight हो जाता है।
संक्षेप में, यह specific words को शीघ्रता से ढूँढने और उन्हें automatic रूप से visually रूप से mark करने में मदद करता है।
Autofit Table to Window
Sub AutoFitTable()
If Selection.Information(wdWithInTable) Then
Selection.Tables(1).AutoFitBehavior wdAutoFitWindow
End If
End Sub
इस VBA code का उपयोग Microsoft Word में किसी table का size automatic रूप से बदलने के लिए किया जाता है ताकि वह page की width में पूरी तरह से fit हो जाए।
यह इस प्रकार काम करता है:
- If Selection.Information(wdWithInTable) जाँचता है कि cursor table के अंदर है या नहीं।
- If true, तो Selection.Tables(1).AutoFitBehavior wdAutoFitWindow selection में पहली table को window या page की width में automatic रूप से fit करने के लिए adjust करता है।
- यह सुनिश्चित करता है कि एक neat layout के लिए columns का size proportionally रूप से बदल जाए।
जब आप cursor को table में रखते हुए इस macro को चलाते हैं, तो Word उस table के आकार को तुरंत adjust कर देता है।
संक्षेप में, यह table को document window में automatic रूप से सुव्यवस्थित रूप से fit कर देता है।
Add Colored Row to Table
Sub AddColoredRow()
Dim tbl As Table
Set tbl = ActiveDocument.Tables(1)
tbl.Rows.Add
tbl.Rows.Last.Shading.BackgroundPatternColor = wdColorYellow
End Sub
इस VBA code का उपयोग Microsoft Word में किसी existing table में एक row जोड़ने और उसे पीले रंग से रंगने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- यह किसी table को represent के लिए एक variable tbl declare करता है।
- Set tbl = ActiveDocument.Tables(1) active document में first table का selection करता है।
- tbl.Rows.Add table के नीचे एक नई row जोड़ता है।
- tbl.Rows.Last.Shading.BackgroundPatternColor = wdColorYellow उस नई row को पीले रंग की background से भर देता है।
जब आप इस macro को चलाते हैं, तो first table के अंत में एक पीले रंग की row अपने आप जुड़ जाती है।
Apply Title Style to First Paragraph
Sub ApplyTitleStyle()
ActiveDocument.Paragraphs(1).Range.Style = wdStyleTitle
End Sub
इस VBA code का उपयोग Microsoft Word में किसी document के first paragraph पर Title style को automatic रूप से लागू करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- ActiveDocument.Paragraphs(1) active Word document के first paragraph को refer करता है।
- .Range.Style = wdStyleTitle उस paragraph की style को "Title" में बदल देता है, जो Word की built-in heading styles में से एक है।
- "Title" style आमतौर पर text को larger font size, bold रूप और center alignment के साथ format करती है।
जब आप इस macro को चलाते हैं, तो first paragraph तुरंत एक professional दिखने वाले title के रूप में format हो जाता है।
संक्षेप में, यह आपके document की पहली line पर title style को तुरंत लागू कर देता है।
Insert Page Break Every 2 Paragraphs
Sub PageBreakInterval()
Dim i As Long
For i = 2 To ActiveDocument.Paragraphs.Count Step 2
ActiveDocument.Paragraphs(i).Range.InsertBreak wdPageBreak
Next i
End Sub
यह VBA macro Word document में हर दूसरे paragraph के बाद automatic रूप से page break insert करता है।
यह इस प्रकार काम करता है:
- Variable i का उपयोग paragraphs की गणना के लिए किया जाता है।
- For loop second paragraph (i = 2) से शुरू होता है और हर बार 2 से बढ़ता है (Step 2), जिसका अर्थ है कि यह हर second paragraph को छोड़ देता है।
- ActiveDocument.Paragraphs(i).Range.InsertBreak wdPageBreak प्रत्येक selected paragraph के बाद एक page break जोड़ता है।
परिणामस्वरूप, प्रत्येक second paragraph एक नए page पर शुरू होता है, जिससे content को automatic रूप से organize या separate करने में मदद मिलती है - जो reports, forms या structured documents के लिए उपयोगी है।
Add Bookmark to Selected Text
Sub AddBookmark()
ActiveDocument.Bookmarks.Add Name:="MyBookmark", Range:=Selection.Range
End Sub
यह VBA macro Word document में current selection point पर एक bookmark जोड़ता है।
यह इस प्रकार काम करता है:
- Code एक नया bookmark बनाने के लिए ActiveDocument.Bookmarks.Add method का उपयोग करता है।
- Name parameter का उपयोग करके bookmark को "MyBookmark" नाम दिया गया है।
- Range:=Selection.Range part Word को bookmark को ठीक उसी स्थान पर रखने के लिए कहता है जहाँ user का cursor है या selected text के ऊपर है।
यह document में महत्वपूर्ण positions या text को mark करने में मदद करता है, ताकि आप बाद में VBA या Word की bookmark सुविधा का उपयोग करके उन specific parts पर जल्दी से जा सकें, उनका reference ले सकें या उन्हें edit कर सकें।
Jump to a Bookmark
Sub GoToBookmark()
ActiveDocument.Bookmarks("MyBookmark").Select
End Sub
इस VBA macro का उपयोग Word document में किसी specific bookmark पर सीधे जाने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- Code ActiveDocument.Bookmarks("MyBookmark") का उपयोग करके "MyBookmark" नामक bookmark तक पहुँचता है।
- .Select method cursor को document में उस bookmark के स्थान पर ले जाती है।
यह तब उपयोगी होता है जब आप किसी specific section या word पर तेज़ी से navigate करना चाहते हैं जिसे आपने पहले bookmark का उपयोग करके mark किया था। यह लंबे documents के साथ काम करते समय समय बचा सकता है, जिससे आप VBA automation के माध्यम से महत्वपूर्ण text या predefined positions पर automatic रूप से जा सकते हैं।
Run Mail Merge to New Document
Sub MailMergeToDoc()
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
End Sub
इस VBA macro का उपयोग Microsoft Word में Mail Merge चलाने और merge किए गए results के साथ एक नया document बनाने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- With ActiveDocument.MailMerge statement वर्तमान Word document के mail merge setup तक पहुँचता है।
- .Destination = wdSendToNewDocument, Word को merge किए गए output को print या email करने के बजाय एक नए document में भेजने के लिए कहता है।
- .Execute, mail merge प्रक्रिया को चलाता है।
इस macro को चलाने के बाद, Word automatic रूप से आपके data source स्रोत से सभी merge किए गए record वाला एक नया document तैयार करता है, जो edit या save के लिए तैयार होता है।
Insert Image into Word
Sub InsertImage()
Dim imgPath As String
imgPath = "C:\Users\YourName\Pictures\Image.jpg"
Selection.InlineShapes.AddPicture imgPath, False, True
End Sub
इस VBA macro का उपयोग किसी Word document में current cursor position पर एक image insert करने के लिए किया जाता है।
यह इस प्रकार काम करता है:
- एक variable imgPath image file का full path store करता है (उदाहरण के लिए, "C:\Users\YourName\Pictures\Image.jpg")।
- Selection.InlineShapes.AddPicture command document में current selection point पर image insert करता है।
- False parameter का अर्थ है कि image original file से link नहीं है, और True यह सुनिश्चित करता है कि image तुरंत display हो।
संक्षेप में, यह code आपके चुने हुए चित्र को आपके Word document में बिना किसी manual प्रक्रिया के automatic रूप से डाल देता है।
Create Table of Contents Automatically
Sub InsertTOC()
ActiveDocument.TablesOfContents.Add _
Range:=Selection.Range, _
RightAlignPageNumbers:=True, _
UseHeadingStyles:=True, _
UpperHeadingLevel:=1, _
LowerHeadingLevel:=3
End Sub
यह VBA macro automatic रूप से Word document में एक Table of Contents (TOC) सम्मिलित करता है।
यह इस प्रकार कार्य करता है:
- TablesOfContents.Add method वर्तमान cursor (Selection.Range) position पर एक विषय-सूची (TOC) बनाती है।
- RightAlignPageNumbers:=True page numbers को दाईं ओर align करता है।
- UseHeadingStyles:=True का अर्थ है कि यह entries बनाने के लिए Word की built-in heading styles (Heading 1, Heading 2, आदि) का उपयोग करता है।
- UpperHeadingLevel:=1 और LowerHeadingLevel:=3 में level 1 से 3 तक के headings शामिल हैं।
संक्षेप में, यह code document headings के आधार पर एक formatted Table of Contents शीघ्रता से बनाता है, जिससे time और effort की बचत होती है।
