Results 1 to 31 of 31

Thread: MS Word Error(Resolved)

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    MS Word Error(Resolved)

    I'm taking an ascii data file and converting/moving the data into MS Word tables.
    It opens MS Word with the completed documemt.
    I've gotten that part working well.

    But here's the problem:
    It can not make more that one word doc unless you close/restart the program.

    If I try to make two documents in a row I get the following msg:
    Code:
    Runtime Error '462'
    The remote server machine does not exist or is unavailble.
    I get this error at line:
    Set myRange = ActiveDocument.Content

    I've only tried doing a second document after MS Word has been saved and closed.

    Full code:

    VB Code:
    1. Private Sub InsertToTemplate3(Columns() As String, ByRef Year As Integer)
    2. 'inserts the preformayed columns text into the template then saves it
    3.     Dim sPath As String
    4.     Dim MSWord As Word.Application
    5.     Dim TemplateDOC As Document
    6.     Dim myRange As Word.Range
    7.     Dim lM As Long
    8.     Dim lD As Long
    9.     Dim sFind As String
    10.    
    11.     staBar.Panels(1).Text = "Initalizing MSWord"
    12.     sPath = App.Path & "\Template\TableTemplate12g.dot"
    13.     Set MSWord = New Word.Application
    14.     With MSWord
    15. '        .Visible = True
    16.         'Load the template
    17.         Set TemplateDOC = .Documents.Open(sPath)
    18.         .Selection.WholeStory
    19.         .Selection.Copy
    20.         'create a ne document from the template
    21.         .Documents.Add DocumentType:=wdNewBlankDocument
    22.         .Selection.PasteAndFormat (wdPasteDefault)
    23.         Set myRange = ActiveDocument.Content
    24.         'notify the user
    25.         staBar.Panels(1).Text = "Creating Word Document"
    26.         Screen.MousePointer = vbDefault
    27.         ProgressBarInitalize 365
    28.        
    29.         'Set the Name of Location
    30.         sFind = "Name of Location"
    31.         myRange.Find.Execute FindText:=sFind, _
    32.                              MatchCase:=True, _
    33.                              MatchWholeWord:=True, _
    34.                              ReplaceWith:=m_sLocName, _
    35.                              Replace:=wdReplaceAll
    36.         'Set the Year
    37.         sFind = "Year"
    38.         myRange.Find.Execute FindText:=sFind, _
    39.                              MatchCase:=True, _
    40.                              MatchWholeWord:=True, _
    41.                              ReplaceWith:=CStr(Year), _
    42.                              Replace:=wdReplaceAll
    43.        
    44.         For lM = 1 To 12
    45.             For lD = 1 To 31
    46.                 ProgressBarDecrement
    47.                 'add the Day Numeral
    48.                 sFind = "Dm" & CStr(lM) & "d" & CStr(lD)
    49.                 myRange.Find.Execute FindText:=sFind, _
    50.                                      MatchCase:=True, _
    51.                                      MatchWholeWord:=True, _
    52.                                      ReplaceWith:=Columns(lM, 0, lD), _
    53.                                      Replace:=wdReplaceAll
    54.                 'add the Day Name
    55.                 sFind = "Wm" & CStr(lM) & "d" & CStr(lD)
    56.                 myRange.Find.Execute FindText:=sFind, _
    57.                                      MatchCase:=True, _
    58.                                      MatchWholeWord:=True, _
    59.                                      ReplaceWith:=Columns(lM, 1, lD), _
    60.                                      Replace:=wdReplaceAll
    61.                 'add the Moon Phase
    62.                 sFind = "Sm" & CStr(lM) & "d" & CStr(lD)
    63.                 myRange.Find.Execute FindText:=sFind, _
    64.                                      MatchCase:=True, _
    65.                                      MatchWholeWord:=True, _
    66.                                      ReplaceWith:=Columns(lM, 2, lD), _
    67.                                      Replace:=wdReplaceAll
    68.                
    69.                 'add the Time column
    70.                 sFind = "Tm" & CStr(lM) & "d" & CStr(lD)
    71.                 myRange.Find.Execute FindText:=sFind, _
    72.                                      MatchCase:=True, _
    73.                                      MatchWholeWord:=True, _
    74.                                      ReplaceWith:=Columns(lM, 3, lD), _
    75.                                      Replace:=wdReplaceAll
    76.             Next
    77.         Next
    78.         .Visible = True
    79.     End With
    80.     'Clear the progress bar
    81.     ProgressBarClear
    82.     staBar.Panels(1).Text = "Processing Complete"
    83.     staBar.Panels(2).Text = "Use MS Word to save the file."
    84.     'kill the MS Word objects
    85.     TemplateDOC.Close
    86.     Set MSWord = Nothing
    87.     Set TemplateDOC = Nothing
    88.     Set myRange = Nothing
    89. End Sub
    Last edited by longwolf; Jul 1st, 2005 at 06:48 PM. Reason: Resolved

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: MS Word Error

    Do something like this when you open Word.

    VB Code:
    1. Public Sub OpenWord()
    2.  
    3.     Dim hWnd As Long
    4.     Dim nRpt As Integer
    5.    
    6.     On Error GoTo ErrorRoutine
    7.    
    8. StartOver:
    9.     hWnd = FindWindow("OpusApp", vbNullString)
    10.     If hWnd = 0 Then
    11.         Set wdApp = New Word.Application
    12.     Else
    13.         Set wdApp = Word.Application
    14.     End If
    15.  
    16.     Exit Sub
    17.  
    18. ErrorRoutine:
    19.     Select Case Err.Number
    20.         Case -2147023174
    21.             ' The user has manually shut down Word, so we need to
    22.             ' clean up and then restart Word.
    23.             For nRpt = 1 To wdApp.Documents.Count
    24.                 wdApp.Documents(nRpt).Close SaveChanges:=wdDoNotSaveChanges
    25.             Next nRpt
    26.             wdApp.Quit
    27.             Set wdApp = Nothing
    28.             Resume StartOver
    29.         Case 462
    30.             Resume Next
    31.         Case Else
    32.             DisplayError "OpenWord"
    33.     End Select
    34.  
    35. End Sub

  3. #3

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Thanks Martin.

    I adapted the code to work within my sub because I'm not using global MS Word objects

    But I'd like to know what the full msg is that should go with..
    VB Code:
    1. Case Else
    2.             DisplayError "OpenWord"
    3.     End Select

    I've hit it twice with two different error numbers.

  4. #4

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Error

    You should fully qualify your word objects when dimensioning them.
    VB Code:
    1. Dim TemplateDOC As Document
    2.  
    3. 'Should be
    4. Dim TemplateDOC As Word.Document
    Also, when you destroy your word objects you should do it in reverse order of their structure within word.
    VB Code:
    1. 'kill the MS Word objects
    2.     Set myRange = Nothing
    3.     TemplateDOC.Close
    4.     Set TemplateDOC = Nothing
    5.     Set MSWord = Nothing
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Thanks guys,

    I made the changes.

    I still get an error when I try to do two files back to back.

    But now it get's errors at:
    VB Code:
    1. myRange.Find.Execute ......
    And gives error:
    "Object variable or With block variable not set"

    Checking myRange shows that myRange = nothing

  7. #7

  8. #8

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Quote Originally Posted by MartinLiss
    I know you say you aren't global MS Word objects, but certainly your myRange.Find.Execute statement must be prepended with a local Word object at least.

    Boy did I just get a good scare!

    I took the:
    Code:
    	Set myRange = ActiveDocument.Content
    bits from the help file.
    The range object allows the use of vbCr while the find object doesn't

    After reading Martins last post I changed the code to:
    Code:
    	Set myRange = .ActiveDocument.Content
    So that the dot would plug it into the MSWord object.
    Then tried doing two doc's in a row. (After closing the 1st one.)

    The code seemed to be working and was about half done when I black screened.
    The comp rebooted itself and told me I didn't have a MBR!

    Turned out to be a lose cable

    Now the app is working fine when the first document gets closed before making a new one.
    I'll do a test without closing the 1st doc now.

    Thanks Martin and Rob, Y'all are great!

  9. #9

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Wellllll, the new test was interesting. (making a 2nd doc while the 1st is still open)
    It did make the 2nd document, but I got two odd pop-ups.

    The first one said


    After using the 'switch to' button I got this one:


    It must be referring to the template, it's full of tags like:
    Dm1d1
    Wm7d3
    Sm4d25
    The replacement text is almost all numeric.

    I also get a msg about 'a lot of data has been added to the clipboard.....' when the document is closed.


    It'd be nice to get rid of those.

    I wouldn't want the users to think something is wrong.
    Attached Images Attached Images   

  10. #10

  11. #11

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Quote Originally Posted by MartinLiss
    Have you tried spellchecking and accepting all "misspellings" in the template before you attempt to use it?
    I just tried it.
    It didn't report ANY spelling errors, wierd.

    And the only spelling 'error' I see in the output document is the name of the location that gets plugged into the top of each page. (6 in all)

  12. #12

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Ok, I made some changes that seem to have gotten rid of the pop ups

    Tell me if these are a mistakes.

    VB Code:
    1. lHwnd = FindWindow("OpusApp", vbNullString)
    2.     If lHwnd = 0 Then
    3.         Set MSWord = New Word.Application
    4.     Else
    5.         Set MSWord = Word.Application
    6. 'Added the next line so they can't click the wrong
    7. 'instance of word and set it active
    8.         MSWord.Application.Visible = False
    9.     End If

    And I added:
    VB Code:
    1. Clipboard.Clear
    After all the pasing is finished.
    Hopefully it will stop the 'clipboard' msg.
    Last edited by longwolf; Jun 30th, 2005 at 05:35 PM.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Error

    This is what I write to re-use an instance of Word if its running and if its not I create one.
    VB Code:
    1. Option Explicit
    2.  
    3. Private moApp As Word.Application
    4.  
    5. Private Sub Form_Load()
    6.    
    7.     On Error GoTo MyError
    8.    
    9.     Set moApp = GetObject(, "Word.Application")
    10.     If moApp Is Nothing Then
    11.         Set moApp = CreateObject("Word.Application")
    12.     End If
    13.     'Do some of your apps initialization stuff
    14.     '...
    15.     Exit Sub
    16.    
    17. MyError:
    18.     If Err.Number = 429 Then
    19.         Resume Next
    20.     Else
    21.         MsgBox Err.Number & " - " & Err.Description
    22.     End If
    23. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    What is the difference in using:
    Set moApp = GetObject(, "Word.Application") ?

  15. #15

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    DAAAAMN.
    Just found a new problem.
    If the user closes the application while it's making a Word File both the program and Word stay open but hidden.

    Should I make a new thread for this?

    I added flag (m_bProcessing).
    I check the flag in the Form_QueryUnload event
    If True I warn the user, give them a chance to change their minds and turn the flag false if they still want to stop.
    The 'Insertion' sub checks to see if the flag has changed and calls the '=Nothing' lines at the end of the sub.

    But that didn't help!

    I guess I could remove their option to kill the app while it's working, but that doesn't seem very Profes
    Last edited by longwolf; Jun 30th, 2005 at 06:58 PM.

  16. #16

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Just tried a 'work around'

    If they want to close the app it just hides itself until the job is done, then closes.
    But the app may run for another 5 mins (it's adding about 1500 pieces of info to the file).
    And the whole time WINWORD.EXE is pegging the CPU at about 98%.

    Then, when it does close, it pops up a box asking if you want to save the changes.

    Imagine an app asking to save changes 5 mins after you 'closed' it. AAAAAAAARRRRRRRRRGGGGGGG

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Error

    You need to post some of your code that inserts the data to the document. Maybe we can optimize it somehow.

    I guess GetObject and this "Set MSWord = Word.Application" may be the same thing.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: MS Word Error

    Do something like this when you want to save

    VB Code:
    1. wdApp.ActiveDocument.SaveAs FileName:="MyDoc.doc", FileFormat:= _
    2.         wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    3.         True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    4.         False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    5.         SaveAsAOCELetter:=False
    6.     wdApp.ActiveWindow.Close
    7.     Application.Quit

  19. #19

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Thanks guys

    I'm in the process of setting the Word Object to be global level.
    That way I can move a bunch of this code to a module and just kill the obj when the user wants out.

    I'll let you know how well it goes.
    I'll probably be screaming again
    Last edited by longwolf; Jun 30th, 2005 at 09:45 PM.

  20. #20

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Quote Originally Posted by RobDog888
    You need to post some of your code that inserts the data to the document. Maybe we can optimize it somehow.

    I guess GetObject and this "Set MSWord = Word.Application" may be the same thing.
    The app is formating tide tables for a give loc for a full year.
    The reason it's taking so long is because I get an error if I try to insert any text over 256 chars long.

    The basic insertion code hasn't changed sence my posting at 04:59 PM
    I could have consolidated some of the insertions but those sections need different font sizes.
    It was just easier to set the template Tags the correct font size(s) to begin with.
    (It would also be nice if I could change the font for the MoonPhase Inserts and use the Moons from the WingDings 2 Font, but that a whole other problem)


    BTW,
    On that 'GetObject', I believe I saw a post here were that code caused someone a problem.
    But I can't remember the details

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: MS Word Error

    Maybe you could use my method of creating the tables in Word, and saving it as a RTF file. Replace the entries with tags (I use [1] [2]) which you can then load as a document in a rtb in your app. Then you could replace [1] with your first item, and loop through the other ones. If you saved the moon phase X in a tag, you could prolly isolate it in between tags [mp1 X ], and then parse it out.

    I am assuming the phase would be different for each chart.

    The only problem I was having when the chart size changed. You shouldn't have that issue.

    Use the WYSIWYG example at the M$ site so that you get actual dimensions for a print preview of your RTB.

  22. #22

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Quote Originally Posted by dglienna
    Maybe you could use my method of creating the tables in Word, and saving it as a RTF file. Replace the entries with tags (I use [1] [2]) which you can then load as a document in a rtb in your app. Then you could replace [1] with your first item, and loop through the other ones. If you saved the moon phase X in a tag, you could prolly isolate it in between tags [mp1 X ], and then parse it out.

    I am assuming the phase would be different for each chart.

    The only problem I was having when the chart size changed. You shouldn't have that issue.

    Use the WYSIWYG example at the M$ site so that you get actual dimensions for a print preview of your RTB.
    Hi David,

    My client was pretty specific about the layout and file type he wants.

    But you've got me curious about you method, do you have an example somewhere?

    The document is laided out something like a calander with 2 months sided-by-sided per page.
    For the Moon phases, the app finds the 4 quarter moon dates for each month and adds an abbreviation to those days.
    NM = New mon
    FQ = First Quarter
    FM = Full Mon
    LQ = Last Quarter

    I had hoped to replace those abbreviations with moon symbols.
    But I'm having an issue with the font.
    See this link: Strange Font Problem
    http://www.vbforums.com/showthread.p...highlight=font

    Even if I get that working, I'd have to figure out how to change the chars and fonts at the same time, I'm guessing it'd use the Selection part of the Word Obj

  23. #23
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: MS Word Error

    Basically, I use .Find to search for a tag, then replace it with my output. This all fits in the cell of the table that I designed. For the headings on the top and bottom, I center and bold them, after I've written the rest of the info. I even copy in a different image into the third table (which has only one square cell that holds a picture).

    I'm assuming that you could get the quarters to show up on the document. Then, you could just copy and paste them (I think) from one place to another. You might have to set the font, but you can use .SelFont for that, as you know.

    I generate a receipt by replacing 45 fields, and formatting each one of them that includes about 25 prices, and a lot of detail that varies from page to page.

    I can't post it, but I'll help you. Start with the WYSIWYG RTB from M$'s site.
    It lets you work with a preview that is half the size of the printed page, like Word does.

    It took a while to get everything working, and the biggest problem was trying to get an image next to text. I finally gave up and placed it on the bottom of the page (above the footer)

  24. #24

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Hooray!
    I think the beast is tamed.
    I wouldn't call it house broken, but at least it seems to have stopped snapping at me

    David,
    I had a problem with the 'CreateObject' method.
    If the user closed the 1st doc and tried to make a new one, CreateObject reported the MSWord object as still existing.
    I adopted Marty's method and it's working fine.

  25. #25

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    One more task and I can (I hope) consider this job a complete success.

    I want to replace the inserted Moon Phase 'tags' with the moon symbol fonts from Wingdings 2.
    I can't seem to find a Chr$() value to create the First and Last Quarter Moon symbols.

    To get those chars from the font I had to :
    1. create an rtf file
    2. use the Windows Character Map to copy them to the file
    3 Load the new file into a RichTextBox.

    I tried getting it directly from the richtextbox like this:
    VB Code:
    1. sFind = "NM"
    2.     myRange.Find.Execute FindText:=sFind, _
    3.                          MatchCase:=True, _
    4.                          MatchWholeWord:=True, _
    5.                          ReplaceWith:=rtbSmbls.SelRTF, _
    6.                          Replace:=wdReplaceAll
    But that pasted all the formating for the symbol, as if you opened an rtf file in Notepad.


    So I created a sub to copy any one of the needed symbols to the windows clipboard.
    Next I tried getting the symbol from the windows clipboard:
    VB Code:
    1. MoonPhaseToClipboard FullMoon
    2.     g_MSWord.Selection.Find.Execute "FM", True, True, , , , True, wdFindContinue
    3.     g_MSWord.Selection.PasteAndFormat wdFormatOriginalFormatting
    But that pasted the entire original template into each of the tag positions, not good, lol

    Any ideas on how to replace the tags and the fonts from the WINDOWS clipboard at the same time?
    Last edited by longwolf; Jul 1st, 2005 at 11:45 AM.

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: MS Word Error

    Where X is your symbol, can't you do"

    VB Code:
    1. str = .Find "[  X ]"
    2. fq = mid$(str,3,1)

    if that doesn't work, just find it, and use str = .SelText to load it into the string.
    You might have to change the font when you paste it.

    If you post a small rtf, then I will try to mess around with it later tonight or tomorrow. Going out soon.

  27. #27

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Thanks David,
    But this is pasting onto a MS Word Document.

    It'd be easy If I was making an rtf file.

    I'm only using an rtf file to get the moon font symbols.
    I can get the correct symbol and copy it to the windows clipboard or find it in the Richtextbox.
    The Richtextbox is only used to hold those symbols.

    I can also find and select the correct 'tags' in the MS Word doc.

    I just can't seem to replace them with the symbols.

  28. #28
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: MS Word Error

    Quote Originally Posted by longwolf
    One more task and I can (I hope) consider this job a complete success.

    I want to replace the inserted Moon Phase 'tags' with the moon symbol fonts from Wingdings 2.
    I can't seem to find a Chr$() value to create the First and Last Quarter Moon symbols.

    To get those chars from the font I had to :
    1. create an rtf file
    2. use the Windows Character Map to copy them to the file
    3 Load the new file into a RichTextBox.

    I tried getting it directly from the richtextbox like this:
    VB Code:
    1. sFind = "NM"
    2.     myRange.Find.Execute FindText:=sFind, _
    3.                          MatchCase:=True, _
    4.                          MatchWholeWord:=True, _
    5.                          ReplaceWith:=rtbSmbls.SelRTF, _
    6.                          Replace:=wdReplaceAll
    But that pasted all the formating for the symbol, as if you opened an rtf file in Notepad.


    So I created a sub to copy any one of the needed symbols to the windows clipboard.
    Next I tried getting the symbol from the windows clipboard:
    VB Code:
    1. MoonPhaseToClipboard FullMoon
    2.     g_MSWord.Selection.Find.Execute "FM", True, True, , , , True, wdFindContinue
    3.     g_MSWord.Selection.PasteAndFormat wdFormatOriginalFormatting
    But that pasted the entire original template into each of the tag positions, not good, lol

    Any ideas on how to replace the tags and the fonts from the WINDOWS clipboard at the same time?
    I responded in your other thread and what I said was that I can't figure out how to do it using SendKeys but Alt plus 386 on the number pad will produce that character if the font is wingdings 2. The trick is to use the number pad and I've seen references that say you can use something like SendKeys "%{Pad3}{Pad8}{Pad6}" but that gives an error.

  29. #29

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Thanks Marty,

    I hadn't thought of SendKeys.

    Perhaps I could have Word Highlight the tags then use SendKeys to send the paste command after the char is sent to the clipboard?

    But at that point in the program the Word Doc is hidden.

    It seems like there should be a way to paste it direcctly useing the MSWord object and methods.

  30. #30

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    Quote Originally Posted by longwolf
    David,
    I had a problem with the 'CreateObject' method.
    If the user closed the 1st doc and tried to make a new one, CreateObject reported the MSWord object as still existing.
    I adopted Marty's method and it's working fine.

    Update to this.
    Marty's code had a problem if the user opened thier own Word Document while the program was running.

    I made a hybred sub using both of y'alls codes.
    It aint pretty, but it works

    VB Code:
    1. Public Function WordInitialize() As Boolean
    2.     Dim nRpt As Integer
    3.    
    4.     On Error GoTo ErrorRoutine:
    5. StartOver:
    6.     nRpt = FindWindow("OpusApp", vbNullString)
    7.     If nRpt = 0 Then
    8.         Set g_MSWord = New Word.Application
    9.     Else
    10.         Set g_MSWord = Word.Application
    11.     End If
    12.     WordInitialize = True
    13.     Exit Function
    14.    
    15. ErrorRoutine:
    16.     Select Case Err.Number
    17.         Case -2147023174
    18.             ' The user has manually shut down Word, so we need to
    19.             ' clean up and then restart Word.
    20.             For nRpt = 1 To g_MSWord.Documents.Count
    21.                 g_MSWord.Documents(nRpt).Close SaveChanges:=wdDoNotSaveChanges
    22.             Next nRpt
    23.             g_MSWord.Quit
    24.             Set g_MSWord = Nothing
    25.             Resume StartOver
    26.         Case 429
    27.             'ActiveX component can't create object
    28.             Resume Next
    29.         Case 462
    30.             Resume Next
    31.         Case Else
    32.             'Sometimes FindWindow doesn't work, try GetObject
    33.             On Error GoTo StillBad:
    34.             Set g_MSWord = GetObject(, "Word.Application")
    35.             If g_MSWord Is Nothing Then
    36.                 Set g_MSWord = CreateObject("Word.Application")
    37.             End If
    38.             g_lMSWordHwnd = FindWindow("OpusApp", vbNullString)
    39.             WordInitialize = True
    40.             Exit Function
    41. StillBad:
    42.             MsgBox Err.Number & " - " & Err.Description & vbCrLf & _
    43.                    "Please make sure there there are no other instances of MS Word running.", vbExclamation, App.Title
    44.     End Select
    45. End Function

  31. #31

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: MS Word Error

    I got it.

    For some reason I had to clear the clipboard before pasteing the new char to it.
    Then use the PasteSpecial method in a loop.
    It only replaces chars one at a time.
    VB Code:
    1. '    'change the font symbols
    2.     Clipboard.Clear
    3.     MoonPhaseToClipboard NewMoon
    4.     Do
    5.         g_MSWord.Selection.Find.Execute "NM", True, True, , , , True, wdFindContinue
    6.         If g_MSWord.Selection.Find.Found Then
    7.             g_MSWord.Selection.PasteSpecial , , , , wdPasteRTF
    8.         End If
    9.     Loop While g_MSWord.Selection.Find.Found

    Thanks Guys!
    You were a big help and also helped keep me thinking.

    Now as long as the guy doesn't call me back asking what happened to his MBR............
    Attached Images Attached Images  
    Last edited by longwolf; Jul 1st, 2005 at 06:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width