Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Modifying An OLE Word Doc Through VB Code

I've created an application in VB6 that allows me to browse *.cgm files (pictures). The app contains a FileListBox, a DirListBox, and an OLE Container of class "Word.Document.8". The idea is that when the user double-clicks a filename in the FileListBox, its image will replace the current picture in the embedded Word document (if one exists), and size it accordingly.

The problem I'm having is that as the code is replacing the picture, Word's menu bar is visible. This causes the MDI Children to temporarily resize, and looks very unprofessional.

I've tried using "Application.Screenupdating = False" on MY application but I get error 429, which tells me that "ActiveX component can't create object". Is there a Reference I can add to avoid this problem? Or does anyone have any other suggestions to make the application appear to run smoother, or to prevent Word's menu from appearing?


View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Modifying Word Documentsin From VB Code
I have a Table in a word document
I want to insert an image in one of the cells of the table

How can i do this?
I wrote some code
But this dosnot serve the correct purpose.
The image is inserted in the document but not in the table
Here is my code

Sub UpdateDoc()
Dim myDoc as Document

'THere is a single table in this document
set myDoc = GetObject("d:vbTest.doc")

'I want to put a picture inside the third column ,first row
'in the only table in the document

'get the picture in clipboard
Clipboard.SetData LoadPicture("c:windowsBubbles.bmp")

'Select the Cell(1,3)
myDoc.Tables(1).Cell(1, 3).Select

'The image is put in the document
'But not in the table before the table

End Sub

Thanx and Regards

Modifying Word Docs
Whenever I want to modify a Word document from within VB I always do

VB Code:
Public wdApp As Word.Application     wdApp.Documents.Open strDocName

Is there any other way?

Modifying Microsoft Word Documents
I am wondering how I can modify a MSWord document using Visual Basic. I want to do something like mail/fax merging, so whenever I send a mail/fax to multiple recipients using a Word document as the source, each mail/fax will have e.g. its Dear <name of recipients> line automatically personalised to each recipient's name.
If the mail/fax part is too long to explain, information about how I can modify the word document will also be greatly appreciated.
BTW, I'm using Visual Basic 6.0

Modifying Word Template From Excel
Hey everyone, kinda stuck here if anyone can lend a hand

The template opens but after that i get errors saying my selection is wrong.
Sub MakeWordDocument()
Dim oWord As Object
Set oWord = CreateObject("word.Application")
oWord.Visible = True ' <-- ** Optional **

With oWord
.Documents.Add Template:= _
ThisWorkbook.Path & "" & "" _
, NewTemplate:=False, DocumentType:=0
End With

With .Selection
.MoveDown Unit:=wdLine, Count:=6
.TypeText Text:="HEre"
End With
End Sub

Modifying Microsoft Word Document
I am wondering how I can modify a MSWord document using Visual Basic. I want to do something like mail/fax merging, so whenever I send a mail/fax to multiple recipients using a Word document as the source, each mail/fax will have e.g. its Dear <name of recipients> line automatically personalised to each recipient's name.
If the mail/fax part is too long to explain, information about how I can modify the word document will also be greatly appreciated.
BTW, I'm using Visual Basic 6.0

Word VBA - Reading Thru Lines, Modifying And Deleting

I need to code some VBA that will read down the lines in my document and perform modification or deletion of the line depending on the value of the first character.

I have figured out how to move down a line at a time using "Selection.MoveDown wdLine", but I am unsure how to :

1) Detect that I've reached the end of the document (i.e. the last line)
2) Delete a line
3) Modify the content of a line (e.g. remove first character)

Any help greatly appreciated.

Self Modifying Code
I know this is worst practise but I'm writing a word macro that modifies itself using the following code.

A button on a form calls a subroutine that changes the code that contains a constant string...

With Application.VBE.ActiveVBProject.VBComponents("modHighlight")
Call .CodeModule.Find("Const strFind", lLineFunctionStart, 1, -1, -1)
Call .CodeModule.ReplaceLine(lLineFunctionStart, strConst)
End With
However the new value of the constant is not picked up by the form when another event occurs. If i close the form and open it again, the new value is picked up. Without closing the form is it possible to reload/recompile the code so that the value can be used?


Self Modifying Code
I currently have a simple password routine with the username/password combinations in the code. This is not a high security application, but I would like to have the ability to add or change users or passwords from within the application.

Can someone tell me how I can store data elements in the application, and be able to modify them from an application form?



Self-Modifying Code
Is it possible to write a program, compile it into an exe, then have it modify or chage itself? If not in VB, what about some other language?

Jacob Sheehy

Multitasking - screwing up several things at once

Help Modifying Code
I found this code for removeing duplicates from a list box.

HTML Code:
For i = 0 To List1.ListCount - 1 'things to check
For y = 0 To List1.ListCount - 1 'things to check with
If y <> i Then 'do not check the same thing
If List1.List(i) = List1.List(y) Then
List1.RemoveItem i 'got duplicate remove it
i=i-1 'little bug fix!
Exit For 'speed thing up get out of this loop
End If
End If
Next y
Next i
Now how can I modify for a listview instead of a listbox and also instead of removing duplicate items, have it popup a msgbox to notify me of duplicates?

Modifying This Code...

the following code comes from sub Gen() which will generate a 'random' entry from a file - HOWEVER this is not totally random as the user may (optional) select a Cert value (from a list of optionboxes) and/or select any/all of 4 Markets.

'get filecount and pick random file
Open App.Path & "/films.txt" For Input As #1

Do While Not (EOF(1))
Input #1, Entry, Cert, Market, Drama, Action, Humour
fileCnt = fileCnt + 1

Close #1

randomNum = 1 + Int(Rnd * fileCnt)

'find random entry, if match display, if not resume random search
Open App.Path & "/films.txt" For Input As #1

Do While (randomCount <> randomNum) And (Not EOF(1))
Input #1, Entry, Cert, Market, Drama, Action, Humour
randomCount = randomCount + 1
'random file found, check attributes
If (Cert = sCert) Then



how can the market variables be correctly implemented into a subRoutine such as the one above?



Help Modifying Code..

This is another post related to one I posted earlier. I found this excellent code at FreeVBCode:

I was hoping that someone could help me extend the functionality to include the ability to manipulate the .Left, .Top, .Height and .Width properties of the form the that control is hosted on.

You can just put a default value of 100,100,100,100 for all the above properties..


What Is Self Modifying Code, Adn Can It Be Done In VB?

I have a recursive function with huge potential for stack over flow and I want to avoid it if possible. (A while loop inside a nested FOR loop :8-0

I hear that a way around this is to generate self modifying code but I have no solid understanding of what this term means in terms of code implementation.

Below is what I think the concept of self modifying code is.

Code: for i = 0 to 10
    i = i /fixed_scalar.

Is this a trivial example of self modifying code?

Cheers all

Word: Adding Toolbars/menus W/out Modifying

Is there any way that I can specify that if I'm adding a new toolbar and a new menu, that I want those changes to be saved with my file, rather than with I know that manually, I can create a toolbar in my template, and in the create options, I can specify the template to which it will be associated with, but I'm not sure how to do this through code. Can I somehow grab the commandbars of my template?

On a related note, does anyone know how to create a new CommandBarComboBox manually (that is, from within word, and without code). I can create a toolbar and add buttons to it, but can I do something more advanced like a dropdown?



Modifying Excel/VBA Code From VB6?
I've written a tool in Excel 97 / VBA that allows me to replace code modules, specified procedures etc in remote workbooks i.e workbooks held on servers across the global company. Nothing too great in that I know.
But I need to re-write this in VB6 and cant seem to get access to the code modules to get count of lines etc etc

Can anyone shed some light on how to do this from VB6?


Must Recompile After Modifying Code?
I took an existing vba code - an .xls file and a .xla that the .xls makes reference to - and added some new code to it (snippets of new lines into existing modules as well as a NEW module).

My colleague runs this program on his computer. The program downloads real time data from a 3rd party source (Bloomberg I believe) which I do not have access to. So I have been coding without the ability to test.

To no major surprise, the program has not been functioning correctly. And I am wondering if it maybe that the program has to be compiled on his computer before running?

I thought of this because I added some new public sub's. But when I go to the .xls -> run macro I do not see some of the new public subs in the listbox.

Modifying Code During Runtime
how would i go about adding information to empty arrays during runtime?

for example i have a program that has the following array:-

Option Explicit

Public myarray(1 to 10) as string

myarray(1) = "number one"
myarray(2) = "number two"
and each block in that array is outputted to a text box

now i need a screen where the user can input information into a textbox, (like one word or a number) click a command button and it will save the information to myarray(i) - i being the next free space in the array.

any suggestions??


Modifying VB Code, From Another VB Application ??
Does anyone know of a control or some kind of dialog box that could allow me to type a function in, that would prompt you and come up with errors like when writing a function in the normal way on the script side ?

The reason behind this is I am writing a VB app that allows the user to modify the code of another VB application, then compile it. At the minute I am just using a text box, but of course when I come to complile it on the fly, there may be errors in the function code that would have shown up if I had the right control/display.

If anyone could help it would be much appreciated, or if you need me to explain things a little more please ask


Modifying AutoExec.bat Thru VB Code
I need to write a program that will be executed 1 time during the install of an application. This program needs to open the Autoexec.bat file and insert a line that will cause the main program to execute on bootup. This program will be ran on an NT 2000 server. What would be the best way to do this?


Help With Modifying This Code For Bullet Path

I have some code I downloaded that fires a bullet based on the angle and power of the gun. And it draws a circle as it fires. It does great but when the bullet is going either up or down (not curving much) it is spaced out far so the bullet doesn't look smooth when its traveling.

Below is the code the determines the new X/Y for the next bullet position. Can anyone tell by this part alone how to increase more positions of the bullet so theyr are closer? Thanks!!!!


Const PI As Double = 3.14159265358979 'PI

'find the new x and y for the gun on a circilar axis
X = lneBarrel.X1 + (Sin(((playerGunAngle / 360) * (2 * PI)) + (PI / 2)) * -playerGunPower)
Y = lneBarrel.Y1 + (Cos(((playerGunAngle / 360) * (2 * PI)) + (PI / 2)) * -playerGunPower)
X = (lneBarrel.X1 - X) + lneBarrel.X1

Software Registration - Self Modifying Code
I need to write a module that will register my application as securely as possible. Once downloaded the application goes back to the web to authorise itself, and if successful must store the fact that it is now registered internally such that noone can (easily) edit it. Can VB modify its object code in such a fashion? Can anyone tell me how, or point me somewhere where there are more resources? Regards, James.

Modifying Proper Case Conversion Code - Help!

i have a user form to automate letter creation and i started off by using StrConv with vbProperCase on the KeyPress and Exit events of the text box controls (using VBA in Word)
and this worked fine until I realised the exceptions ie. Mc and O'Malley etc

so i found this nifty piece of code in the FAQ's

Code:Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 'only allow propercase text in a text box
  If KeyAscii <> 8 Then
    Dim iPos As Integer
    iPos = TextBox2.SelStart + 1
    Dim sText As String
    sText = Left$(TextBox2.Text, iPos)
    If iPos = 1 Then GoTo Upit
    KeyAscii = Asc(LCase$(Chr$(KeyAscii)))
    If iPos > 1 And _
     (InStr(iPos - 1, sText, " ") > 0 Or _
      InStr(iPos - 1, sText, "-") > 0 Or _
      InStr(iPos - 1, sText, ".") > 0 Or _
      InStr(iPos - 1, sText, "'") > 0) _
      Then GoTo Upit
    If iPos > 2 Then _
      If InStr(iPos - 2, sText, "Mc") > 0 _
        Then GoTo Upit
   End If
  Exit Sub
  KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Sub

now this works great for names with Mc and hyphenated names and also place names with "." in them but as I understand this only reacts to the keypress hence the "KeyAscii" part of it, it does not work if i apply it to an Exit event - reason I want to do this is because if someone pastes text into the field, I also want it to be converted eg. if they paste something in UPPER case or in mixed case. StrConv works fine on Exit event to allow this to happen but if i apply StrConv on exit, i lose any special formatting that takes into account the exceptions i mentioned earlier.

in a nutshell i would like some help with:
-modifying this code to allow for pasted text to be converted not just as it is typed
-modifying this code to allow for the exceptions "Po" and "Gpo" which need to be converted to UPPER case (normally if someone types Po or Gpo there is a space afterwards so I need it to go back and change the previous letter.

-also allowing this code to be in the base module to be accessed from anywhere. the original code was a Public Function LwrToUpper (Control As Object, KeyAscii As Integer) but when I try and call the function VBA will not accept the arguments??
how would i do it for my TextBox2 control?

Edited by - jonnysx on 9/29/2007 12:34:27 AM

Modifying Some Winsock Code To Add Proxy Support (hard) - Please Help!! *UNRESOLVED*
Im trying to modify anothers source to logging in to Yahoo Chat by adding proxy support.

However, the source already contains a lot of code in the _Connect, and _DataArrival events, and if i connected thru a proxy first, those events would fire causing some errors and weird strings to be sent to the server.

I need to know, how can i connect to a proxy (WS.Connect "", 80) and wait until its connected? Then, after its connected to the proxy, how can i code it so it will go thru the original code the author put in to connect to the chat server? - that is, how to get the code in the _Connect/_Dataarrival events to go to the right server.

Here are some snippets of the code that needs to be changed to add proxy support.

The connection (command1_click)
VB Code:
WS.Connect txtServer

WS_Connect event
VB Code:
tcpheader = HEADER & CalcSize(Len(UserName) + 5) & Chr(0) & "W" & Chr(0) & Chr(0) & Chr(0) &_Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & "1" & Divider & UserName & DividerWS.SendData tcpheader

VB Code:
Dim DataType As IntegerOn Error Resume NextWS.GetData WData, vbStringDataType = Asc(Mid(WData, 12, 1))HandleData DataType, Right(WData, Len(WData) - 20)

The HandleData sub just checks the data received to see what is happening in the chatroom (ie logged off, entering chat, joining room, etc)

So, how can i edit this to add proxy support?
Here is how proxies work:
First to connect - use WS.Connect "", 80
in telnet, i found that after u were connected, the string "CONNECT " & txtServer is used to connect to the chat server thru the proxy. If connected successfully, the server returns "HTTP/1.0 200 Connection Established"..

so, how can i use this information to add proxy support in the program? What snippets need to be changed and how?

Resolved - Is There A Way To Insert A Word File Into Another Word Doc Via Code?
I have 2 word files created but I need to make them one. Is there a way to code this so it is done automatically and without user input? I need to put one of the files into a specific place in the other word document. I have a bookmark where it needs to be but the way I have it coded it only puts the path not the actual file. I know there is an “Insert File” option in Word but how can I use it in VB? And how can I make it insert in the correct place?

'Put in HotFile Data

wordApp.Selection.Text = (CurrentProject.Path & "Word Audit ReportsHotFile.doc")

End With

--------------------- this is the code that worked for me ---------------

wordApp.Selection.InsertFile CurrentProject.Path & "Word Audit ReportsHotFile.doc"

VB Code In Word
Is there a way (other than colouring manually) to get coloured VB Code into MSWord?



Code To Word
Does anyone know a good way to put your vb code into word without to much trouble?

And while I'm at it, does anyone know who to change the coller of a buttons caption text? to red ex.

Help Me ! The VB Code Can't Word
this VB code can't work when the HTML page have frame
but it can work when the HTML page don't have frame.
how can i do if the HTML page have frame when i want to control
the text1.value?

Help With Using Code For Word 97
What is the code to:
1) return to a new line
2) setting tabs and activating the proper tab spacing

Can anybody shed any light on this?

Help Me With My MS Word Code Please
Hi everyone,
I am trying to modify the code shown below, which currently moves a user-selected shape and/or text box to the positions shown below.

I now need help to modify the code below to allow a user to move frames. The users will be creating these frames themselves within this Word document. I'm trying to get them to move to the same positions shown below.

Could anyone help me please! Thank You.

Note: the processes will be executed when a button is clicked on my custom Word Toolbar. I've been searching sites but no luck...

Sub MoveShapeToSideBar()
If Selection.ShapeRange.Left >= InchesToPoints(3.5) Then
With Selection.ShapeRange
Left = InchesToPoints(0.65)
RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
End With
With Selection.ShapeRange
Left = InchesToPoints(6.1)
RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
End With
End If
End Sub

Vb6 Write Vba Code To Word?
Ok, I know this is a little confusing, but here goes....

Is it possible from vb6 to insert code into vba in word? - I have code which needs to be added to documents when the user selects to... For practicality, I can't replace their (otherwise I could just store it there).

I have noticed that adobe (and others) include their templates (ie. when I open the vba editor, additional templates from different software houses appear, and are unviewable), this would work too, as it would leave intact anything the user already has in their, any idea how to do that? (programmatically)

the background is:
We have a vb6 interface with Jet Database, the user's enter client data, then create reports (over 200 available), in word using automation.
So far so good.
However we want to be able to let the user create new reports, hence we need to programmatically modify the vba in their document.

Your assistance (even just a bump in the right direction!) would be greatly appreciated.


Code For Undo In Word VBA
Anyone know how to replicate the pressing of the undo button within a Word macro?

I usually depend on the "record macro" feature to start developing macros, but you cant select Undo whilst recording macros.

I'm trying to get this to work, where "UNDO" is a line of code that replicates pressing the undo button, and 'undovalue' is a global variable that stores the amount of times to "press undo":
Sub Undo_Macro()

For counter = 1 To undovalue
Next counter

End Sub

any ideas?

cheers, Simon

Paste Code Into Ms Word
Is it possible to paste code into MS Word, with comments coming up as green and different colours for the different syntaxes?
Thank you,

My Code And Word 2002
Hi have a problem and I think its to do with because I have word 2002. I am trying to call the spellchecker in word this is my code:

Private Sub mnuSpellChecking_Click()
Clipboard.SetText txtInput.Text 'Copy Text to Clipboard
Call SpellCheck
txtInput.Text = Clipboard.GetText
MsgBox "Spell Check Complete"
End Sub

Public Function SpellCheck()
MsgBox "hello: " & txtInput.Text
Set objSpellCheck = CreateObject("Word.Application") 'Set up reference to WORD
objSpellCheck.Visible = False 'Hide WORD from user
objSpellCheck.Documents.Add 'Open New Document (hidden)
objSpellCheck.Selection.Paste 'Paste Text into WORD
objSpellCheck.CheckSpelling 'Activate the Spell Checker
objSpellCheck.Visible = False 'Hide WORD from user
objSpellCheck.Select 'Select the corrected text
objSpellCheck.Selection.Cut 'Cut the text To Clipboard
objSpellCheck.Close SaveChanges:=0 'Close & DO NOT save changes
objSpellCheck.Quit 'Quit
Set objSpellCheck = Nothing
End Function
and at the top I have: Public objSpellCheck

But i get an error saying the clipboard is either empty or not allowed on the line: objSpellCheck.Selection.Paste

Any ideas and is it because of word 2002

How To Export VB Code To Word?

Just wondering if there was a way to do this, so all it's formatting stays the same. What is VB's documentation facility?

Would be grateful for your help.

Displaying VB Code In MS Word
i know on these forums we can use [highlight=VB] and [highlight=VB] with the / before but is there a way to do this in ms word?if so how?


Copy Code To Word
does anyone know how to copy visual basic code to word, and keep all the colours after the apostrophe. when i copy my code it all comes out black and it will take a long time to colour it in green afterwards. is there an easy way of doing this. somethin similar to the [highlight=VB] command in these forums, but on microsoft word 2003

Can You Word Wrap Your Code?
Pretty annoying when i always have to scroll over the the right of the screen to see my code. Seeing as i run in 800x640 this happens a lot.

Can Someone Help Me With A Bit Of Code (VBA Word) To Get This To Work?
I need to make sure that two documents are placed in Words Startup folder. I have been looking around at installers but none of them seem to be able to check which folder Word is using as startup. I have found out that there is a way to do it but it needs to be done with VBA. It would need to be work something like this:

Have a document with a button in it saying something like install. When the user presses the button Word would check for its starup folder location, I believe like this:


And then copy the two files, "" + "data.dat" which are in the same folder as this document to Words Startup folder.

Because someone might have their security options on high they might not be able to run the Macro and would not be warned either.

I thought maybe to have a line of text in red under the button saying "If you can see this text your macro security settings are to high. Please change them before you run the installer"

So if there could be a Macro that would run as soon as the document was opened it would turn any red text white, which would mean that the user wouldn't see this warning and could run the macro from the button.

I would be grateful for some help.

Thanks in advance for any help.

Why The Code Didn't Word
dim astring as string
dim re as string
for astring="a" to "z"
re=re & astring
next astring

Exporting VB Code To Word Doc ??
How I can Export the VB code to word document with the same color format ???

I mean with :

Green comment

and Blue functions

Please help


VB Code Snippets To MS Word
I thought of placing 'code snippets' in a word doc with HyperLinks
to Bookmarks locating these examples....

Well, my Q is when pasted to Word(97) the formatting is lost.
The pasted code contains no tabs/returns.

I guess I'm slow today, but do I need a RTF converter???



VB Code In Word Document
Does anyone know if the following is possible: I'd like to paste or import some VB code into a word document, but i'd like to keep the colors (f.e. bleu for codewords, green for comment).

I wouldn't like waste time changing all the colors manually.
And the code is too large to just take a screendump...

I don't know if this is possible... does anybody of you guys?

Running VB Code In MS Word.
¿Can someone help me?
I made a MS Word document with some Visual Basic objects. This is, using the "Control Toolbox" toolbar. I put to my document check boxes, option buttons, command buttons, and a list box. And added some Visual Basic code to the "click" event of all command buttons.
When I run the code, this is when I press the "Exit Design Mode" button of the "Control Toolbox" toolbar, the code does not execute properly. The check boxes and option buttons work fine because I can make selections (check, uncheck, select) but the code under the "click" events of all command-buttons does not execute. What is wrong? What is missing? Any thoughts?
Best regards,
Luis R. Villegas H.
LVVL1000 (at)

Embedding Word Into My VB App, All Through VB Code
I think I'm posting this in the correct forum, but if not, please let me know and I'll switch to general discussion or another...

I need to make an instance of Microsoft Word 2000 on my form in VB. I need to have it all dynamically created through code so that a user can either open an existing word document or create a new one to work with. What is the best way for accomplishing this? I would also need to do some simple text manipulations to the document as it's open (through code).

If anyone could give me some good links or tips on how to best accomplish this, I would appreciate it immensely. Additionally, please comment on whether or not all of the features of Word are including after embedding it into my application (things like mail merge, etc) or if I have to add those features in some way...

Oh, also just to make sure I'm clear, I want Word to actually be embedded on the form, I can't have it open word outside of the application or something like that...

Thanks in advance!


Edited by - KowCiller on 7/29/2002 9:03:31 AM

How Can I Lock A Word Doc From VB6 Code
I'm quite new to VB and Word coding so please accept my ignorance.

I have a Word doc which is populated from VB 6 using bookmarks in the Word doc. Once populated I need the Word doc to be locked so that no changes of any sort can be made to it but I am not to sure about what code to use.

Please help me out if you can.

Thank You,


Word Processor Code
Hello to all vb developers.

Has anyone ever made a MDI Word Processor before? If you have can I please look at the code bacause I need some tips in writing mine?

Thanks, Rowan.

Time is what i need. Time is what I don't have.

Modifying An Exe
Just wondering how to add information into a exe file. Say change a program so it shows a message box. I have basic knowledge of ASM.

Modifying N Using Dll In Asp
Dear dost
I've created dll in and used it in ASP pages.I got success.but now I've to modify this dll and again iI have to use it.I modified dll file and made it's dll by building it in I followed the sam procedure which I followed last time.but in result it's showing me privious result only.If I delete this file from system32 folder even though it's working.
so plssssssssss tell me how can I use the modified dll in ASP pages.
Thanks in advance.

Uttam Kumar

Self Modifying App
Is it possible for an app to open itself in binary mode then make changes to itself? I thought once I worked it out but I lost the code already, it is for the purpose of making a trialware... To determine how many times an app has been run without using the registry since I dont trust the registry in such situations....

I hope somebody shed light on this......

Copyright © 2005-08, All rights reserved